{"id":6445,"date":"2024-11-14T10:47:39","date_gmt":"2024-11-14T02:47:39","guid":{"rendered":"https:\/\/fwq.ai\/blog\/6445\/"},"modified":"2024-11-14T10:47:39","modified_gmt":"2024-11-14T02:47:39","slug":"mysql%e6%95%b0%e6%8d%ae%e5%ba%93%e5%a4%96%e9%94%ae%e6%80%8e%e4%b9%88%e8%ae%be%e7%bd%ae","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/6445\/","title":{"rendered":"mysql\u6570\u636e\u5e93\u5916\u952e\u600e\u4e48\u8bbe\u7f6e"},"content":{"rendered":"<blockquote><p>\n  \u5916\u952e\u5728 mysql \u4e2d\u7528\u4e8e\u5728\u8868\u4e4b\u95f4\u5efa\u7acb\u5173\u7cfb\u7ea6\u675f\uff0c\u786e\u4fdd\u5f15\u7528\u6570\u636e\u7684\u5b8c\u6574\u6027\u3002\u6b65\u9aa4\u5305\u62ec\uff1a1. \u521b\u5efa\u5916\u952e\uff0c\u6307\u5b9a\u5916\u952e\u5217\u548c\u6240\u5f15\u7528\u7684\u7236\u8868\u548c\u7236\u952e\uff1b2. \u6307\u5b9a\u7ea6\u675f\u7c7b\u578b\uff08\u9650\u5236\u3001\u7ea7\u8054\u3001\u7f6e\u7a7a\uff09\u3002\u8bbe\u7f6e\u5916\u952e\u53ef\u4ee5\u7ef4\u62a4\u6570\u636e\u5b8c\u6574\u6027\uff0c\u9632\u6b62\u5f15\u7528\u4e0d\u5b58\u5728\u7684\u8bb0\u5f55\uff0c\u7b80\u5316\u6570\u636e\u64cd\u7eb5\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202408\/05\/2024080507335046046.jpg\" class=\"aligncenter\" title=\"mysql\u6570\u636e\u5e93\u5916\u952e\u600e\u4e48\u8bbe\u7f6e\u63d2\u56fe\" alt=\"mysql\u6570\u636e\u5e93\u5916\u952e\u600e\u4e48\u8bbe\u7f6e\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5728 MySQL \u6570\u636e\u5e93\u4e2d\u8bbe\u7f6e\u5916\u952e<\/strong><\/p>\n<p>\u5916\u952e\u662f\u4e00\u79cd\u6570\u636e\u5e93\u7ea6\u675f\uff0c\u5b83\u7528\u4e8e\u5f3a\u5236\u8868\u4e4b\u95f4\u7684\u5173\u7cfb\u5b8c\u6574\u6027\u3002\u5b83\u901a\u8fc7\u786e\u4fdd\u5b50\u8868\u4e2d\u7684\u8bb0\u5f55\u5f15\u7528\u7236\u8868\u4e2d\u7684\u73b0\u6709\u8bb0\u5f55\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002\u5728 MySQL \u6570\u636e\u5e93\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u8bbe\u7f6e\u5916\u952e\uff1a<\/p>\n<p><strong>1. \u521b\u5efa\u5916\u952e<\/strong><\/p>\n<p>\u5728\u5916\u952e\u5217\u4e2d\u6dfb\u52a0 FOREIGN KEY \u7ea6\u675f\u3002\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>ALTER TABLE \u5b50\u8868 ADD FOREIGN KEY (\u5916\u952e\u5217\u540d\u79f0) REFERENCES \u7236\u8868 (\u7236\u952e\u5217\u540d\u79f0);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u6307\u5b9a\u53c2\u8003<\/strong><\/p>\n<p>REFERENCES \u5b50\u53e5\u6307\u5b9a\u4e86\u5916\u952e\u5217\u6240\u5f15\u7528\u7684\u7236\u8868\u548c\u7236\u952e\u5217\u3002\u7236\u952e\u5217\u5fc5\u987b\u662f\u7236\u8868\u4e2d\u7684\u552f\u4e00\u952e\u6216\u4e3b\u952e\u3002<\/p>\n<p><strong>3. \u7ea6\u675f\u7c7b\u578b<\/strong><\/p>\n<p>FOREIGN KEY \u7ea6\u675f\u53ef\u4ee5\u6709\u4ee5\u4e0b\u7c7b\u578b\uff1a<\/p>\n<ul>\n<li> <strong>RESTRICT<\/strong>\uff1a\u5982\u679c\u5f15\u7528\u7236\u8868\u7684\u8bb0\u5f55\u88ab\u5220\u9664\uff0c\u5219\u5916\u952e\u7ea6\u675f\u5c06\u963b\u6b62\u5b50\u8868\u4e2d\u7684\u8bb0\u5f55\u88ab\u5220\u9664\u3002<\/li>\n<li> <strong>CASCADE<\/strong>\uff1a\u5982\u679c\u5f15\u7528\u7236\u8868\u7684\u8bb0\u5f55\u88ab\u5220\u9664\uff0c\u5219\u5916\u952e\u7ea6\u675f\u5c06\u81ea\u52a8\u5220\u9664\u5b50\u8868\u4e2d\u7684\u76f8\u5173\u8bb0\u5f55\u3002<\/li>\n<li> <strong>SET NULL<\/strong>\uff1a\u5982\u679c\u5f15\u7528\u7236\u8868\u7684\u8bb0\u5f55\u88ab\u5220\u9664\uff0c\u5219\u5916\u952e\u7ea6\u675f\u5c06\u81ea\u52a8\u5c06\u5b50\u8868\u4e2d\u7684\u5916\u952e\u5217\u8bbe\u7f6e\u4e3a NULL\u3002<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b<\/strong><\/p>\n<p>\u5047\u8bbe\u5b58\u5728\u4e24\u4e2a\u8868\uff1a\u8ba2\u5355 \u548c \u4ea7\u54c1\u3002\u8ba2\u5355 \u8868\u5305\u542b\u8ba2\u5355\u4fe1\u606f\uff0c\u4ea7\u54c1 \u8868\u5305\u542b\u4ea7\u54c1\u4fe1\u606f\u3002\u8981\u786e\u4fdd \u8ba2\u5355 \u8868\u4e2d\u7684\u4ea7\u54c1 ID \u5217\u59cb\u7ec8\u5f15\u7528 \u4ea7\u54c1 \u8868\u4e2d\u7684\u6709\u6548\u4ea7\u54c1\uff0c\u53ef\u4ee5\u8bbe\u7f6e\u4ee5\u4e0b\u5916\u952e\uff1a<\/p>\n<pre>ALTER TABLE \u8ba2\u5355 ADD FOREIGN KEY (\u4ea7\u54c1_ID) REFERENCES \u4ea7\u54c1 (\u4ea7\u54c1_ID);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u4f18\u70b9<\/strong><\/p>\n<p>\u8bbe\u7f6e\u5916\u952e\u7684\u597d\u5904\u5305\u62ec\uff1a<\/p>\n<ul>\n<li>\u7ef4\u62a4\u6570\u636e\u5b8c\u6574\u6027<\/li>\n<li>\u9632\u6b62\u521b\u5efa\u6307\u5411\u4e0d\u5b58\u5728\u8bb0\u5f55\u7684\u5f15\u7528<\/li>\n<li>\u7b80\u5316\u6570\u636e\u64cd\u7eb5<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662f\u6570\u636e\u5e93\u5916\u952e\u600e\u4e48\u8bbe\u7f6e\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>\u5916\u952e\u5728 mysql \u4e2d\u7528\u4e8e\u5728\u8868\u4e4b\u95f4\u5efa\u7acb\u5173\u7cfb\u7ea6\u675f\uff0c\u786e\u4fdd\u5f15\u7528\u6570\u636e\u7684\u5b8c\u6574\u6027\u3002\u6b65\u9aa4\u5305\u62ec\uff1a1. \u521b\u5efa\u5916\u952e\uff0c\u6307\u5b9a\u5916\u952e\u5217\u548c\u6240\u5f15\u7528\u7684\u7236\u8868\u548c\u7236\u952e\uff1b2. \u6307\u5b9a\u7ea6\u675f\u7c7b\u578b\uff08\u9650\u5236\u3001\u7ea7\u8054\u3001\u7f6e\u7a7a\uff09\u3002\u8bbe\u7f6e\u5916\u952e\u53ef\u4ee5\u7ef4\u62a4\u6570\u636e\u5b8c\u6574\u6027\uff0c\u9632\u6b62\u5f15\u7528\u4e0d\u5b58\u5728\u7684\u8bb0\u5f55\uff0c\u7b80\u5316\u6570\u636e\u64cd\u7eb5\u3002 \u5982\u4f55\u5728 MySQL \u6570\u636e\u5e93\u4e2d\u8bbe\u7f6e\u5916\u952e \u5916\u952e\u662f\u4e00\u79cd\u6570\u636e\u5e93\u7ea6\u675f\uff0c\u5b83\u7528\u4e8e\u5f3a\u5236\u8868\u4e4b\u95f4\u7684\u5173\u7cfb\u5b8c\u6574\u6027\u3002\u5b83\u901a\u8fc7\u786e\u4fdd\u5b50\u8868\u4e2d\u7684\u8bb0\u5f55\u5f15\u7528\u7236\u8868\u4e2d\u7684\u73b0\u6709\u8bb0\u5f55\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002\u5728 MySQL \u6570\u636e\u5e93\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u8bbe\u7f6e\u5916\u952e\uff1a 1. \u521b\u5efa\u5916\u952e \u5728\u5916\u952e\u5217\u4e2d\u6dfb\u52a0 FOREIGN KEY \u7ea6\u675f\u3002\u8bed\u6cd5\u5982\u4e0b\uff1a ALTER TABLE \u5b50\u8868 ADD FOREIGN KEY (\u5916\u952e\u5217\u540d\u79f0) REFERENCES \u7236\u8868 (\u7236\u952e\u5217\u540d\u79f0); \u767b\u5f55\u540e\u590d\u5236 2. \u6307\u5b9a\u53c2\u8003 REFERENCES \u5b50\u53e5\u6307\u5b9a\u4e86\u5916\u952e\u5217\u6240\u5f15\u7528\u7684\u7236\u8868\u548c\u7236\u952e\u5217\u3002\u7236\u952e\u5217\u5fc5\u987b\u662f\u7236\u8868\u4e2d\u7684\u552f\u4e00\u952e\u6216\u4e3b\u952e\u3002 3. \u7ea6\u675f\u7c7b\u578b FOREIGN KEY \u7ea6\u675f\u53ef\u4ee5\u6709\u4ee5\u4e0b\u7c7b\u578b\uff1a RESTRICT\uff1a\u5982\u679c\u5f15\u7528\u7236\u8868\u7684\u8bb0\u5f55\u88ab\u5220\u9664\uff0c\u5219\u5916\u952e\u7ea6\u675f\u5c06\u963b\u6b62\u5b50\u8868\u4e2d\u7684\u8bb0\u5f55\u88ab\u5220\u9664\u3002 CASCADE\uff1a\u5982\u679c\u5f15\u7528\u7236\u8868\u7684\u8bb0\u5f55\u88ab\u5220\u9664\uff0c\u5219\u5916\u952e\u7ea6\u675f\u5c06\u81ea\u52a8\u5220\u9664\u5b50\u8868\u4e2d\u7684\u76f8\u5173\u8bb0\u5f55\u3002 SET NULL\uff1a\u5982\u679c\u5f15\u7528\u7236\u8868\u7684\u8bb0\u5f55\u88ab\u5220\u9664\uff0c\u5219\u5916\u952e\u7ea6\u675f\u5c06\u81ea\u52a8\u5c06\u5b50\u8868\u4e2d\u7684\u5916\u952e\u5217\u8bbe\u7f6e\u4e3a NULL\u3002 \u793a\u4f8b \u5047\u8bbe\u5b58\u5728\u4e24\u4e2a\u8868\uff1a\u8ba2\u5355 \u548c \u4ea7\u54c1\u3002\u8ba2\u5355 \u8868\u5305\u542b\u8ba2\u5355\u4fe1\u606f\uff0c\u4ea7\u54c1 \u8868\u5305\u542b\u4ea7\u54c1\u4fe1\u606f\u3002\u8981\u786e\u4fdd \u8ba2\u5355 \u8868\u4e2d\u7684\u4ea7\u54c1 ID \u5217\u59cb\u7ec8\u5f15\u7528 \u4ea7\u54c1 \u8868\u4e2d\u7684\u6709\u6548\u4ea7\u54c1\uff0c\u53ef\u4ee5\u8bbe\u7f6e\u4ee5\u4e0b\u5916\u952e\uff1a ALTER [&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-6445","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6445","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=6445"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6445\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=6445"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=6445"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=6445"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}