{"id":5157,"date":"2024-11-14T11:41:01","date_gmt":"2024-11-14T03:41:01","guid":{"rendered":"https:\/\/fwq.ai\/blog\/5157\/"},"modified":"2024-11-14T11:41:01","modified_gmt":"2024-11-14T03:41:01","slug":"mysql%e6%80%8e%e4%b9%88%e8%ae%be%e7%bd%ae%e4%b8%bb%e5%a4%96%e9%94%ae%e5%85%b3%e8%81%94","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/5157\/","title":{"rendered":"mysql\u600e\u4e48\u8bbe\u7f6e\u4e3b\u5916\u952e\u5173\u8054"},"content":{"rendered":"<blockquote><p>\n  \u5728 mysql \u4e2d\u8bbe\u7f6e\u4e3b\u5916\u952e\u5173\u8054\u9700\u8981\u9075\u5faa\u4ee5\u4e0b\u6b65\u9aa4\uff1a\u5728\u5b50\u8868\u4e2d\u5b9a\u4e49\u5916\u952e\u7ea6\u675f\uff0c\u6307\u5b9a\u5916\u952e\u5217\u4e0e\u4e3b\u8868\u4e2d\u76f8\u5173\u8054\u7684\u5217\uff1b\u6307\u5b9a\u66f4\u65b0\u548c\u5220\u9664\u64cd\u4f5c\u65f6\u5b50\u8868\u8bb0\u5f55\u5e94\u5982\u4f55\u5904\u7406\uff1b\u6dfb\u52a0\u5916\u952e\u7ea6\u675f\u540e\uff0cmysql \u5c06\u5f3a\u5236\u6267\u884c\u6570\u636e\u5b8c\u6574\u6027\uff0c\u786e\u4fdd\u5b50\u8868\u4e2d\u7684\u8bb0\u5f55\u59cb\u7ec8\u4e0e\u4e3b\u8868\u4e2d\u7684\u76f8\u5173\u8bb0\u5f55\u5173\u8054\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202405\/22\/2024052217392577397.jpg\" class=\"aligncenter\" title=\"mysql\u600e\u4e48\u8bbe\u7f6e\u4e3b\u5916\u952e\u5173\u8054\u63d2\u56fe\" alt=\"mysql\u600e\u4e48\u8bbe\u7f6e\u4e3b\u5916\u952e\u5173\u8054\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u8bbe\u7f6e MySQL \u4e2d\u7684\u4e3b\u5916\u952e\u5173\u8054\uff1f<\/strong><\/p>\n<p>\u5728 MySQL \u6570\u636e\u5e93\u4e2d\uff0c\u901a\u8fc7\u8bbe\u7f6e\u4e3b\u5916\u952e\u5173\u8054\u53ef\u4ee5\u5efa\u7acb\u8868\u4e4b\u95f4\u7684\u5173\u7cfb\u3002\u4e3b\u5916\u952e\u5173\u8054\u786e\u4fdd\u5b50\u8868\u4e2d\u7684\u8bb0\u5f55\u4e0e\u4e3b\u8868\u4e2d\u7684\u8bb0\u5f55\u76f8\u5173\u8054\uff0c\u4ece\u800c\u7ef4\u62a4\u6570\u636e\u5b8c\u6574\u6027\u548c\u4e00\u81f4\u6027\u3002<\/p>\n<p><strong>\u6b65\u9aa4\uff1a<\/strong><\/p>\n<p><strong>1. \u5b9a\u4e49\u5916\u952e\u7ea6\u675f<\/strong><\/p>\n<p>\u5728\u5b50\u8868\u4e2d\u4e3a\u5916\u952e\u5217\u6dfb\u52a0 FOREIGN KEY \u7ea6\u675f\uff0c\u8be5\u7ea6\u675f\u6307\u5b9a\u5b83\u4e0e\u4e3b\u8868\u7684\u54ea\u4e2a\u5217\u76f8\u5173\u8054\u3002<\/p>\n<p>\u8bed\u6cd5\uff1a<\/p>\n<pre>ALTER TABLE \u5b50\u8868 ADD FOREIGN KEY (`\u5916\u952e\u5217`) REFERENCES \u4e3b\u8868 (`\u4e3b\u8868\u5217`)<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u6307\u5b9a\u66f4\u65b0\u548c\u5220\u9664\u64cd\u4f5c<\/strong><\/p>\n<p>\u6307\u5b9a\u5f53\u4e3b\u8868\u7684\u76f8\u5173\u8bb0\u5f55\u88ab\u66f4\u65b0\u6216\u5220\u9664\u65f6\uff0c\u5b50\u8868\u8bb0\u5f55\u5e94\u5982\u4f55\u5904\u7406\u3002<\/p>\n<ul>\n<li>ON UPDATE CASCADE\uff1a\u5f53\u4e3b\u8868\u8bb0\u5f55\u66f4\u65b0\u65f6\uff0c\u81ea\u52a8\u66f4\u65b0\u5b50\u8868\u4e2d\u76f8\u5173\u7684\u5916\u952e\u503c\u3002<\/li>\n<li>ON DELETE CASCADE\uff1a\u5f53\u4e3b\u8868\u8bb0\u5f55\u5220\u9664\u65f6\uff0c\u81ea\u52a8\u5220\u9664\u5b50\u8868\u4e2d\u76f8\u5173\u7684\u5916\u952e\u8bb0\u5f55\u3002<\/li>\n<li>ON UPDATE SET NULL\uff1a\u5f53\u4e3b\u8868\u8bb0\u5f55\u66f4\u65b0\u65f6\uff0c\u5c06\u5b50\u8868\u4e2d\u76f8\u5173\u7684\u5916\u952e\u503c\u8bbe\u7f6e\u4e3a NULL\u3002<\/li>\n<li>ON DELETE SET NULL\uff1a\u5f53\u4e3b\u8868\u8bb0\u5f55\u5220\u9664\u65f6\uff0c\u5c06\u5b50\u8868\u4e2d\u76f8\u5173\u7684\u5916\u952e\u503c\u8bbe\u7f6e\u4e3a NULL\u3002<\/li>\n<\/ul>\n<p><strong>3. \u7ef4\u62a4\u6570\u636e\u5b8c\u6574\u6027<\/strong><\/p>\n<p>\u6dfb\u52a0\u4e86\u5916\u952e\u7ea6\u675f\u540e\uff0cMySQL \u5c06\u5f3a\u5236\u6267\u884c\u6570\u636e\u5b8c\u6574\u6027\uff0c\u786e\u4fdd\u5b50\u8868\u4e2d\u7684\u8bb0\u5f55\u59cb\u7ec8\u4e0e\u4e3b\u8868\u4e2d\u7684\u76f8\u5173\u8bb0\u5f55\u5173\u8054\u3002\u5982\u679c\u8fdd\u53cd\u8be5\u7ea6\u675f\uff0c\u5c06\u89e6\u53d1\u9519\u8bef\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<p>\u5047\u8bbe\u6211\u4eec\u6709\u4e24\u4e2a\u8868\uff1a<\/p>\n<ul>\n<li>\u8ba2\u5355\u8868\uff0c\u5305\u542b\u8ba2\u5355\u4fe1\u606f\u3002<\/li>\n<li>\u8ba2\u5355\u660e\u7ec6\u8868\uff0c\u5305\u542b\u6bcf\u4e2a\u8ba2\u5355\u7684\u5546\u54c1\u8be6\u7ec6\u4fe1\u606f\u3002<\/li>\n<\/ul>\n<p>\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u8bbe\u7f6e\u4e3b\u5916\u952e\u5173\u8054\uff1a<\/p>\n<pre>-- \u5728`\u8ba2\u5355\u660e\u7ec6`\u8868\u4e2d\u521b\u5efa\u5916\u952e\u7ea6\u675f\nALTER TABLE \u8ba2\u5355\u660e\u7ec6 ADD FOREIGN KEY (`\u8ba2\u5355_ID`) REFERENCES \u8ba2\u5355 (`\u8ba2\u5355_ID`) ON UPDATE CASCADE ON DELETE CASCADE;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u73b0\u5728\uff0c\u8ba2\u5355\u660e\u7ec6\u8868\u4e2d\u7684 \u8ba2\u5355_ID \u5217\u5c06\u4e0e \u8ba2\u5355 \u8868\u4e2d\u7684 \u8ba2\u5355_ID \u5217\u5173\u8054\u3002\u5f53 \u8ba2\u5355 \u8868\u4e2d\u7684\u8bb0\u5f55\u66f4\u65b0\u6216\u5220\u9664\u65f6\uff0c\u8ba2\u5355\u660e\u7ec6 \u8868\u4e2d\u7684\u76f8\u5173\u8bb0\u5f55\u5c06\u81ea\u52a8\u66f4\u65b0\u6216\u5220\u9664\uff0c\u4ece\u800c\u7ef4\u62a4\u6570\u636e\u5b8c\u6574\u6027\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u600e\u4e48\u8bbe\u7f6e\u4e3b\u5916\u952e\u5173\u8054\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>\u5728 mysql \u4e2d\u8bbe\u7f6e\u4e3b\u5916\u952e\u5173\u8054\u9700\u8981\u9075\u5faa\u4ee5\u4e0b\u6b65\u9aa4\uff1a\u5728\u5b50\u8868\u4e2d\u5b9a\u4e49\u5916\u952e\u7ea6\u675f\uff0c\u6307\u5b9a\u5916\u952e\u5217\u4e0e\u4e3b\u8868\u4e2d\u76f8\u5173\u8054\u7684\u5217\uff1b\u6307\u5b9a\u66f4\u65b0\u548c\u5220\u9664\u64cd\u4f5c\u65f6\u5b50\u8868\u8bb0\u5f55\u5e94\u5982\u4f55\u5904\u7406\uff1b\u6dfb\u52a0\u5916\u952e\u7ea6\u675f\u540e\uff0cmysql \u5c06\u5f3a\u5236\u6267\u884c\u6570\u636e\u5b8c\u6574\u6027\uff0c\u786e\u4fdd\u5b50\u8868\u4e2d\u7684\u8bb0\u5f55\u59cb\u7ec8\u4e0e\u4e3b\u8868\u4e2d\u7684\u76f8\u5173\u8bb0\u5f55\u5173\u8054\u3002 \u5982\u4f55\u8bbe\u7f6e MySQL \u4e2d\u7684\u4e3b\u5916\u952e\u5173\u8054\uff1f \u5728 MySQL \u6570\u636e\u5e93\u4e2d\uff0c\u901a\u8fc7\u8bbe\u7f6e\u4e3b\u5916\u952e\u5173\u8054\u53ef\u4ee5\u5efa\u7acb\u8868\u4e4b\u95f4\u7684\u5173\u7cfb\u3002\u4e3b\u5916\u952e\u5173\u8054\u786e\u4fdd\u5b50\u8868\u4e2d\u7684\u8bb0\u5f55\u4e0e\u4e3b\u8868\u4e2d\u7684\u8bb0\u5f55\u76f8\u5173\u8054\uff0c\u4ece\u800c\u7ef4\u62a4\u6570\u636e\u5b8c\u6574\u6027\u548c\u4e00\u81f4\u6027\u3002 \u6b65\u9aa4\uff1a 1. \u5b9a\u4e49\u5916\u952e\u7ea6\u675f \u5728\u5b50\u8868\u4e2d\u4e3a\u5916\u952e\u5217\u6dfb\u52a0 FOREIGN KEY \u7ea6\u675f\uff0c\u8be5\u7ea6\u675f\u6307\u5b9a\u5b83\u4e0e\u4e3b\u8868\u7684\u54ea\u4e2a\u5217\u76f8\u5173\u8054\u3002 \u8bed\u6cd5\uff1a ALTER TABLE \u5b50\u8868 ADD FOREIGN KEY (`\u5916\u952e\u5217`) REFERENCES \u4e3b\u8868 (`\u4e3b\u8868\u5217`) \u767b\u5f55\u540e\u590d\u5236 2. \u6307\u5b9a\u66f4\u65b0\u548c\u5220\u9664\u64cd\u4f5c \u6307\u5b9a\u5f53\u4e3b\u8868\u7684\u76f8\u5173\u8bb0\u5f55\u88ab\u66f4\u65b0\u6216\u5220\u9664\u65f6\uff0c\u5b50\u8868\u8bb0\u5f55\u5e94\u5982\u4f55\u5904\u7406\u3002 ON UPDATE CASCADE\uff1a\u5f53\u4e3b\u8868\u8bb0\u5f55\u66f4\u65b0\u65f6\uff0c\u81ea\u52a8\u66f4\u65b0\u5b50\u8868\u4e2d\u76f8\u5173\u7684\u5916\u952e\u503c\u3002 ON DELETE CASCADE\uff1a\u5f53\u4e3b\u8868\u8bb0\u5f55\u5220\u9664\u65f6\uff0c\u81ea\u52a8\u5220\u9664\u5b50\u8868\u4e2d\u76f8\u5173\u7684\u5916\u952e\u8bb0\u5f55\u3002 ON UPDATE SET NULL\uff1a\u5f53\u4e3b\u8868\u8bb0\u5f55\u66f4\u65b0\u65f6\uff0c\u5c06\u5b50\u8868\u4e2d\u76f8\u5173\u7684\u5916\u952e\u503c\u8bbe\u7f6e\u4e3a NULL\u3002 ON DELETE SET NULL\uff1a\u5f53\u4e3b\u8868\u8bb0\u5f55\u5220\u9664\u65f6\uff0c\u5c06\u5b50\u8868\u4e2d\u76f8\u5173\u7684\u5916\u952e\u503c\u8bbe\u7f6e\u4e3a NULL\u3002 3. \u7ef4\u62a4\u6570\u636e\u5b8c\u6574\u6027 \u6dfb\u52a0\u4e86\u5916\u952e\u7ea6\u675f\u540e\uff0cMySQL \u5c06\u5f3a\u5236\u6267\u884c\u6570\u636e\u5b8c\u6574\u6027\uff0c\u786e\u4fdd\u5b50\u8868\u4e2d\u7684\u8bb0\u5f55\u59cb\u7ec8\u4e0e\u4e3b\u8868\u4e2d\u7684\u76f8\u5173\u8bb0\u5f55\u5173\u8054\u3002\u5982\u679c\u8fdd\u53cd\u8be5\u7ea6\u675f\uff0c\u5c06\u89e6\u53d1\u9519\u8bef\u3002 \u793a\u4f8b\uff1a \u5047\u8bbe\u6211\u4eec\u6709\u4e24\u4e2a\u8868\uff1a \u8ba2\u5355\u8868\uff0c\u5305\u542b\u8ba2\u5355\u4fe1\u606f\u3002 [&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-5157","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5157","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=5157"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5157\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=5157"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=5157"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=5157"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}