{"id":6211,"date":"2024-11-14T09:32:27","date_gmt":"2024-11-14T01:32:27","guid":{"rendered":"https:\/\/fwq.ai\/blog\/6211\/"},"modified":"2024-11-14T09:32:27","modified_gmt":"2024-11-14T01:32:27","slug":"mysql%e5%a6%82%e4%bd%95%e5%bb%ba%e7%ab%8b%e5%a4%96%e9%94%ae","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/6211\/","title":{"rendered":"mysql\u5982\u4f55\u5efa\u7acb\u5916\u952e"},"content":{"rendered":"<blockquote><p>\n  \u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u5728 mysql \u4e2d\u5efa\u7acb\u5916\u952e\uff1a1. alter table [\u5b50\u8868\u540d] add constraint [\u5916\u952e\u540d\u79f0] foreign key ([\u5b50\u8868\u5916\u952e\u5217\u540d]) references [\u7236\u8868\u540d] ([\u7236\u8868\u4e3b\u952e\u5217\u540d])\uff1b2. \u8fd9\u6837\u505a\u53ef\u4ee5\u5f3a\u5236\u5b50\u8868\u4e2d\u7684\u5916\u952e\u503c\u4e0e\u7236\u8868\u4e2d\u7684\u4e3b\u952e\u503c\u4e00\u81f4\uff0c\u5e76\u652f\u6301 restrict\u3001cascade\u3001set null \u7b49\u7ea6\u675f\u7c7b\u578b\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202406\/15\/2024061513392863780.jpg\" class=\"aligncenter\" title=\"mysql\u5982\u4f55\u5efa\u7acb\u5916\u952e\u63d2\u56fe\" alt=\"mysql\u5982\u4f55\u5efa\u7acb\u5916\u952e\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5728 MySQL \u4e2d\u5efa\u7acb\u5916\u952e<\/strong><\/p>\n<p>\u5916\u952e\u662f\u4e00\u79cd\u5173\u7cfb\u6570\u636e\u5e93\u4e2d\u7528\u6765\u5efa\u7acb\u8868\u4e4b\u95f4\u5173\u8054\u7684\u7ea6\u675f\u3002\u5728 MySQL \u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u5efa\u7acb\u5916\u952e\uff1a<\/p>\n<p><strong>1. \u521b\u5efa\u5916\u952e<\/strong><\/p>\n<pre>ALTER TABLE [\u5b50\u8868\u540d] ADD CONSTRAINT [\u5916\u952e\u540d\u79f0] FOREIGN KEY ([\u5b50\u8868\u5916\u952e\u5217\u540d]) REFERENCES [\u7236\u8868\u540d] ([\u7236\u8868\u4e3b\u952e\u5217\u540d])<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u53c2\u6570\u8bf4\u660e<\/strong><\/p>\n<ul>\n<li> <strong>[\u5b50\u8868\u540d]<\/strong>\uff1a\u6307\u5b9a\u8981\u521b\u5efa\u5916\u952e\u7684\u5b50\u8868\u3002<\/li>\n<li> <strong>[\u5916\u952e\u540d\u79f0]<\/strong>\uff1a\u6307\u5b9a\u5916\u952e\u7684\u540d\u79f0\u3002<\/li>\n<li> <strong>[\u5b50\u8868\u5916\u952e\u5217\u540d]<\/strong>\uff1a\u6307\u5b9a\u5b50\u8868\u4e2d\u5f15\u7528\u7236\u8868\u4e3b\u952e\u7684\u5217\u3002<\/li>\n<li> <strong>[\u7236\u8868\u540d]<\/strong>\uff1a\u6307\u5b9a\u5916\u952e\u5f15\u7528\u7684\u7236\u8868\u3002<\/li>\n<li> <strong>[\u7236\u8868\u4e3b\u952e\u5217\u540d]<\/strong>\uff1a\u6307\u5b9a\u7236\u8868\u4e2d\u88ab\u5f15\u7528\u7684\u4e3b\u952e\u5217\u3002<\/li>\n<\/ul>\n<p><strong>3. \u793a\u4f8b<\/strong><\/p>\n<p>\u8003\u8651\u4ee5\u4e0b\u4e24\u4e2a\u8868\uff1a<\/p>\n<ul>\n<li>\u8ba2\u5355\u8868 (orders)\uff1a\u5305\u542b\u8ba2\u5355\u4fe1\u606f<\/li>\n<li>\u4ea7\u54c1\u8868 (products)\uff1a\u5305\u542b\u4ea7\u54c1\u4fe1\u606f<\/li>\n<\/ul>\n<p>\u8981\u5728 orders \u8868\u4e2d\u5efa\u7acb\u5f15\u7528 products \u8868 product_id \u5217\u7684\u5916\u952e\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u8bed\u53e5\uff1a<\/p>\n<pre>ALTER TABLE orders ADD CONSTRAINT FK_product FOREIGN KEY (product_id) REFERENCES products (product_id)<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>4. \u4f5c\u7528<\/strong><\/p>\n<p>\u5916\u952e\u5efa\u7acb\u540e\uff0c\u5c06\u5f3a\u5236\u5b50\u8868\u4e2d\u7684\u5916\u952e\u5217\u503c\u4e0e\u7236\u8868\u4e2d\u7684\u4e3b\u952e\u5217\u503c\u4fdd\u6301\u4e00\u81f4\u3002\u8fd9\u610f\u5473\u7740\uff1a<\/p>\n<ul>\n<li>\u5b50\u8868\u8bb0\u5f55\u7684\u5916\u952e\u503c\u5fc5\u987b\u5b58\u5728\u4e8e\u7236\u8868\u7684\u4e3b\u952e\u503c\u4e2d\u3002<\/li>\n<li>\u5982\u679c\u7236\u8868\u4e2d\u7684\u4e3b\u952e\u503c\u88ab\u5220\u9664\u6216\u4fee\u6539\uff0c\u5219\u76f8\u5e94\u7684\u5b50\u8868\u8bb0\u5f55\u4e5f\u4f1a\u88ab\u5220\u9664\u6216\u4fee\u6539\uff08\u53d6\u51b3\u4e8e\u5916\u952e\u7ea6\u675f\u7684\u7c7b\u578b\uff09\u3002<\/li>\n<\/ul>\n<p><strong>5. \u7ea6\u675f\u7c7b\u578b<\/strong><\/p>\n<p>MySQL \u652f\u6301\u4ee5\u4e0b\u5916\u952e\u7ea6\u675f\u7c7b\u578b\uff1a<\/p>\n<ul>\n<li> <strong>RESTRICT<\/strong>\uff1a\u4e0d\u5141\u8bb8\u5220\u9664\u6216\u4fee\u6539\u7236\u8868\u4e2d\u7684\u4e3b\u952e\u503c\uff0c\u5426\u5219\u4f1a\u5bfc\u81f4\u8fdd\u53cd\u5916\u952e\u7ea6\u675f\u3002<\/li>\n<li> <strong>CASCADE<\/strong>\uff1a\u5f53\u7236\u8868\u4e2d\u7684\u4e3b\u952e\u503c\u88ab\u5220\u9664\u6216\u4fee\u6539\u65f6\uff0c\u81ea\u52a8\u5220\u9664\u6216\u4fee\u6539\u5b50\u8868\u4e2d\u7684\u5916\u952e\u503c\u3002<\/li>\n<li> <strong>SET NULL<\/strong>\uff1a\u5f53\u7236\u8868\u4e2d\u7684\u4e3b\u952e\u503c\u88ab\u5220\u9664\u6216\u4fee\u6539\u65f6\uff0c\u5c06\u5b50\u8868\u4e2d\u7684\u5916\u952e\u503c\u8bbe\u7f6e\u4e3a NULL\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u5efa\u7acb\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>\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u5728 mysql \u4e2d\u5efa\u7acb\u5916\u952e\uff1a1. alter table [\u5b50\u8868\u540d] add constraint [\u5916\u952e\u540d\u79f0] foreign key ([\u5b50\u8868\u5916\u952e\u5217\u540d]) references [\u7236\u8868\u540d] ([\u7236\u8868\u4e3b\u952e\u5217\u540d])\uff1b2. \u8fd9\u6837\u505a\u53ef\u4ee5\u5f3a\u5236\u5b50\u8868\u4e2d\u7684\u5916\u952e\u503c\u4e0e\u7236\u8868\u4e2d\u7684\u4e3b\u952e\u503c\u4e00\u81f4\uff0c\u5e76\u652f\u6301 restrict\u3001cascade\u3001set null \u7b49\u7ea6\u675f\u7c7b\u578b\u3002 \u5982\u4f55\u5728 MySQL \u4e2d\u5efa\u7acb\u5916\u952e \u5916\u952e\u662f\u4e00\u79cd\u5173\u7cfb\u6570\u636e\u5e93\u4e2d\u7528\u6765\u5efa\u7acb\u8868\u4e4b\u95f4\u5173\u8054\u7684\u7ea6\u675f\u3002\u5728 MySQL \u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u5efa\u7acb\u5916\u952e\uff1a 1. \u521b\u5efa\u5916\u952e ALTER TABLE [\u5b50\u8868\u540d] ADD CONSTRAINT [\u5916\u952e\u540d\u79f0] FOREIGN KEY ([\u5b50\u8868\u5916\u952e\u5217\u540d]) REFERENCES [\u7236\u8868\u540d] ([\u7236\u8868\u4e3b\u952e\u5217\u540d]) \u767b\u5f55\u540e\u590d\u5236 2. \u53c2\u6570\u8bf4\u660e [\u5b50\u8868\u540d]\uff1a\u6307\u5b9a\u8981\u521b\u5efa\u5916\u952e\u7684\u5b50\u8868\u3002 [\u5916\u952e\u540d\u79f0]\uff1a\u6307\u5b9a\u5916\u952e\u7684\u540d\u79f0\u3002 [\u5b50\u8868\u5916\u952e\u5217\u540d]\uff1a\u6307\u5b9a\u5b50\u8868\u4e2d\u5f15\u7528\u7236\u8868\u4e3b\u952e\u7684\u5217\u3002 [\u7236\u8868\u540d]\uff1a\u6307\u5b9a\u5916\u952e\u5f15\u7528\u7684\u7236\u8868\u3002 [\u7236\u8868\u4e3b\u952e\u5217\u540d]\uff1a\u6307\u5b9a\u7236\u8868\u4e2d\u88ab\u5f15\u7528\u7684\u4e3b\u952e\u5217\u3002 3. \u793a\u4f8b \u8003\u8651\u4ee5\u4e0b\u4e24\u4e2a\u8868\uff1a \u8ba2\u5355\u8868 (orders)\uff1a\u5305\u542b\u8ba2\u5355\u4fe1\u606f \u4ea7\u54c1\u8868 (products)\uff1a\u5305\u542b\u4ea7\u54c1\u4fe1\u606f \u8981\u5728 [&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-6211","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6211","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=6211"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6211\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=6211"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=6211"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=6211"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}