{"id":6377,"date":"2024-11-14T12:01:30","date_gmt":"2024-11-14T04:01:30","guid":{"rendered":"https:\/\/fwq.ai\/blog\/6377\/"},"modified":"2024-11-14T12:01:30","modified_gmt":"2024-11-14T04:01:30","slug":"mysql%e5%88%9b%e5%bb%ba%e8%a7%a6%e5%8f%91%e5%99%a8%e7%9a%84%e8%af%ad%e6%b3%95","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/6377\/","title":{"rendered":"mysql\u521b\u5efa\u89e6\u53d1\u5668\u7684\u8bed\u6cd5"},"content":{"rendered":"<blockquote><p>\n  mysql \u89e6\u53d1\u5668\u7684\u8bed\u6cd5\uff1acreate trigger [\u89e6\u53d1\u5668\u540d]before\/after [insert\/update\/delete] on [\u8868\u540d]for each row[\u89e6\u53d1\u5668\u4e3b\u4f53]\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202408\/01\/2024080106154494701.jpg\" class=\"aligncenter\" title=\"mysql\u521b\u5efa\u89e6\u53d1\u5668\u7684\u8bed\u6cd5\u63d2\u56fe\" alt=\"mysql\u521b\u5efa\u89e6\u53d1\u5668\u7684\u8bed\u6cd5\u63d2\u56fe\" \/><\/p>\n<p><strong>MySQL \u89e6\u53d1\u5668\u7684\u8bed\u6cd5<\/strong><\/p>\n<p>\u89e6\u53d1\u5668\u662f\u4e00\u79cd\u6570\u636e\u5e93\u5bf9\u8c61\uff0c\u7528\u4e8e\u5728\u63d2\u5165\u3001\u66f4\u65b0\u6216\u5220\u9664\u8868\u4e2d\u7684\u8bb0\u5f55\u65f6\u81ea\u52a8\u6267\u884c\u7279\u5b9a\u64cd\u4f5c\u3002MySQL \u4e2d\u89e6\u53d1\u5668\u7684\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>CREATE TRIGGER [\u89e6\u53d1\u5668\u540d]\nBEFORE\/AFTER [INSERT\/UPDATE\/DELETE] ON [\u8868\u540d]\nFOR EACH ROW\n[\u89e6\u53d1\u5668\u4e3b\u4f53]<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u8bed\u6cd5\u53c2\u6570\u89e3\u91ca\uff1a<\/strong><\/p>\n<ul>\n<li> <strong>\u89e6\u53d1\u5668\u540d\uff1a<\/strong>\u4e3a\u89e6\u53d1\u5668\u6307\u5b9a\u4e00\u4e2a\u540d\u79f0\u3002<\/li>\n<li> <strong>\u65f6\u673a\uff1a<\/strong>\u6307\u5b9a\u89e6\u53d1\u5668\u5728\u64cd\u4f5c\u53d1\u751f\u524d\uff08BEFORE\uff09\u6216\u540e\uff08AFTER\uff09\u6267\u884c\u3002<\/li>\n<li> <strong>\u4e8b\u4ef6\uff1a<\/strong>\u6307\u5b9a\u89e6\u53d1\u5668\u5728\u54ea\u4e9b\u64cd\u4f5c\uff08INSERT\/UPDATE\/DELETE\uff09\u4e0a\u89e6\u53d1\u3002<\/li>\n<li> <strong>\u8868\u540d\uff1a<\/strong>\u6307\u5b9a\u8981\u4e3a\u5176\u521b\u5efa\u89e6\u53d1\u5668\u7684\u8868\u3002<\/li>\n<li> <strong>\u89e6\u53d1\u5668\u4e3b\u4f53\uff1a<\/strong>\u89e6\u53d1\u5668\u6267\u884c\u7684\u52a8\u4f5c\uff0c\u5305\u62ec SQL \u8bed\u53e5\u548c\u63a7\u5236\u6d41\u8bed\u53e5\u3002<\/li>\n<li> <strong>FOR EACH ROW\uff1a<\/strong>\u6307\u5b9a\u89e6\u53d1\u5668\u5bf9\u6bcf\u4e2a\u53d7\u5f71\u54cd\u7684\u884c\u6267\u884c\u4e00\u6b21\u3002<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<p>\u4ee5\u4e0b\u89e6\u53d1\u5668\u793a\u4f8b\u4f1a\u5728\u5411 customers \u8868\u4e2d\u63d2\u5165\u65b0\u8bb0\u5f55\u65f6\u66f4\u65b0 last_updated \u5217\uff1a<\/p>\n<pre>CREATE TRIGGER update_last_updated\nBEFORE INSERT ON customers\nFOR EACH ROW\nSET last_updated = NOW();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u8be6\u7ec6\u5185\u5bb9\uff1a<\/strong><\/p>\n<ul>\n<li> <strong>\u89e6\u53d1\u5668\u65f6\u673a\uff1a<\/strong>\u89e6\u53d1\u5668\u53ef\u4ee5\u5728\u64cd\u4f5c\u4e4b\u524d\uff08BEFORE\uff09\u6216\u4e4b\u540e\uff08AFTER\uff09\u6267\u884c\u3002<\/li>\n<li> <strong>\u89e6\u53d1\u5668\u4e8b\u4ef6\uff1a<\/strong>\u89e6\u53d1\u5668\u53ef\u4ee5\u5728 INSERT\u3001UPDATE \u6216 DELETE \u64cd\u4f5c\u4e0a\u89e6\u53d1\u3002<\/li>\n<li>\n<p><strong>\u89e6\u53d1\u5668\u4e3b\u4f53\uff1a<\/strong>\u89e6\u53d1\u5668\u4e3b\u4f53\u53ef\u4ee5\u5305\u542b\u4ee5\u4e0b\u5185\u5bb9\uff1a<\/p>\n<ul>\n<li>SQL \u8bed\u53e5\uff08\u4f8b\u5982\uff0c\u66f4\u65b0\u3001\u63d2\u5165\u6216\u5220\u9664\u5176\u4ed6\u8868\u4e2d\u7684\u8bb0\u5f55\uff09\u3002<\/li>\n<li>\u63a7\u5236\u6d41\u8bed\u53e5\uff08\u4f8b\u5982\uff0cIF-ELSE\u3001CASE \u7b49\uff09\u3002<\/li>\n<li>\u4f7f\u7528 OLD \u548c NEW \u5173\u952e\u5b57\u8bbf\u95ee\u89e6\u53d1\u64cd\u4f5c\u524d\u540e\u7684\u8bb0\u5f55\u503c\u3002<\/li>\n<\/ul>\n<\/li>\n<li> <strong>\u9650\u5236\uff1a<\/strong>\u89e6\u53d1\u5668\u4e0d\u80fd\u5f15\u7528\u540c\u4e00\u8868\u4e2d\u7684\u5176\u4ed6\u89e6\u53d1\u5668\u3002<\/li>\n<\/ul>\n<p>\u901a\u8fc7\u521b\u5efa\u89e6\u53d1\u5668\uff0c\u53ef\u4ee5\u81ea\u52a8\u5316\u6570\u636e\u5e93\u4e2d\u7684\u4efb\u52a1\uff0c\u51cf\u5c11\u91cd\u590d\u64cd\u4f5c\u5e76\u786e\u4fdd\u6570\u636e\u5b8c\u6574\u6027\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u521b\u5efa\u89e6\u53d1\u5668\u7684\u8bed\u6cd5\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u7f51\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>mysql \u89e6\u53d1\u5668\u7684\u8bed\u6cd5\uff1acreate trigger [\u89e6\u53d1\u5668\u540d]before\/after [insert\/update\/delete] on [\u8868\u540d]for each row[\u89e6\u53d1\u5668\u4e3b\u4f53] MySQL \u89e6\u53d1\u5668\u7684\u8bed\u6cd5 \u89e6\u53d1\u5668\u662f\u4e00\u79cd\u6570\u636e\u5e93\u5bf9\u8c61\uff0c\u7528\u4e8e\u5728\u63d2\u5165\u3001\u66f4\u65b0\u6216\u5220\u9664\u8868\u4e2d\u7684\u8bb0\u5f55\u65f6\u81ea\u52a8\u6267\u884c\u7279\u5b9a\u64cd\u4f5c\u3002MySQL \u4e2d\u89e6\u53d1\u5668\u7684\u8bed\u6cd5\u5982\u4e0b\uff1a CREATE TRIGGER [\u89e6\u53d1\u5668\u540d] BEFORE\/AFTER [INSERT\/UPDATE\/DELETE] ON [\u8868\u540d] FOR EACH ROW [\u89e6\u53d1\u5668\u4e3b\u4f53] \u767b\u5f55\u540e\u590d\u5236 \u8bed\u6cd5\u53c2\u6570\u89e3\u91ca\uff1a \u89e6\u53d1\u5668\u540d\uff1a\u4e3a\u89e6\u53d1\u5668\u6307\u5b9a\u4e00\u4e2a\u540d\u79f0\u3002 \u65f6\u673a\uff1a\u6307\u5b9a\u89e6\u53d1\u5668\u5728\u64cd\u4f5c\u53d1\u751f\u524d\uff08BEFORE\uff09\u6216\u540e\uff08AFTER\uff09\u6267\u884c\u3002 \u4e8b\u4ef6\uff1a\u6307\u5b9a\u89e6\u53d1\u5668\u5728\u54ea\u4e9b\u64cd\u4f5c\uff08INSERT\/UPDATE\/DELETE\uff09\u4e0a\u89e6\u53d1\u3002 \u8868\u540d\uff1a\u6307\u5b9a\u8981\u4e3a\u5176\u521b\u5efa\u89e6\u53d1\u5668\u7684\u8868\u3002 \u89e6\u53d1\u5668\u4e3b\u4f53\uff1a\u89e6\u53d1\u5668\u6267\u884c\u7684\u52a8\u4f5c\uff0c\u5305\u62ec SQL \u8bed\u53e5\u548c\u63a7\u5236\u6d41\u8bed\u53e5\u3002 FOR EACH ROW\uff1a\u6307\u5b9a\u89e6\u53d1\u5668\u5bf9\u6bcf\u4e2a\u53d7\u5f71\u54cd\u7684\u884c\u6267\u884c\u4e00\u6b21\u3002 \u793a\u4f8b\uff1a \u4ee5\u4e0b\u89e6\u53d1\u5668\u793a\u4f8b\u4f1a\u5728\u5411 customers \u8868\u4e2d\u63d2\u5165\u65b0\u8bb0\u5f55\u65f6\u66f4\u65b0 last_updated \u5217\uff1a CREATE TRIGGER update_last_updated BEFORE INSERT ON customers FOR EACH ROW SET last_updated = [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-6377","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6377","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=6377"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6377\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=6377"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=6377"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=6377"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}