{"id":6375,"date":"2024-11-14T10:37:32","date_gmt":"2024-11-14T02:37:32","guid":{"rendered":"https:\/\/fwq.ai\/blog\/6375\/"},"modified":"2024-11-14T10:37:32","modified_gmt":"2024-11-14T02:37:32","slug":"mysql%e5%a6%82%e4%bd%95%e5%88%9b%e5%bb%ba%e5%a4%96%e9%94%ae","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/6375\/","title":{"rendered":"mysql\u5982\u4f55\u521b\u5efa\u5916\u952e"},"content":{"rendered":"<blockquote><p>\n  mysql\u4e2d\u521b\u5efa\u5916\u952e\u7684\u6b65\u9aa4\uff1a\u521b\u5efa\u7236\u8868\u548c\u5b50\u8868\uff0c\u7236\u8868\u5305\u542b\u4e3b\u952e\u5217\uff0c\u5b50\u8868\u5305\u542b\u5916\u952e\u5217\u3002\u5728\u5b50\u8868\u4e2d\u4f7f\u7528foreign key\u7ea6\u675f\u5b9a\u4e49\u5916\u952e\u5217\uff0c\u8be5\u7ea6\u675f\u5c06\u5f15\u7528\u7236\u8868\u7684\u4e3b\u952e\u5217\u3002\u901a\u8fc7\u6307\u5b9a\u7ea7\u8054\u64cd\u4f5c\u5b9a\u4e49\u5bf9\u5b50\u8868\u4e2d\u76f8\u5173\u8bb0\u5f55\u7684\u5904\u7406\u65b9\u5f0f\uff08\u53ef\u9009\uff09\u3002\u4f7f\u7528alter table\u8bed\u53e5\u6dfb\u52a0\u5916\u952e\u7ea6\u675f\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202408\/01\/2024080106130828441.jpg\" class=\"aligncenter\" title=\"mysql\u5982\u4f55\u521b\u5efa\u5916\u952e\u63d2\u56fe\" alt=\"mysql\u5982\u4f55\u521b\u5efa\u5916\u952e\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5728 MySQL \u4e2d\u521b\u5efa\u5916\u952e<\/strong><\/p>\n<p>MySQL \u4e2d\u7684\u5916\u952e\u662f\u7528\u6765\u5f3a\u5236\u6267\u884c\u6570\u636e\u5b8c\u6574\u6027\u7684\u7ea6\u675f\uff0c\u786e\u4fdd\u76f8\u5173\u8868\u4e4b\u95f4\u7684\u4e00\u81f4\u6027\u3002\u521b\u5efa\u5916\u952e\u7684\u8fc7\u7a0b\u5982\u4e0b\uff1a<\/p>\n<p><strong>1. \u521b\u5efa\u6570\u636e\u8868<\/strong><\/p>\n<p>\u9996\u5148\uff0c\u521b\u5efa\u4e24\u4e2a\u6570\u636e\u8868\uff0c\u4e00\u4e2a\u4f5c\u4e3a\u7236\u8868\uff08\u5305\u542b\u4e3b\u952e\u5217\uff09\uff0c\u53e6\u4e00\u4e2a\u4f5c\u4e3a\u5b50\u8868\uff08\u5305\u542b\u5916\u952e\u5217\uff09\u3002<\/p>\n<p><strong>2. \u5b9a\u4e49\u5916\u952e\u5217<\/strong><\/p>\n<p>\u5728\u5b50\u8868\u4e2d\uff0c\u4f7f\u7528 FOREIGN KEY \u7ea6\u675f\u5b9a\u4e49\u5916\u952e\u5217\u3002\u8be5\u7ea6\u675f\u5c06\u5f15\u7528\u7236\u8868\u7684\u4e3b\u952e\u5217\u3002\u7ea6\u675f\u7684\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>FOREIGN KEY (\u5b50\u8868\u5916\u952e\u5217) REFERENCES \u7236\u8868(\u7236\u8868\u4e3b\u952e\u5217)<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3. \u6307\u5b9a\u7ea7\u8054\u64cd\u4f5c\uff08\u53ef\u9009\uff09<\/strong><\/p>\n<p>\u5f53\u7236\u8868\u4e2d\u7684\u6570\u636e\u66f4\u65b0\u6216\u5220\u9664\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u6307\u5b9a\u7ea7\u8054\u64cd\u4f5c\u6765\u5b9a\u4e49\u5bf9\u5b50\u8868\u4e2d\u76f8\u5173\u8bb0\u5f55\u7684\u5904\u7406\u65b9\u5f0f\u3002\u5e38\u7528\u7684\u7ea7\u8054\u64cd\u4f5c\u6709\uff1a<\/p>\n<ul>\n<li> <strong>CASCADE:<\/strong> \u5f53\u7236\u8868\u8bb0\u5f55\u66f4\u65b0\u6216\u5220\u9664\u65f6\uff0c\u81ea\u52a8\u66f4\u65b0\u6216\u5220\u9664\u5b50\u8868\u4e2d\u76f8\u5173\u7684\u8bb0\u5f55\u3002<\/li>\n<li> <strong>RESTRICT:<\/strong> \u5f53\u7236\u8868\u8bb0\u5f55\u66f4\u65b0\u6216\u5220\u9664\u65f6\uff0c\u7981\u6b62\u5bf9\u5b50\u8868\u4e2d\u76f8\u5173\u7684\u8bb0\u5f55\u8fdb\u884c\u64cd\u4f5c\u3002<\/li>\n<li> <strong>SET NULL:<\/strong> \u5f53\u7236\u8868\u8bb0\u5f55\u66f4\u65b0\u6216\u5220\u9664\u65f6\uff0c\u5c06\u5b50\u8868\u4e2d\u76f8\u5173\u8bb0\u5f55\u7684\u5916\u952e\u5217\u8bbe\u7f6e\u4e3a NULL\u3002<\/li>\n<\/ul>\n<p><strong>4. \u6dfb\u52a0\u7ea6\u675f<\/strong><\/p>\n<p>\u5b9a\u4e49\u597d\u5916\u952e\u5217\u540e\uff0c\u4f7f\u7528 ALTER TABLE \u8bed\u53e5\u6dfb\u52a0\u5916\u952e\u7ea6\u675f\u3002\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>ALTER TABLE \u5b50\u8868 ADD FOREIGN KEY (\u5916\u952e\u5217) REFERENCES \u7236\u8868(\u4e3b\u952e\u5217)<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u4f8b\u5982\uff1a<\/strong><\/p>\n<p>\u5047\u8bbe\u6709\u7236\u8868 Customers \u548c\u5b50\u8868 Orders\uff0c\u5176\u4e2d Customers \u8868\u7684\u4e3b\u952e\u5217\u662f customer_id\uff0cOrders \u8868\u5916\u952e\u5217\u662f customer_id\u3002\u521b\u5efa\u5916\u952e\u7ea6\u675f\u7684\u8bed\u53e5\u5982\u4e0b\uff1a<\/p>\n<pre>ALTER TABLE Orders ADD FOREIGN KEY (customer_id) REFERENCES Customers(customer_id)<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u901a\u8fc7\u521b\u5efa\u5916\u952e\uff0c\u53ef\u4ee5\u786e\u4fdd Orders \u8868\u4e2d\u7684 customer_id \u59cb\u7ec8\u5f15\u7528 Customers \u8868\u4e2d\u7684\u6709\u6548 customer_id\uff0c\u4ece\u800c\u4fdd\u8bc1\u6570\u636e\u5b8c\u6574\u6027\u548c\u4e00\u81f4\u6027\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u521b\u5efa\u5916\u952e\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\u4e2d\u521b\u5efa\u5916\u952e\u7684\u6b65\u9aa4\uff1a\u521b\u5efa\u7236\u8868\u548c\u5b50\u8868\uff0c\u7236\u8868\u5305\u542b\u4e3b\u952e\u5217\uff0c\u5b50\u8868\u5305\u542b\u5916\u952e\u5217\u3002\u5728\u5b50\u8868\u4e2d\u4f7f\u7528foreign key\u7ea6\u675f\u5b9a\u4e49\u5916\u952e\u5217\uff0c\u8be5\u7ea6\u675f\u5c06\u5f15\u7528\u7236\u8868\u7684\u4e3b\u952e\u5217\u3002\u901a\u8fc7\u6307\u5b9a\u7ea7\u8054\u64cd\u4f5c\u5b9a\u4e49\u5bf9\u5b50\u8868\u4e2d\u76f8\u5173\u8bb0\u5f55\u7684\u5904\u7406\u65b9\u5f0f\uff08\u53ef\u9009\uff09\u3002\u4f7f\u7528alter table\u8bed\u53e5\u6dfb\u52a0\u5916\u952e\u7ea6\u675f\u3002 \u5982\u4f55\u5728 MySQL \u4e2d\u521b\u5efa\u5916\u952e MySQL \u4e2d\u7684\u5916\u952e\u662f\u7528\u6765\u5f3a\u5236\u6267\u884c\u6570\u636e\u5b8c\u6574\u6027\u7684\u7ea6\u675f\uff0c\u786e\u4fdd\u76f8\u5173\u8868\u4e4b\u95f4\u7684\u4e00\u81f4\u6027\u3002\u521b\u5efa\u5916\u952e\u7684\u8fc7\u7a0b\u5982\u4e0b\uff1a 1. \u521b\u5efa\u6570\u636e\u8868 \u9996\u5148\uff0c\u521b\u5efa\u4e24\u4e2a\u6570\u636e\u8868\uff0c\u4e00\u4e2a\u4f5c\u4e3a\u7236\u8868\uff08\u5305\u542b\u4e3b\u952e\u5217\uff09\uff0c\u53e6\u4e00\u4e2a\u4f5c\u4e3a\u5b50\u8868\uff08\u5305\u542b\u5916\u952e\u5217\uff09\u3002 2. \u5b9a\u4e49\u5916\u952e\u5217 \u5728\u5b50\u8868\u4e2d\uff0c\u4f7f\u7528 FOREIGN KEY \u7ea6\u675f\u5b9a\u4e49\u5916\u952e\u5217\u3002\u8be5\u7ea6\u675f\u5c06\u5f15\u7528\u7236\u8868\u7684\u4e3b\u952e\u5217\u3002\u7ea6\u675f\u7684\u8bed\u6cd5\u5982\u4e0b\uff1a FOREIGN KEY (\u5b50\u8868\u5916\u952e\u5217) REFERENCES \u7236\u8868(\u7236\u8868\u4e3b\u952e\u5217) \u767b\u5f55\u540e\u590d\u5236 3. \u6307\u5b9a\u7ea7\u8054\u64cd\u4f5c\uff08\u53ef\u9009\uff09 \u5f53\u7236\u8868\u4e2d\u7684\u6570\u636e\u66f4\u65b0\u6216\u5220\u9664\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u6307\u5b9a\u7ea7\u8054\u64cd\u4f5c\u6765\u5b9a\u4e49\u5bf9\u5b50\u8868\u4e2d\u76f8\u5173\u8bb0\u5f55\u7684\u5904\u7406\u65b9\u5f0f\u3002\u5e38\u7528\u7684\u7ea7\u8054\u64cd\u4f5c\u6709\uff1a CASCADE: \u5f53\u7236\u8868\u8bb0\u5f55\u66f4\u65b0\u6216\u5220\u9664\u65f6\uff0c\u81ea\u52a8\u66f4\u65b0\u6216\u5220\u9664\u5b50\u8868\u4e2d\u76f8\u5173\u7684\u8bb0\u5f55\u3002 RESTRICT: \u5f53\u7236\u8868\u8bb0\u5f55\u66f4\u65b0\u6216\u5220\u9664\u65f6\uff0c\u7981\u6b62\u5bf9\u5b50\u8868\u4e2d\u76f8\u5173\u7684\u8bb0\u5f55\u8fdb\u884c\u64cd\u4f5c\u3002 SET NULL: \u5f53\u7236\u8868\u8bb0\u5f55\u66f4\u65b0\u6216\u5220\u9664\u65f6\uff0c\u5c06\u5b50\u8868\u4e2d\u76f8\u5173\u8bb0\u5f55\u7684\u5916\u952e\u5217\u8bbe\u7f6e\u4e3a NULL\u3002 4. \u6dfb\u52a0\u7ea6\u675f \u5b9a\u4e49\u597d\u5916\u952e\u5217\u540e\uff0c\u4f7f\u7528 ALTER TABLE \u8bed\u53e5\u6dfb\u52a0\u5916\u952e\u7ea6\u675f\u3002\u8bed\u6cd5\u5982\u4e0b\uff1a ALTER TABLE \u5b50\u8868 ADD FOREIGN KEY (\u5916\u952e\u5217) REFERENCES \u7236\u8868(\u4e3b\u952e\u5217) \u767b\u5f55\u540e\u590d\u5236 \u4f8b\u5982\uff1a \u5047\u8bbe\u6709\u7236\u8868 Customers \u548c\u5b50\u8868 Orders\uff0c\u5176\u4e2d [&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-6375","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6375","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=6375"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6375\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=6375"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=6375"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=6375"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}