{"id":6298,"date":"2024-11-14T10:39:32","date_gmt":"2024-11-14T02:39:32","guid":{"rendered":"https:\/\/fwq.ai\/blog\/6298\/"},"modified":"2024-11-14T10:39:32","modified_gmt":"2024-11-14T02:39:32","slug":"sql%e4%b8%adfk%e6%98%af%e4%bb%80%e4%b9%88%e6%84%8f%e6%80%9d","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/6298\/","title":{"rendered":"sql\u4e2dfk\u662f\u4ec0\u4e48\u610f\u601d"},"content":{"rendered":"<blockquote><p>\n  sql \u4e2d\u7684 fk \u662f\u5916\u952e\u7ea6\u675f\uff0c\u7528\u4e8e\u7ef4\u62a4\u4e24\u4e2a\u8868\u4e4b\u95f4\u7684\u6570\u636e\u5b8c\u6574\u6027\u548c\u4e00\u81f4\u6027\uff0c\u901a\u8fc7\u5efa\u7acb\u903b\u8f91\u5173\u7cfb\u786e\u4fdd\u5b50\u8868\u4e2d\u7684\u6570\u636e\u5f15\u7528\u4e86\u4e3b\u8868\u4e2d\u7684\u6709\u6548\u6570\u636e\uff0c\u5e76\u9632\u6b62\u5220\u9664\u4e3b\u8868\u4e2d\u5f15\u7528\u7684\u6570\u636e\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202407\/11\/2024071110123772008.jpg\" class=\"aligncenter\" title=\"sql\u4e2dfk\u662f\u4ec0\u4e48\u610f\u601d\u63d2\u56fe\" alt=\"sql\u4e2dfk\u662f\u4ec0\u4e48\u610f\u601d\u63d2\u56fe\" \/><\/p>\n<p><strong>\u4ec0\u4e48\u662f SQL \u4e2d\u7684 FK\uff1f<\/strong><\/p>\n<p>FK \u662f\u5916\u952e\u7ea6\u675f\u7684\u7f29\u5199\u3002\u5916\u952e\u7ea6\u675f\u662f\u5173\u7cfb\u578b\u6570\u636e\u5e93\u4e2d\u7528\u4e8e\u7ef4\u62a4\u6570\u636e\u5b8c\u6574\u6027\u548c\u4e00\u81f4\u6027\u7684\u6570\u636e\u5e93\u5bf9\u8c61\u3002<\/p>\n<p><strong>\u5916\u952e\u7ea6\u675f\u7684\u4f5c\u7528<\/strong><\/p>\n<p>\u5916\u952e\u7ea6\u675f\u5728\u4e24\u4e2a\u8868\u4e4b\u95f4\u5efa\u7acb\u903b\u8f91\u5173\u7cfb\uff0c\u786e\u4fdd\u4e00\u4e2a\u8868\u4e2d\u7684\u6570\u636e\u4e0e\u53e6\u4e00\u4e2a\u8868\u4e2d\u7684\u6570\u636e\u76f8\u5173\u8054\u3002\u5b83\u6267\u884c\u4ee5\u4e0b\u64cd\u4f5c\uff1a<\/p>\n<ul>\n<li> <strong>\u6570\u636e\u5b8c\u6574\u6027\uff1a<\/strong>\u786e\u4fdd\u5b50\u8868\u4e2d\u7684\u6570\u636e\u5f15\u7528\u4e86\u4e3b\u8868\u4e2d\u7684\u6709\u6548\u6570\u636e\u3002<\/li>\n<li> <strong>\u53c2\u7167\u5b8c\u6574\u6027\uff1a<\/strong>\u963b\u6b62\u4ece\u5b50\u8868\u4e2d\u5220\u9664\u4e3b\u8868\u4e2d\u5f15\u7528\u7684\u6570\u636e\u3002<\/li>\n<li> <strong>\u7ea7\u8054\u64cd\u4f5c\uff1a<\/strong>\u5f53\u4e3b\u8868\u4e2d\u7684\u6570\u636e\u53d1\u751f\u53d8\u5316\u65f6\uff0c\u4f1a\u81ea\u52a8\u89e6\u53d1\u5b50\u8868\u4e2d\u7684\u76f8\u5e94\u64cd\u4f5c\uff08\u5982\u7ea7\u8054\u66f4\u65b0\u6216\u7ea7\u8054\u5220\u9664\uff09\u3002<\/li>\n<\/ul>\n<p><strong>\u5916\u952e\u7ea6\u675f\u7684\u8bed\u6cd5<\/strong><\/p>\n<p>\u5728 SQL \u4e2d\uff0c\u5916\u952e\u7ea6\u675f\u7684\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>ALTER TABLE [\u5b50\u8868\u540d\u79f0]\nADD FOREIGN KEY ([\u5b50\u8868\u5217\u540d\u79f0])\nREFERENCES [\u4e3b\u8868\u540d\u79f0] ([\u4e3b\u8868\u5217\u540d\u79f0])<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u5916\u952e\u7ea6\u675f\u7684\u793a\u4f8b<\/strong><\/p>\n<p>\u5047\u8bbe\u6211\u4eec\u6709\u4ee5\u4e0b\u4e24\u4e2a\u8868\uff1a<\/p>\n<ul>\n<li>Customers \u8868\u5305\u542b\u5ba2\u6237\u4fe1\u606f\uff0c\u5176\u4e2d customer_id \u662f\u4e3b\u952e\u3002<\/li>\n<li>Orders \u8868\u5305\u542b\u8ba2\u5355\u4fe1\u606f\uff0c\u5176\u4e2d customer_id \u662f\u5916\u952e\uff0c\u5b83\u5f15\u7528 Customers \u8868\u4e2d\u7684 customer_id\u3002<\/li>\n<\/ul>\n<p>\u4e3a\u4e86\u521b\u5efa\u5916\u952e\u7ea6\u675f\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b SQL \u8bed\u53e5\uff1a<\/p>\n<pre>ALTER TABLE Orders\nADD FOREIGN KEY (customer_id)\nREFERENCES Customers (customer_id)<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u8fd9\u4e2a\u5916\u952e\u7ea6\u675f\u786e\u4fdd Orders \u8868\u4e2d\u7684\u6240\u6709 customer_id \u90fd\u5bf9\u5e94\u4e8e Customers \u8868\u4e2d\u7684\u6709\u6548\u5ba2\u6237\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fsql\u4e2dfk\u662f\u4ec0\u4e48\u610f\u601d\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>sql \u4e2d\u7684 fk \u662f\u5916\u952e\u7ea6\u675f\uff0c\u7528\u4e8e\u7ef4\u62a4\u4e24\u4e2a\u8868\u4e4b\u95f4\u7684\u6570\u636e\u5b8c\u6574\u6027\u548c\u4e00\u81f4\u6027\uff0c\u901a\u8fc7\u5efa\u7acb\u903b\u8f91\u5173\u7cfb\u786e\u4fdd\u5b50\u8868\u4e2d\u7684\u6570\u636e\u5f15\u7528\u4e86\u4e3b\u8868\u4e2d\u7684\u6709\u6548\u6570\u636e\uff0c\u5e76\u9632\u6b62\u5220\u9664\u4e3b\u8868\u4e2d\u5f15\u7528\u7684\u6570\u636e\u3002 \u4ec0\u4e48\u662f SQL \u4e2d\u7684 FK\uff1f FK \u662f\u5916\u952e\u7ea6\u675f\u7684\u7f29\u5199\u3002\u5916\u952e\u7ea6\u675f\u662f\u5173\u7cfb\u578b\u6570\u636e\u5e93\u4e2d\u7528\u4e8e\u7ef4\u62a4\u6570\u636e\u5b8c\u6574\u6027\u548c\u4e00\u81f4\u6027\u7684\u6570\u636e\u5e93\u5bf9\u8c61\u3002 \u5916\u952e\u7ea6\u675f\u7684\u4f5c\u7528 \u5916\u952e\u7ea6\u675f\u5728\u4e24\u4e2a\u8868\u4e4b\u95f4\u5efa\u7acb\u903b\u8f91\u5173\u7cfb\uff0c\u786e\u4fdd\u4e00\u4e2a\u8868\u4e2d\u7684\u6570\u636e\u4e0e\u53e6\u4e00\u4e2a\u8868\u4e2d\u7684\u6570\u636e\u76f8\u5173\u8054\u3002\u5b83\u6267\u884c\u4ee5\u4e0b\u64cd\u4f5c\uff1a \u6570\u636e\u5b8c\u6574\u6027\uff1a\u786e\u4fdd\u5b50\u8868\u4e2d\u7684\u6570\u636e\u5f15\u7528\u4e86\u4e3b\u8868\u4e2d\u7684\u6709\u6548\u6570\u636e\u3002 \u53c2\u7167\u5b8c\u6574\u6027\uff1a\u963b\u6b62\u4ece\u5b50\u8868\u4e2d\u5220\u9664\u4e3b\u8868\u4e2d\u5f15\u7528\u7684\u6570\u636e\u3002 \u7ea7\u8054\u64cd\u4f5c\uff1a\u5f53\u4e3b\u8868\u4e2d\u7684\u6570\u636e\u53d1\u751f\u53d8\u5316\u65f6\uff0c\u4f1a\u81ea\u52a8\u89e6\u53d1\u5b50\u8868\u4e2d\u7684\u76f8\u5e94\u64cd\u4f5c\uff08\u5982\u7ea7\u8054\u66f4\u65b0\u6216\u7ea7\u8054\u5220\u9664\uff09\u3002 \u5916\u952e\u7ea6\u675f\u7684\u8bed\u6cd5 \u5728 SQL \u4e2d\uff0c\u5916\u952e\u7ea6\u675f\u7684\u8bed\u6cd5\u5982\u4e0b\uff1a ALTER TABLE [\u5b50\u8868\u540d\u79f0] ADD FOREIGN KEY ([\u5b50\u8868\u5217\u540d\u79f0]) REFERENCES [\u4e3b\u8868\u540d\u79f0] ([\u4e3b\u8868\u5217\u540d\u79f0]) \u767b\u5f55\u540e\u590d\u5236 \u5916\u952e\u7ea6\u675f\u7684\u793a\u4f8b \u5047\u8bbe\u6211\u4eec\u6709\u4ee5\u4e0b\u4e24\u4e2a\u8868\uff1a Customers \u8868\u5305\u542b\u5ba2\u6237\u4fe1\u606f\uff0c\u5176\u4e2d customer_id \u662f\u4e3b\u952e\u3002 Orders \u8868\u5305\u542b\u8ba2\u5355\u4fe1\u606f\uff0c\u5176\u4e2d customer_id \u662f\u5916\u952e\uff0c\u5b83\u5f15\u7528 Customers \u8868\u4e2d\u7684 customer_id\u3002 \u4e3a\u4e86\u521b\u5efa\u5916\u952e\u7ea6\u675f\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b SQL \u8bed\u53e5\uff1a ALTER TABLE Orders ADD FOREIGN KEY (customer_id) REFERENCES Customers [&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-6298","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6298","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=6298"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6298\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=6298"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=6298"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=6298"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}