{"id":6220,"date":"2024-11-14T12:29:35","date_gmt":"2024-11-14T04:29:35","guid":{"rendered":"https:\/\/fwq.ai\/blog\/6220\/"},"modified":"2024-11-14T12:29:35","modified_gmt":"2024-11-14T04:29:35","slug":"mysql%e5%a6%82%e4%bd%95%e5%88%9b%e5%bb%ba%e5%a4%96%e9%94%ae%e7%ba%a6%e6%9d%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/6220\/","title":{"rendered":"mysql\u5982\u4f55\u521b\u5efa\u5916\u952e\u7ea6\u675f"},"content":{"rendered":"<blockquote><p>\n  \u5916\u952e\u7ea6\u675f\u5f3a\u5236\u8868\u4e2d\u5217\u4e0e\u53e6\u4e00\u8868\u7684\u4e3b\u952e\u76f8\u5173\u8054\uff0c\u4ee5\u786e\u4fdd\u6570\u636e\u5b8c\u6574\u6027\u548c\u4e00\u81f4\u6027\u3002\u521b\u5efa\u5916\u952e\u7ea6\u675f\u7684\u8bed\u6cd5\u4e3a\uff1aalter table add foreign key () references ()\u3002\u5916\u952e\u7ea6\u675f\u7684\u4f18\u70b9\u5305\u62ec\uff1a\u6570\u636e\u5b8c\u6574\u6027\u3001\u6570\u636e\u4e00\u81f4\u6027\u7ef4\u62a4\u548c\u67e5\u8be2\u6027\u80fd\u63d0\u9ad8\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202406\/15\/2024061514153093589.jpg\" class=\"aligncenter\" title=\"mysql\u5982\u4f55\u521b\u5efa\u5916\u952e\u7ea6\u675f\u63d2\u56fe\" alt=\"mysql\u5982\u4f55\u521b\u5efa\u5916\u952e\u7ea6\u675f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5728 MySQL \u4e2d\u521b\u5efa\u5916\u952e\u7ea6\u675f<\/strong><\/p>\n<p><strong>\u4ec0\u4e48\u662f\u5916\u952e\u7ea6\u675f\uff1f<\/strong><\/p>\n<p>\u5916\u952e\u7ea6\u675f\u662f\u4e00\u79cd\u6570\u636e\u5e93\u7ea6\u675f\uff0c\u5b83\u5f3a\u5236\u8868\u4e2d\u7684\u5217\u4e0e\u53e6\u4e00\u5f20\u8868\u4e2d\u7684\u4e3b\u952e\u5217\u76f8\u5173\u8054\u3002\u5b83\u6709\u52a9\u4e8e\u786e\u4fdd\u6570\u636e\u7684\u5b8c\u6574\u6027\u548c\u4e00\u81f4\u6027\u3002<\/p>\n<p><strong>\u5982\u4f55\u521b\u5efa\u5916\u952e\u7ea6\u675f\uff1f<\/strong><\/p>\n<p>\u8981\u5728 MySQL \u4e2d\u521b\u5efa\u5916\u952e\u7ea6\u675f\uff0c\u8bf7\u4f7f\u7528\u4ee5\u4e0b\u8bed\u6cd5\uff1a<\/p>\n<pre>ALTER TABLE &lt;child_table&gt; ADD FOREIGN KEY (&lt;child_column&gt;) REFERENCES &lt;parent_table&gt;(&lt;parent_column&gt;)<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5176\u4e2d\uff1a<\/p>\n<ul>\n<li> \u662f\u8981\u4e3a\u5176\u521b\u5efa\u5916\u952e\u7ea6\u675f\u7684\u8868\u3002 <\/li>\n<li> \u662f \u4e2d\u7684\u5916\u952e\u5217\u3002 <\/li>\n<li> \u662f\u5305\u542b\u4e3b\u952e\u5217\u7684\u8868\uff0c\u7528\u4e8e\u53c2\u7167\u3002 <\/li>\n<li> \u662f \u4e2d\u7684\u4e3b\u952e\u5217\u3002 <\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<p>\u5047\u8bbe\u6211\u4eec\u6709\u4e24\u4e2a\u8868\uff1aCustomers \u548c Orders\u3002Customers \u8868\u5305\u542b\u5ba2\u6237\u4fe1\u606f\uff0c\u800c Orders \u8868\u5305\u542b\u5ba2\u6237\u4e0b\u7684\u8ba2\u5355\u3002<\/p>\n<p>\u8981\u5728 Orders \u8868\u4e2d\u521b\u5efa\u5916\u952e\u7ea6\u675f\uff0c\u5c06 customer_id \u5217\u4e0e Customers \u8868\u4e2d\u7684 id \u5217\u5173\u8054\uff0c\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<pre>ALTER TABLE Orders ADD FOREIGN KEY (customer_id) REFERENCES Customers(id)<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u5916\u952e\u7ea6\u675f\u7684\u597d\u5904\uff1a<\/strong><\/p>\n<ul>\n<li>\u786e\u4fdd\u6570\u636e\u5b8c\u6574\u6027\uff1a\u901a\u8fc7\u5f3a\u5236\u6570\u636e\u5728\u76f8\u5173\u8868\u4e2d\u4fdd\u6301\u4e00\u81f4\uff0c\u5916\u952e\u7ea6\u675f\u9632\u6b62\u65e0\u6548\u6216\u4e0d\u4e00\u81f4\u7684\u6570\u636e\u63d2\u5165\u3002<\/li>\n<li>\u7ef4\u62a4\u6570\u636e\u4e00\u81f4\u6027\uff1a\u5f53\u76f8\u5173\u8868\u4e2d\u7684\u6570\u636e\u66f4\u65b0\u65f6\uff0c\u5916\u952e\u7ea6\u675f\u4f1a\u81ea\u52a8\u7ea7\u8054\u66f4\u65b0\u6216\u5220\u9664\u6570\u636e\uff0c\u4ee5\u4fdd\u6301\u4e00\u81f4\u6027\u3002<\/li>\n<li>\u63d0\u9ad8\u67e5\u8be2\u6027\u80fd\uff1a\u901a\u8fc7\u5efa\u7acb\u7d22\u5f15\uff0c\u5916\u952e\u7ea6\u675f\u53ef\u4ee5\u63d0\u9ad8\u5bf9\u76f8\u5173\u8868\u8fdb\u884c\u8fde\u63a5\u67e5\u8be2\u7684\u6027\u80fd\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u521b\u5efa\u5916\u952e\u7ea6\u675f\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\u7ea6\u675f\u5f3a\u5236\u8868\u4e2d\u5217\u4e0e\u53e6\u4e00\u8868\u7684\u4e3b\u952e\u76f8\u5173\u8054\uff0c\u4ee5\u786e\u4fdd\u6570\u636e\u5b8c\u6574\u6027\u548c\u4e00\u81f4\u6027\u3002\u521b\u5efa\u5916\u952e\u7ea6\u675f\u7684\u8bed\u6cd5\u4e3a\uff1aalter table add foreign key () references ()\u3002\u5916\u952e\u7ea6\u675f\u7684\u4f18\u70b9\u5305\u62ec\uff1a\u6570\u636e\u5b8c\u6574\u6027\u3001\u6570\u636e\u4e00\u81f4\u6027\u7ef4\u62a4\u548c\u67e5\u8be2\u6027\u80fd\u63d0\u9ad8\u3002 \u5728 MySQL \u4e2d\u521b\u5efa\u5916\u952e\u7ea6\u675f \u4ec0\u4e48\u662f\u5916\u952e\u7ea6\u675f\uff1f \u5916\u952e\u7ea6\u675f\u662f\u4e00\u79cd\u6570\u636e\u5e93\u7ea6\u675f\uff0c\u5b83\u5f3a\u5236\u8868\u4e2d\u7684\u5217\u4e0e\u53e6\u4e00\u5f20\u8868\u4e2d\u7684\u4e3b\u952e\u5217\u76f8\u5173\u8054\u3002\u5b83\u6709\u52a9\u4e8e\u786e\u4fdd\u6570\u636e\u7684\u5b8c\u6574\u6027\u548c\u4e00\u81f4\u6027\u3002 \u5982\u4f55\u521b\u5efa\u5916\u952e\u7ea6\u675f\uff1f \u8981\u5728 MySQL \u4e2d\u521b\u5efa\u5916\u952e\u7ea6\u675f\uff0c\u8bf7\u4f7f\u7528\u4ee5\u4e0b\u8bed\u6cd5\uff1a ALTER TABLE &lt;child_table&gt; ADD FOREIGN KEY (&lt;child_column&gt;) REFERENCES &lt;parent_table&gt;(&lt;parent_column&gt;) \u767b\u5f55\u540e\u590d\u5236 \u5176\u4e2d\uff1a \u662f\u8981\u4e3a\u5176\u521b\u5efa\u5916\u952e\u7ea6\u675f\u7684\u8868\u3002 \u662f \u4e2d\u7684\u5916\u952e\u5217\u3002 \u662f\u5305\u542b\u4e3b\u952e\u5217\u7684\u8868\uff0c\u7528\u4e8e\u53c2\u7167\u3002 \u662f \u4e2d\u7684\u4e3b\u952e\u5217\u3002 \u793a\u4f8b\uff1a \u5047\u8bbe\u6211\u4eec\u6709\u4e24\u4e2a\u8868\uff1aCustomers \u548c Orders\u3002Customers \u8868\u5305\u542b\u5ba2\u6237\u4fe1\u606f\uff0c\u800c Orders \u8868\u5305\u542b\u5ba2\u6237\u4e0b\u7684\u8ba2\u5355\u3002 \u8981\u5728 Orders \u8868\u4e2d\u521b\u5efa\u5916\u952e\u7ea6\u675f\uff0c\u5c06 customer_id \u5217\u4e0e Customers \u8868\u4e2d\u7684 id \u5217\u5173\u8054\uff0c\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\uff1a ALTER TABLE Orders ADD FOREIGN [&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-6220","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6220","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=6220"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6220\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=6220"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=6220"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=6220"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}