{"id":219488,"date":"2021-06-14T10:46:16","date_gmt":"2021-06-14T02:46:16","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=219488"},"modified":"2021-05-25T08:48:19","modified_gmt":"2021-05-25T00:48:19","slug":"mysql-trigger-use","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/mysql-trigger-use.html","title":{"rendered":"MySQL\u89e6\u53d1\u5668\u7684\u4f7f\u7528\u89c4\u5219"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u89e6\u53d1\u5668\u7528\u4e8e\u5728 MySQL \u6267\u884c\u63d2\u5165\u3001\u66f4\u65b0\u6216\u5220\u9664\u8bed\u53e5\u65f6\uff0c\u81ea\u52a8\u89e6\u53d1\u6267\u884c\u5176\u4ed6SQL\u4ee3\u7801\u3002\u672c\u6587\u8bb2\u89e3\u89e6\u53d1\u5668\u7684\u6b63\u786e\u4f7f\u7528\u65b9\u5f0f<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\u89e6\u53d1\u5668\u53ef\u4ee5\u5728\u6267\u884c\u8bed\u53e5\u524d\u6216\u6267\u884c\u540e\u89e6\u53d1\u5176\u4ed6 SQL \u4ee3\u7801\u8fd0\u884c\u3002\u89e6\u53d1\u5668\u53ef\u4ee5\u8bfb\u53d6\u89e6\u53d1\u8bed\u53e5\u6539\u53d8\u4e86\u54ea\u4e9b\u6570\u636e\uff0c\u4f46\u662f\u6ca1\u6709\u8fd4\u56de\u503c\u3002\u56e0\u6b64\u53ef\u4ee5\u4f7f\u7528\u89e6\u53d1\u5668\u52a0\u5f3a\u4e1a\u52a1\u903b\u8f91\u7684\u7ea6\u675f\u800c\u4e0d\u9700\u8981\u5728\u5e94\u7528\u7a0b\u5e8f\u5199\u5bf9\u5e94\u7684\u4ee3\u7801\u3002<\/p>\n

\u4ece\u4e0a\u8ff0\u63cf\u8ff0\u53ef\u4ee5\u770b\u5230\uff0c\u89e6\u53d1\u5668\u53ef\u4ee5\u7b80\u5316\u5e94\u7528\u7a0b\u5e8f\u7684\u903b\u8f91\u5e76\u4e14\u53ef\u4ee5\u63d0\u5347\u6027\u80fd\uff0c\u8fd9\u662f\u56e0\u4e3a\u4f7f\u7528\u89e6\u53d1\u5668\u51cf\u5c11\u4e86\u5e94\u7528\u7a0b\u5e8f\u548c\u670d\u52a1\u7aef\u7684\u4ea4\u4e92\u6b21\u6570\u3002\u540c\u65f6\uff0c\u89e6\u53d1\u5668\u6709\u52a9\u4e8e\u5b8c\u6210\u81ea\u52a8\u66f4\u65b0\u5f52\u4e00\u5316\u548c\u7edf\u8ba1\u6570\u636e\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u89e6\u53d1\u5668\u81ea\u52a8\u7edf\u8ba1\u4ea4\u6613\u8ba2\u5355\u603b\u91d1\u989d\uff0c\u8ba2\u5355\u6570\u53ca\u5e73\u5747\u5ba2\u5355\u4ef7\u3002 \u7136\u800c\uff0cMySQL \u7684\u89e6\u53d1\u5668\u7684\u5e94\u7528\u573a\u5408\u4e5f\u5341\u5206\u6709\u9650\uff0c\u5982\u679c\u4f60\u4f7f\u7528\u8fc7\u5176\u4ed6\u6570\u636e\u5e93\u4ea7\u54c1\u7684\u89e6\u53d1\u5668\uff0c\u4e0d\u8981\u4ee5\u4e3a MySQL \u4e5f\u80fd\u5b9e\u73b0\u76f8\u540c\u7684\u529f\u80fd\uff0c\u4f8b\u5982\uff1a<\/p>\n

    \n
  • \u6bcf\u4e2a\u6570\u636e\u8868\u7684\u5355\u4e00\u4e8b\u4ef6\u53ea\u80fd\u6709\u4e00\u4e2a\u89e6\u53d1\u5668\uff0c\u4e5f\u5c31\u662f\u8bf4\u5bf9\u4e8e AFTER INSERT \u8fd9\u6837\u7684\u4e8b\u4ef6\u6765\u8bf4\uff0c\u4e0d\u80fd\u540c\u65f6\u6709\u8d85\u8fc71\u4e2a\u7684\u89e6\u53d1\u5668\u3002<\/li>\n
  • MySQL \u53ea\u652f\u6301\u884c\u7ea7\u522b\u7684\u89e6\u53d1\u5668\uff0c\u4e5f\u5c31\u662f\u53ea\u80fd\u6309 FOR EACH ROW \u8fd9\u79cd\u65b9\u5f0f\u4f7f\u7528\u89e6\u53d1\u800c\u4e0d\u662f\u6574\u4e2a SQL \u8bed\u53e5\uff0c\u8fd9\u5bf9\u4e8e\u5927\u91cf\u6570\u636e\u7684\u64cd\u4f5c\u800c\u8a00\u4f1a\u6bd4\u8f83\u4f4e\u6548\u3002MySQL \u7684\u89e6\u53d1\u5668\u53ea\u80fd\u6309\u4e0b\u9762\u7684\u5f62\u5f0f\u7f16\u5199\uff1a<\/li>\n<\/ul>\n
    \r\nCREATE TRIGGER \u89e6\u53d1\u5668\u540d BEFORE|AFTER \u89e6\u53d1\u4e8b\u4ef6\r\nON \u8868\u540d FOR EACH ROW\r\nBEGIN\r\n    \u6267\u884c\u8bed\u53e5\u5217\u8868;\r\nEND\r\n<\/pre>\n

    \u6267\u884c\u8bed\u53e5\u5217\u8868\u652f\u6301\u5355\u6761\u6216\u591a\u6761\u8bed\u53e5\uff0c\u4e0b\u9762\u662f\u4e00\u4e2a\u591a\u6761\u8bed\u53e5\u7684\u793a\u4f8b\uff1a<\/p>\n

    \r\nDELIMITER $$\r\nCREATE TRIGGER user_create_log AFTER INSERT ON t_users FOR EACH ROW\r\nBEGIN\r\nDECLARE log_info VARCHAR(40)character set utf8;\r\nDECLARE description VARCHAR(20) character set utf8;#\u540e\u9762\u53d1\u73b0\u4e2d\u6587\u5b57\u7b26\u7f16\u7801\u51fa\u73b0\u4e71\u7801\uff0c\u8fd9\u91cc\u8bbe\u7f6e\u5b57\u7b26\u96c6\r\nSET description = \" is created\";\r\nSET log_info = CONCAT(NEW.user_name, description);     #\u51fd\u6570CONCAT\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u8fde\u63a5\r\nINSERT INTO logs(log) values(log_info);\r\nEND $$\r\n \r\nDELIMITER ;\r\n<\/pre>\n
      \n
    • \u89e6\u53d1\u5668\u53ef\u80fd\u5bfc\u81f4\u670d\u52a1\u7aef\u5b9e\u9645\u6267\u884c\u7684\u5de5\u4f5c\u4e0d\u53ef\u9884\u6d4b\uff0c\u4e00\u4e2a\u7b80\u5355\u7684\u8bed\u53e5\u53ef\u80fd\u5bfc\u81f4\u670d\u52a1\u7aef\u505a\u5927\u91cf\u4e0d\u53ef\u89c1\u7684\u5de5\u4f5c\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4e00\u4e2a\u89e6\u53d1\u5668\u66f4\u65b0\u4e86 \u4e00\u4e2a\u76f8\u5173\u7684\u8868\uff0c\u53ef\u80fd\u5bfc\u81f4\u53d7\u5f71\u54cd\u7684\u884c\u6570\u52a0\u500d\u3002<\/li>\n
    • \u89e6\u53d1\u5668\u96be\u4ee5\u8c03\u8bd5\uff0c\u5e76\u4e14\u4e00\u65e6\u5f15\u5165\u4e86\u89e6\u53d1\u5668\uff0c\u5f88\u96be\u5206\u6790\u6027\u80fd\u74f6\u9888\u3002<\/li>\n
    • \u89e6\u53d1\u5668\u4f1a\u5bfc\u81f4\u6f5c\u5728\u7684\u9501\u7b49\u5f85\u548c\u6b7b\u9501\u3002\u5982\u679c\u89e6\u53d1\u5668\u5931\u8d25\u4e86\uff0c\u6e90\u67e5\u8be2\u4e5f\u4f1a\u5931\u8d25\u3002\u5982\u679c\u6ca1\u6709\u610f\u8bc6\u5230\u89e6\u53d1\u5668\u7684\u5b58\u5728\uff0c\u8fd9\u7c7b\u73a9\u5462\u63d0\u5f88\u96be\u53d1\u73b0\u3002<\/li>\n<\/ul>\n

      \u5927\u591a\u6570\u9650\u5236\u4e2d\uff0c\u6700\u5927\u7684\u9650\u5236\u662f FOR EACH ROW \u7684\u8bbe\u8ba1\uff0c\u8fd9\u6709\u65f6\u5019\u5bfc\u81f4\u89e6\u53d1\u5668\u6ca1\u6cd5\u7528\u4e8e\u7ef4\u62a4\u7edf\u8ba1\u548c\u7f13\u5b58\u8868\uff0c\u8fd9\u662f\u56e0\u4e3a\u8fd9\u53ef\u80fd\u5f88\u6162\u3002\u4f7f\u7528\u89e6\u53d1\u5668\u7684\u4e3b\u8981\u7406\u7531\u662f\u76f8\u6bd4\u5b9a\u65f6\u540c\u6b65\u66f4\u65b0\uff0c\u89e6\u53d1\u5668\u53ef\u4ee5\u4e00\u81f4\u4fdd\u6301\u6570\u636e\u7684\u4e00\u81f4\u6027\u3002 \u89e6\u53d1\u5668\u4e5f\u6ca1\u6cd5\u4fdd\u8bc1\u539f\u5b50\u6027\u3002\u4f8b\u5982\uff0c\u66f4\u65b0 MyISAM \u6570\u636e\u8868\u7684\u89e6\u53d1\u5668\u5728\u6e90 SQL \u8bed\u53e5\u51fa\u9519\u540e\uff0c\u65e0\u6cd5\u56de\u6eda\u3002\u800c\u4e14\uff0c\u89e6\u53d1\u5668\u81ea\u8eab\u4e5f\u53ef\u80fd\u90fd\u53ea\u9519\u8bef\u3002\u5982\u679c\u6211\u4eec\u4f7f\u7528\u4e86 AFTER UPDATE \u57fa\u4e8e MyISAM \u6570\u636e\u8868\u53bb\u66f4\u65b0\u53e6\u4e00\u4e2a\u8868\u3002\u5982\u679c\u89e6\u53d1\u5668\u6709\u4e2a\u5bfc\u81f4\u7b2c\u4e8c\u5f20\u8868\u64cd\u4f5c\u5931\u8d25\u7684\u9519\u8bef\uff0c\u90a3\u5bf9\u4e8e\u7b2c\u4e00\u5f20\u8868\u7684\u64cd\u4f5c\u4e0d\u4f1a\u56de\u6eda\u3002<\/p>\n

      InnoDB \u7684\u89e6\u53d1\u5668\u76f8\u5173\u7684\u64cd\u4f5c\uff0c\u5305\u62ec\u6e90\u8bed\u53e5\u90fd\u5728\u540c\u4e00\u4e2a\u4e8b\u52a1\u4e2d\uff0c\u56e0\u6b64\u662f\u6ee1\u8db3\u539f\u5b50\u6027\u7684\u3002\u7136\u800c\uff0c\u5982\u679c\u4f7f\u7528InnoDB \u7684\u89e6\u53d1\u5668\u53bb\u4e0e\u53e6\u4e00\u5f20\u8868\u6821\u9a8c\u6570\u636e\u4e00\u81f4\u6027\u7684\u65f6\u5019\uff0c\u8fd9\u4e2a\u65f6\u5019\u5982\u679c\u4e0d\u5c0f\u5fc3\u7684\u8bdd\u53ef\u80fd\u5bfc\u81f4\u4e0d\u6b63\u786e\u7684\u7ed3\u679c\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u9700\u8981\u4f7f\u7528\u89e6\u53d1\u5668\u6a21\u62df\u5916\u952e\uff0c\u53ef\u4ee5\u4f7f\u7528 BEFORE INSERT\u89e6\u53d1\u5668\u9a8c\u8bc1\u53e6\u4e00\u5f20\u8868\u662f\u5426\u5b58\u5728\u5bf9\u5e94\u7684\u8bb0\u5f55\uff0c\u4f46\u662f\u5982\u679c\u5728\u89e6\u53d1\u5668\u8bfb\u53d6\u53e6\u4e00\u5f20\u8868\u6570\u636e\u7684\u65f6\u5019\u4e0d\u4f7f\u7528 SELECT FOR UPDATE\u7684\u8bdd\uff0c\u5219\u7531\u4e8e\u5e76\u53d1\u6027\u6027\u95ee\u9898\u53ef\u80fd\u5bfc\u81f4\u9519\u8bef\u7684\u7ed3\u679c\u3002 \u867d\u7136\u89e6\u53d1\u5668\u6709\u4e9b\u7f3a\u9677\uff0c\u4f46\u662f\u8fd9\u5e76\u4e0d\u610f\u5473\u7740\u4e0d\u80fd\u7528\u3002\u76f8\u53cd\uff0c\u89e6\u53d1\u5668\u672c\u8eab\u4e5f\u662f\u6709\u7528\u7684\uff0c\u5c24\u5176\u662f\u5bf9\u4e8e\u7ea6\u675f\uff0c\u7cfb\u7edf\u7ef4\u62a4\u4efb\u52a1\u548c\u4fdd\u6301\u7edf\u8ba1\u6570\u636e\u4fdd\u6301\u6700\u65b0\u3002<\/p>\n

      \u4e5f\u53ef\u4ee5\u4f7f\u7528\u89e6\u53d1\u5668\u8bb0\u5f55\u6570\u636e\u884c\u7684\u53d8\u5316\u3002\u8fd9\u6837\u5373\u4fbf\u662f\u79bb\u7ebf\u624b\u52a8\u64cd\u4f5c\u6570\u636e\u5e93\u7684\u8bb0\u5f55\uff08\u5982\u4fee\u590d\u9519\u8bef\u6570\u636e\uff09\u4e5f\u80fd\u591f\u88ab\u8bb0\u5f55\u4e0b\u6765\u3002\u4f46\u662f\uff0c\u9700\u8981\u6ce8\u610f\u7684\u662f\u5bf9\u4e8e\u5f80\u5176\u4ed6\u81ea\u589e\u4e3b\u952e\u8868\u63d2\u5165\u6570\u636e\u65f6\u8981\u5c0f\u5fc3\uff0c\u8fd9\u5bf9\u4e8e\u590d\u5236\u6027\u7684\u8bed\u53e5\u8868\u73b0\u4f1a\u6709\u95ee\u9898\uff0c\u56e0\u4e3a\u81ea\u589e\u503c\u5bf9\u4e8e\u4e24\u4e2a\u76f8\u540c\u7684\u526f\u672c\u503c\u5e76\u4e0d\u540c\u3002<\/p>\n

      \u89e6\u53d1\u5668\u5728\u6709\u9650\u7684\u573a\u5408\u80fd\u591f\u53d1\u6325\u5176\u4f18\u52bf\uff0c\u6bd4\u5982\u7edf\u8ba1\u6570\u636e\u3001\u6570\u636e\u8868\u53d8\u66f4\u65e5\u5fd7\u7b49\u3002\u4f46\u662f\u4e5f\u4f1a\u6709\u4e00\u4e9b\u7f3a\u9677\uff0c\u6bd4\u5982\u5927\u6570\u636e\u91cf\u7684\u66f4\u65b0\u7531\u4e8e\u9010\u884c\u89e6\u53d1\uff0c\u4f1a\u964d\u4f4e\u6548\u7387\u3002\u8fd8\u6709\u5c31\u662f\uff0cMyISAM \u5f15\u64ce\u65e0\u6cd5\u4fdd\u969c\u539f\u5b50\u6027\u3002\u56e0\u6b64\uff0c\u8981\u6839\u636e\u5e94\u7528\u573a\u666f\u662f\u5426\u8981\u662f\u6709\u89e6\u53d1\u5668\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

      \u89e6\u53d1\u5668\u53ef\u4ee5\u5728\u6267\u884c\u8bed\u53e5\u524d\u6216\u6267\u884c\u540e\u89e6\u53d1\u5176\u4ed6 SQL \u4ee3\u7801\u8fd0\u884c\u3002\u89e6\u53d1\u5668\u53ef\u4ee5\u8bfb\u53d6\u89e6\u53d1\u8bed\u53e5\u6539\u53d8\u4e86\u54ea\u4e9b\u6570\u636e\uff0c\u4f46\u662f\u6ca1\u6709\u8fd4\u56de\u503c […]<\/p>\n","protected":false},"author":668,"featured_media":4956,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-219488","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/219488","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/668"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=219488"}],"version-history":[{"count":2,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/219488\/revisions"}],"predecessor-version":[{"id":219490,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/219488\/revisions\/219490"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/4956"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=219488"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=219488"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=219488"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}