{"id":5416,"date":"2024-11-14T10:13:20","date_gmt":"2024-11-14T02:13:20","guid":{"rendered":"https:\/\/fwq.ai\/blog\/5416\/"},"modified":"2024-11-14T10:13:20","modified_gmt":"2024-11-14T02:13:20","slug":"mysql%e5%ad%98%e5%82%a8%e8%bf%87%e7%a8%8b%e6%80%8e%e4%b9%88%e8%b0%83%e8%af%95","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/5416\/","title":{"rendered":"mysql\u5b58\u50a8\u8fc7\u7a0b\u600e\u4e48\u8c03\u8bd5"},"content":{"rendered":"<blockquote><p>\n  \u8c03\u8bd5 mysql \u5b58\u50a8\u8fc7\u7a0b\u9075\u5faa\u4ee5\u4e0b\u6b65\u9aa4\uff1a\u542f\u7528\u8c03\u8bd5\u9009\u9879\u521b\u5efa\u5b58\u50a8\u8fc7\u7a0b\u52a0\u8f7d\u5b58\u50a8\u8fc7\u7a0b\u521b\u5efa\u89e6\u53d1\u5668\u6267\u884c\u5b58\u50a8\u8fc7\u7a0b\u68c0\u67e5\u89e6\u53d1\u5668\u83b7\u53d6\u9519\u8bef\u6d88\u606f\u7981\u7528\u8c03\u8bd5\u9009\u9879\u5220\u9664\u89e6\u53d1\u5668\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202405\/28\/2024052811542511569.jpg\" class=\"aligncenter\" title=\"mysql\u5b58\u50a8\u8fc7\u7a0b\u600e\u4e48\u8c03\u8bd5\u63d2\u56fe\" alt=\"mysql\u5b58\u50a8\u8fc7\u7a0b\u600e\u4e48\u8c03\u8bd5\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5728 MySQL \u4e2d\u8c03\u8bd5\u5b58\u50a8\u8fc7\u7a0b<\/strong><\/p>\n<p><strong>\u5982\u4f55\u8c03\u8bd5 MySQL \u5b58\u50a8\u8fc7\u7a0b\uff1f<\/strong><\/p>\n<p>\u8c03\u8bd5 MySQL \u5b58\u50a8\u8fc7\u7a0b\u9075\u5faa\u4ee5\u4e0b\u6b65\u9aa4\uff1a<\/p>\n<p><strong>1. \u542f\u7528\u8c03\u8bd5\u9009\u9879<\/strong><\/p>\n<ul>\n<li>\n<p>\u5728 MySQL \u547d\u4ee4\u884c\u7a97\u53e3\u4e2d\uff0c\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<pre>SET SQL_SAFE_UPDATES = 0;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/li>\n<\/ul>\n<p><strong>2. \u521b\u5efa\u5b58\u50a8\u8fc7\u7a0b<\/strong><\/p>\n<ul>\n<li>\u8981\u8c03\u8bd5\u7684\u5b58\u50a8\u8fc7\u7a0b\u5e94\u5c01\u88c5\u5728 BEGIN &#8230; END \u5757\u4e2d\u3002<\/li>\n<li>\u4f7f\u7528 DELIMITER \u547d\u4ee4\u66f4\u6539\u5206\u9694\u7b26\uff0c\u4ee5\u9632\u5b58\u50a8\u8fc7\u7a0b\u4f53\u4e2d\u51fa\u73b0\u5206\u53f7\u3002<\/li>\n<li>\u521b\u5efa\u5b58\u50a8\u8fc7\u7a0b\u5e76\u5c06\u5176\u4fdd\u5b58\u5728\u4ee5 .sql \u7ed3\u5c3e\u7684\u6587\u4ef6\u4e2d\u3002<\/li>\n<\/ul>\n<p><strong>3. \u52a0\u8f7d\u5b58\u50a8\u8fc7\u7a0b<\/strong><\/p>\n<ul>\n<li>\n<p>\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5c06\u5b58\u50a8\u8fc7\u7a0b\u52a0\u8f7d\u5230 MySQL\uff1a<\/p>\n<pre>SOURCE &lt;path_to_file.sql&gt;;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/li>\n<\/ul>\n<p><strong>4. \u521b\u5efa\u89e6\u53d1\u5668<\/strong><\/p>\n<ul>\n<li>\u4e3a\u5b58\u50a8\u8fc7\u7a0b\u521b\u5efa\u89e6\u53d1\u5668\u6765\u6355\u83b7\u9519\u8bef\u3002<\/li>\n<li>\u89e6\u53d1\u5668\u5e94\u5177\u6709\u4ee5\u4e0b\u7ed3\u6784\uff1a<\/li>\n<\/ul>\n<pre>CREATE TRIGGER &lt;trigger_name&gt;\nBEFORE INSERT ON &lt;table_name&gt;\nFOR EACH ROW\nBEGIN\n    SIGNAL SQLSTATE '02000'\n    SET MESSAGE_TEXT = 'Custom error message';\nEND;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>5. \u6267\u884c\u5b58\u50a8\u8fc7\u7a0b<\/strong><\/p>\n<ul>\n<li>\n<p>\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u6267\u884c\u5b58\u50a8\u8fc7\u7a0b\uff1a<\/p>\n<pre>CALL &lt;procedure_name&gt;();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/li>\n<\/ul>\n<p><strong>6. \u68c0\u67e5\u89e6\u53d1\u5668<\/strong><\/p>\n<ul>\n<li>\u5982\u679c\u5b58\u50a8\u8fc7\u7a0b\u51fa\u73b0\u9519\u8bef\uff0c\u89e6\u53d1\u5668\u5c06\u89e6\u53d1\u5e76\u5b58\u50a8\u9519\u8bef\u6d88\u606f\u3002<\/li>\n<li>\n<p>\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u68c0\u67e5\u89e6\u53d1\u5668\uff1a<\/p>\n<pre>SHOW TRIGGERS LIKE '&lt;trigger_name&gt;';<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/li>\n<\/ul>\n<p><strong>7. \u83b7\u53d6\u9519\u8bef\u6d88\u606f<\/strong><\/p>\n<ul>\n<li>\u9519\u8bef\u6d88\u606f\u5b58\u50a8\u5728 Message_text \u5b57\u6bb5\u4e2d\u3002<\/li>\n<li>\n<p>\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u83b7\u53d6\u9519\u8bef\u6d88\u606f\uff1a<\/p>\n<pre>SELECT Message_text FROM INFORMATION_SCHEMA.TRIGGERS WHERE TRIGGER_NAME = '&lt;trigger_name&gt;';<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/li>\n<\/ul>\n<p><strong>8. \u7981\u7528\u8c03\u8bd5\u9009\u9879<\/strong><\/p>\n<ul>\n<li>\n<p>\u8c03\u8bd5\u5b8c\u6210\u540e\uff0c\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u7981\u7528\u8c03\u8bd5\u9009\u9879\uff1a<\/p>\n<pre>SET SQL_SAFE_UPDATES = 1;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/li>\n<\/ul>\n<p><strong>9. \u5220\u9664\u89e6\u53d1\u5668<\/strong><\/p>\n<ul>\n<li>\n<p>\u5220\u9664\u7528\u4e8e\u6355\u83b7\u9519\u8bef\u7684\u89e6\u53d1\u5668\uff1a<\/p>\n<pre>DROP TRIGGER &lt;trigger_name&gt;;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5b58\u50a8\u8fc7\u7a0b\u600e\u4e48\u8c03\u8bd5\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>\u8c03\u8bd5 mysql \u5b58\u50a8\u8fc7\u7a0b\u9075\u5faa\u4ee5\u4e0b\u6b65\u9aa4\uff1a\u542f\u7528\u8c03\u8bd5\u9009\u9879\u521b\u5efa\u5b58\u50a8\u8fc7\u7a0b\u52a0\u8f7d\u5b58\u50a8\u8fc7\u7a0b\u521b\u5efa\u89e6\u53d1\u5668\u6267\u884c\u5b58\u50a8\u8fc7\u7a0b\u68c0\u67e5\u89e6\u53d1\u5668\u83b7\u53d6\u9519\u8bef\u6d88\u606f\u7981\u7528\u8c03\u8bd5\u9009\u9879\u5220\u9664\u89e6\u53d1\u5668 \u5982\u4f55\u5728 MySQL \u4e2d\u8c03\u8bd5\u5b58\u50a8\u8fc7\u7a0b \u5982\u4f55\u8c03\u8bd5 MySQL \u5b58\u50a8\u8fc7\u7a0b\uff1f \u8c03\u8bd5 MySQL \u5b58\u50a8\u8fc7\u7a0b\u9075\u5faa\u4ee5\u4e0b\u6b65\u9aa4\uff1a 1. \u542f\u7528\u8c03\u8bd5\u9009\u9879 \u5728 MySQL \u547d\u4ee4\u884c\u7a97\u53e3\u4e2d\uff0c\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\uff1a SET SQL_SAFE_UPDATES = 0; \u767b\u5f55\u540e\u590d\u5236 2. \u521b\u5efa\u5b58\u50a8\u8fc7\u7a0b \u8981\u8c03\u8bd5\u7684\u5b58\u50a8\u8fc7\u7a0b\u5e94\u5c01\u88c5\u5728 BEGIN &#8230; END \u5757\u4e2d\u3002 \u4f7f\u7528 DELIMITER \u547d\u4ee4\u66f4\u6539\u5206\u9694\u7b26\uff0c\u4ee5\u9632\u5b58\u50a8\u8fc7\u7a0b\u4f53\u4e2d\u51fa\u73b0\u5206\u53f7\u3002 \u521b\u5efa\u5b58\u50a8\u8fc7\u7a0b\u5e76\u5c06\u5176\u4fdd\u5b58\u5728\u4ee5 .sql \u7ed3\u5c3e\u7684\u6587\u4ef6\u4e2d\u3002 3. \u52a0\u8f7d\u5b58\u50a8\u8fc7\u7a0b \u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5c06\u5b58\u50a8\u8fc7\u7a0b\u52a0\u8f7d\u5230 MySQL\uff1a SOURCE &lt;path_to_file.sql&gt;; \u767b\u5f55\u540e\u590d\u5236 4. \u521b\u5efa\u89e6\u53d1\u5668 \u4e3a\u5b58\u50a8\u8fc7\u7a0b\u521b\u5efa\u89e6\u53d1\u5668\u6765\u6355\u83b7\u9519\u8bef\u3002 \u89e6\u53d1\u5668\u5e94\u5177\u6709\u4ee5\u4e0b\u7ed3\u6784\uff1a CREATE TRIGGER &lt;trigger_name&gt; BEFORE INSERT ON &lt;table_name&gt; FOR EACH [&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-5416","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5416","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=5416"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5416\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=5416"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=5416"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=5416"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}