{"id":6238,"date":"2024-11-14T08:45:05","date_gmt":"2024-11-14T00:45:05","guid":{"rendered":"https:\/\/fwq.ai\/blog\/6238\/"},"modified":"2024-11-14T08:45:05","modified_gmt":"2024-11-14T00:45:05","slug":"mysql%e5%a6%82%e4%bd%95%e6%b7%bb%e5%8a%a0%e5%a4%96%e9%94%ae","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/6238\/","title":{"rendered":"mysql\u5982\u4f55\u6dfb\u52a0\u5916\u952e"},"content":{"rendered":"<blockquote><p>\n  \u5728 mysql \u4e2d\u6dfb\u52a0\u5916\u952e\u53ef\u786e\u4fdd\u6570\u636e\u5b8c\u6574\u6027\uff0c\u901a\u8fc7\u5c06\u4e00\u4e2a\u8868\u4e2d\u7684\u5217\u4e0e\u53e6\u4e00\u4e2a\u8868\u4e2d\u7684\u4e3b\u952e\u5173\u8054\u5b9e\u73b0\u3002\u5728\u8868\u4e2d\u6dfb\u52a0\u5916\u952e\u7684\u8bed\u6cd5\u4e3a\uff1aalter table table_name add foreign key (column_name) references referenced_table_name (referenced_column_name)\u3002\u5728\u63d2\u5165\u65b0\u8bb0\u5f55\u65f6\uff0c\u5916\u952e\u5217\u7684\u503c\u5fc5\u987b\u7b49\u4e8e\u5df2\u5b58\u5728\u884c\u7684\u4e3b\u952e\u503c\u3002\u5916\u952e\u7ea6\u675f\u5305\u62ec on delete \u548c on update\uff0c\u53ef\u6307\u5b9a\u5916\u952e\u5f15\u7528\u7684\u884c\u88ab\u5220\u9664\u6216\u66f4\u65b0\u65f6\u91c7\u53d6\u7684\u64cd\u4f5c\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202406\/15\/2024061515452635589.jpg\" class=\"aligncenter\" title=\"mysql\u5982\u4f55\u6dfb\u52a0\u5916\u952e\u63d2\u56fe\" alt=\"mysql\u5982\u4f55\u6dfb\u52a0\u5916\u952e\u63d2\u56fe\" \/><\/p>\n<p><strong>MySQL \u4e2d\u6dfb\u52a0\u5916\u952e<\/strong><\/p>\n<p>MySQL \u4e2d\u6dfb\u52a0\u5916\u952e\u53ef\u4ee5\u786e\u4fdd\u6570\u636e\u5b8c\u6574\u6027\u548c\u4e00\u81f4\u6027\u3002\u5916\u952e\u901a\u8fc7\u5c06\u4e00\u4e2a\u8868\u4e2d\u7684\u5217\u4e0e\u53e6\u4e00\u4e2a\u8868\u4e2d\u7684\u4e3b\u952e\u5173\u8054\u6765\u5b9e\u73b0\u3002<\/p>\n<p><strong>\u5982\u4f55\u6dfb\u52a0\u5916\u952e<\/strong><\/p>\n<p>\u4f7f\u7528\u4ee5\u4e0b\u8bed\u6cd5\u5728 MySQL \u4e2d\u6dfb\u52a0\u5916\u952e\uff1a<\/p>\n<pre>ALTER TABLE table_name\nADD FOREIGN KEY (column_name) REFERENCES referenced_table_name (referenced_column_name);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5176\u4e2d\uff1a<\/p>\n<ul>\n<li>table_name \u662f\u8981\u6dfb\u52a0\u5916\u952e\u7684\u8868\u3002<\/li>\n<li>column_name \u662f\u8981\u4f5c\u4e3a\u5916\u952e\u7684\u5217\u3002<\/li>\n<li>referenced_table_name \u662f\u5305\u542b\u5916\u952e\u5217\u6240\u5f15\u7528\u7684\u4e3b\u952e\u7684\u8868\u3002<\/li>\n<li>referenced_column_name \u662f\u4f5c\u4e3a\u5916\u952e\u5f15\u7528\u7684\u4e3b\u952e\u5217\u3002<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b<\/strong><\/p>\n<p>\u5047\u8bbe\u6211\u4eec\u6709\u4e24\u4e2a\u8868\uff1a<\/p>\n<ul>\n<li>orders\uff1a\u5305\u542b\u8ba2\u5355\u4fe1\u606f\uff0c\u5b83\u5177\u6709\u5b57\u6bb5 order_id \u4f5c\u4e3a\u4e3b\u952e\u3002<\/li>\n<li>products\uff1a\u5305\u542b\u4ea7\u54c1\u4fe1\u606f\uff0c\u5b83\u5177\u6709\u5b57\u6bb5 product_id \u4f5c\u4e3a\u4e3b\u952e\u3002<\/li>\n<\/ul>\n<p>\u8981\u5728\u5916\u952e order_id \u548c products \u8868\u7684\u4e3b\u952e product_id \u4e4b\u95f4\u5efa\u7acb\u5173\u8054\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u67e5\u8be2\uff1a<\/p>\n<pre>ALTER TABLE orders\nADD FOREIGN KEY (order_id) REFERENCES products (product_id);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u73b0\u5728\uff0c\u5f53\u5411 orders \u8868\u4e2d\u63d2\u5165\u65b0\u8bb0\u5f55\u65f6\uff0corder_id \u5217\u7684\u503c\u5fc5\u987b\u7b49\u4e8e products \u8868\u4e2d\u73b0\u6709\u884c\u7684 product_id \u503c\u3002<\/p>\n<p><strong>\u5916\u952e\u7ea6\u675f<\/strong><\/p>\n<p>\u6dfb\u52a0\u5916\u952e\u65f6\uff0c\u53ef\u4ee5\u6307\u5b9a\u4ee5\u4e0b\u7ea6\u675f\uff1a<\/p>\n<ul>\n<li>ON DELETE\uff1a\u6307\u5b9a\u5f53\u5f15\u7528\u7684\u884c\u5728 referenced_table_name \u8868\u4e2d\u88ab\u5220\u9664\u65f6\u5e94\u91c7\u53d6\u7684\u64cd\u4f5c\u3002\u9009\u9879\u5305\u62ec RESTRICT\u3001CASCADE \u548c SET NULL\u3002<\/li>\n<li>ON UPDATE\uff1a\u6307\u5b9a\u5f53\u5f15\u7528\u7684\u884c\u5728 referenced_table_name \u8868\u4e2d\u88ab\u66f4\u65b0\u65f6\u5e94\u91c7\u53d6\u7684\u64cd\u4f5c\u3002\u9009\u9879\u5305\u62ec RESTRICT\u3001CASCADE \u548c SET NULL\u3002<\/li>\n<\/ul>\n<p>\u4f8b\u5982\uff0c\u8981\u6307\u5b9a\u5728\u5220\u9664 products \u8868\u4e2d\u7684\u884c\u65f6\u7ea7\u8054\u5220\u9664 orders \u8868\u4e2d\u7684\u5f15\u7528\u884c\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u8bed\u6cd5\uff1a<\/p>\n<pre>ALTER TABLE orders\nADD FOREIGN KEY (order_id) REFERENCES products (product_id)\nON DELETE CASCADE;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u6dfb\u52a0\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>\u5728 mysql \u4e2d\u6dfb\u52a0\u5916\u952e\u53ef\u786e\u4fdd\u6570\u636e\u5b8c\u6574\u6027\uff0c\u901a\u8fc7\u5c06\u4e00\u4e2a\u8868\u4e2d\u7684\u5217\u4e0e\u53e6\u4e00\u4e2a\u8868\u4e2d\u7684\u4e3b\u952e\u5173\u8054\u5b9e\u73b0\u3002\u5728\u8868\u4e2d\u6dfb\u52a0\u5916\u952e\u7684\u8bed\u6cd5\u4e3a\uff1aalter table table_name add foreign key (column_name) references referenced_table_name (referenced_column_name)\u3002\u5728\u63d2\u5165\u65b0\u8bb0\u5f55\u65f6\uff0c\u5916\u952e\u5217\u7684\u503c\u5fc5\u987b\u7b49\u4e8e\u5df2\u5b58\u5728\u884c\u7684\u4e3b\u952e\u503c\u3002\u5916\u952e\u7ea6\u675f\u5305\u62ec on delete \u548c on update\uff0c\u53ef\u6307\u5b9a\u5916\u952e\u5f15\u7528\u7684\u884c\u88ab\u5220\u9664\u6216\u66f4\u65b0\u65f6\u91c7\u53d6\u7684\u64cd\u4f5c\u3002 MySQL \u4e2d\u6dfb\u52a0\u5916\u952e MySQL \u4e2d\u6dfb\u52a0\u5916\u952e\u53ef\u4ee5\u786e\u4fdd\u6570\u636e\u5b8c\u6574\u6027\u548c\u4e00\u81f4\u6027\u3002\u5916\u952e\u901a\u8fc7\u5c06\u4e00\u4e2a\u8868\u4e2d\u7684\u5217\u4e0e\u53e6\u4e00\u4e2a\u8868\u4e2d\u7684\u4e3b\u952e\u5173\u8054\u6765\u5b9e\u73b0\u3002 \u5982\u4f55\u6dfb\u52a0\u5916\u952e \u4f7f\u7528\u4ee5\u4e0b\u8bed\u6cd5\u5728 MySQL \u4e2d\u6dfb\u52a0\u5916\u952e\uff1a ALTER TABLE table_name ADD FOREIGN KEY (column_name) REFERENCES referenced_table_name (referenced_column_name); \u767b\u5f55\u540e\u590d\u5236 \u5176\u4e2d\uff1a table_name \u662f\u8981\u6dfb\u52a0\u5916\u952e\u7684\u8868\u3002 column_name \u662f\u8981\u4f5c\u4e3a\u5916\u952e\u7684\u5217\u3002 referenced_table_name \u662f\u5305\u542b\u5916\u952e\u5217\u6240\u5f15\u7528\u7684\u4e3b\u952e\u7684\u8868\u3002 referenced_column_name \u662f\u4f5c\u4e3a\u5916\u952e\u5f15\u7528\u7684\u4e3b\u952e\u5217\u3002 \u793a\u4f8b \u5047\u8bbe\u6211\u4eec\u6709\u4e24\u4e2a\u8868\uff1a orders\uff1a\u5305\u542b\u8ba2\u5355\u4fe1\u606f\uff0c\u5b83\u5177\u6709\u5b57\u6bb5 order_id \u4f5c\u4e3a\u4e3b\u952e\u3002 products\uff1a\u5305\u542b\u4ea7\u54c1\u4fe1\u606f\uff0c\u5b83\u5177\u6709\u5b57\u6bb5 product_id \u4f5c\u4e3a\u4e3b\u952e\u3002 \u8981\u5728\u5916\u952e order_id [&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-6238","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6238","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=6238"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6238\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=6238"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=6238"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=6238"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}