{"id":6338,"date":"2024-11-14T13:31:54","date_gmt":"2024-11-14T05:31:54","guid":{"rendered":"https:\/\/fwq.ai\/blog\/6338\/"},"modified":"2024-11-14T13:31:54","modified_gmt":"2024-11-14T05:31:54","slug":"mysql%e5%88%9b%e5%bb%ba%e5%a4%96%e9%94%ae%e7%9a%84sql%e8%af%ad%e5%8f%a5","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/6338\/","title":{"rendered":"mysql\u521b\u5efa\u5916\u952e\u7684sql\u8bed\u53e5"},"content":{"rendered":"<blockquote><p>\n  \u521b\u5efa mysql \u5916\u952e\u7684 sql \u8bed\u53e5\u4e3a\uff1aalter table child_table add foreign key (foreign_key_column) references parent_table (parent_key_column) [on delete action] [on update action]; \u5176\u4e2d\uff0cchild_table \u4e3a\u8981\u521b\u5efa\u5916\u952e\u7684\u8868\uff0cforeign_key_column \u4e3a\u4e0e\u7236\u8868\u4e3b\u952e\u5173\u8054\u7684\u5217\uff0cparent_table \u4e3a\u5305\u542b\u5916\u952e\u6240\u5f15\u7528\u7684\u4e3b\u5173\u952e\u5b57\u7684\u8868\uff0cpa\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202407\/31\/2024073122580363860.jpg\" class=\"aligncenter\" title=\"mysql\u521b\u5efa\u5916\u952e\u7684sql\u8bed\u53e5\u63d2\u56fe\" alt=\"mysql\u521b\u5efa\u5916\u952e\u7684sql\u8bed\u53e5\u63d2\u56fe\" \/><\/p>\n<p><strong>\u521b\u5efa MySQL \u5916\u952e\u7684 SQL \u8bed\u53e5<\/strong><\/p>\n<p><strong>\u5916\u952e\u4ecb\u7ecd<\/strong><\/p>\n<p>\u5916\u952e\u662f\u4e00\u79cd\u6570\u636e\u5e93\u7ea6\u675f\uff0c\u5b83\u5c06\u4e00\u4e2a\u8868\u4e2d\u7684\u5217\u94fe\u63a5\u5230\u53e6\u4e00\u4e2a\u8868\u4e2d\u7684\u4e3b\u5173\u952e\u5b57\u3002\u5b83\u786e\u4fdd\u4e86\u6570\u636e\u7684\u5b8c\u6574\u6027\uff0c\u5e76\u9632\u6b62\u521b\u5efa\u5f15\u7528\u4e0d\u5b58\u5728\u7684\u8bb0\u5f55\u3002<\/p>\n<p><strong>\u521b\u5efa\u5916\u952e\u8bed\u53e5<\/strong><\/p>\n<p>\u8bed\u6cd5\uff1a<\/p>\n<pre>ALTER TABLE child_table\nADD FOREIGN KEY (foreign_key_column)\nREFERENCES parent_table (parent_key_column)\n[ON DELETE action]\n[ON UPDATE action];<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u53c2\u6570\u8bf4\u660e\uff1a<\/strong><\/p>\n<ul>\n<li> <strong>child_table\uff1a<\/strong>\u8981\u521b\u5efa\u5916\u952e\u7684\u8868\u3002<\/li>\n<li> <strong>foreign_key_column\uff1a<\/strong>child_table \u4e2d\u4e0e parent_table \u4e3b\u952e\u5173\u8054\u7684\u5217\u3002<\/li>\n<li> <strong>parent_table\uff1a<\/strong>\u5305\u542b\u5916\u952e\u6240\u5f15\u7528\u7684\u4e3b\u5173\u952e\u5b57\u7684\u8868\u3002<\/li>\n<li> <strong>parent_key_column\uff1a<\/strong>parent_table \u4e2d\u7684\u4e3b\u5173\u952e\u5b57\u5217\u3002<\/li>\n<li> <strong>ON DELETE action\uff1a<\/strong>\u5f53 parent_table \u4e2d\u5f15\u7528\u7684\u8bb0\u5f55\u88ab\u5220\u9664\u65f6\u7684\u64cd\u4f5c\uff08\u53ef\u9009\uff09\u3002\u53ef\u4ee5\u662f CASCADE\uff08\u7ea7\u8054\u5220\u9664\uff09\u3001SET NULL\uff08\u5c06\u5916\u952e\u5217\u8bbe\u7f6e\u4e3a NULL\uff09\u6216 RESTRICT\uff08\u4e0d\u5141\u8bb8\u5220\u9664\uff09\u3002<\/li>\n<li> <strong>ON UPDATE action\uff1a<\/strong>\u5f53 parent_table \u4e2d\u5f15\u7528\u7684\u8bb0\u5f55\u88ab\u66f4\u65b0\u65f6\u7684\u64cd\u4f5c\uff08\u53ef\u9009\uff09\u3002\u53ef\u4ee5\u662f CASCADE\uff08\u7ea7\u8054\u66f4\u65b0\uff09\u3001SET NULL \u6216 RESTRICT\u3002<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b<\/strong><\/p>\n<p>\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a orders \u7684\u8868\u5e76\u4e3a\u5176\u521b\u5efa\u5916\u952e\uff0c\u8be5\u5916\u952e\u5f15\u7528 customers \u8868\u4e2d\u7684 customer_id \u4e3b\u952e\uff1a<\/p>\n<pre>ALTER TABLE orders\nADD FOREIGN KEY (customer_id)\nREFERENCES customers (customer_id)\nON DELETE CASCADE;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u6b64\u5916\u952e\u786e\u4fdd orders \u8868\u4e2d\u7684 customer_id \u5217\u59cb\u7ec8\u5f15\u7528 customers \u8868\u4e2d\u5b58\u5728\u7684\u5ba2\u6237\u3002\u5982\u679c\u5c1d\u8bd5\u5220\u9664 customers \u8868\u4e2d\u5f15\u7528\u7684\u5ba2\u6237\uff0c\u5219\u4e0e\u8be5\u5ba2\u6237\u5173\u8054\u7684 orders \u8868\u4e2d\u7684\u8bb0\u5f55\u5c06\u88ab\u7ea7\u8054\u5220\u9664\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u521b\u5efa\u5916\u952e\u7684\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>\u521b\u5efa mysql \u5916\u952e\u7684 sql \u8bed\u53e5\u4e3a\uff1aalter table child_table add foreign key (foreign_key_column) references parent_table (parent_key_column) [on delete action] [on update action]; \u5176\u4e2d\uff0cchild_table \u4e3a\u8981\u521b\u5efa\u5916\u952e\u7684\u8868\uff0cforeign_key_column \u4e3a\u4e0e\u7236\u8868\u4e3b\u952e\u5173\u8054\u7684\u5217\uff0cparent_table \u4e3a\u5305\u542b\u5916\u952e\u6240\u5f15\u7528\u7684\u4e3b\u5173\u952e\u5b57\u7684\u8868\uff0cpa \u521b\u5efa MySQL \u5916\u952e\u7684 SQL \u8bed\u53e5 \u5916\u952e\u4ecb\u7ecd \u5916\u952e\u662f\u4e00\u79cd\u6570\u636e\u5e93\u7ea6\u675f\uff0c\u5b83\u5c06\u4e00\u4e2a\u8868\u4e2d\u7684\u5217\u94fe\u63a5\u5230\u53e6\u4e00\u4e2a\u8868\u4e2d\u7684\u4e3b\u5173\u952e\u5b57\u3002\u5b83\u786e\u4fdd\u4e86\u6570\u636e\u7684\u5b8c\u6574\u6027\uff0c\u5e76\u9632\u6b62\u521b\u5efa\u5f15\u7528\u4e0d\u5b58\u5728\u7684\u8bb0\u5f55\u3002 \u521b\u5efa\u5916\u952e\u8bed\u53e5 \u8bed\u6cd5\uff1a ALTER TABLE child_table ADD FOREIGN KEY (foreign_key_column) REFERENCES parent_table (parent_key_column) [ON DELETE action] [ON UPDATE action]; \u767b\u5f55\u540e\u590d\u5236 \u53c2\u6570\u8bf4\u660e\uff1a child_table\uff1a\u8981\u521b\u5efa\u5916\u952e\u7684\u8868\u3002 foreign_key_column\uff1achild_table \u4e2d\u4e0e parent_table [&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-6338","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6338","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=6338"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6338\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=6338"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=6338"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=6338"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}