{"id":5931,"date":"2024-11-14T09:18:32","date_gmt":"2024-11-14T01:18:32","guid":{"rendered":"https:\/\/fwq.ai\/blog\/5931\/"},"modified":"2024-11-14T09:18:32","modified_gmt":"2024-11-14T01:18:32","slug":"sql%e5%a4%96%e9%94%ae%e8%af%ad%e5%8f%a5%e6%80%8e%e4%b9%88%e5%86%99","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/5931\/","title":{"rendered":"sql\u5916\u952e\u8bed\u53e5\u600e\u4e48\u5199"},"content":{"rendered":"<blockquote><p>\n  \u5916\u952e\u662f\u6570\u636e\u5e93\u5173\u7cfb\uff0c\u7528\u4e8e\u5efa\u7acb\u4e24\u4e2a\u8868\u4e4b\u95f4\u7684\u5173\u8054\uff0c\u5f3a\u5236\u6570\u636e\u5b8c\u6574\u6027\u3002sql \u4e2d\u7684\u5916\u952e\u7ea6\u675f\u8bed\u6cd5\u4e3a\uff1aalter table child_table add foreign key (column_name) references parent_table(column_name)\u3002sql \u63d0\u4f9b\u7684\u5916\u952e\u7ea6\u675f\u7c7b\u578b\u5305\u62ec\uff1aon delete cascade\u3001on delete set null\u3001on delete set default\u3001on update cascade\u3001on update set null\u3001on\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202406\/03\/2024060322394069429.jpg\" class=\"aligncenter\" title=\"sql\u5916\u952e\u8bed\u53e5\u600e\u4e48\u5199\u63d2\u56fe\" alt=\"sql\u5916\u952e\u8bed\u53e5\u600e\u4e48\u5199\u63d2\u56fe\" \/><\/p>\n<p><strong>SQL \u5916\u952e\u8bed\u53e5<\/strong><\/p>\n<p><strong>\u4ec0\u4e48\u662f\u5916\u952e\uff1f<\/strong><\/p>\n<p>\u5916\u952e\u662f\u4e00\u79cd\u6570\u636e\u5e93\u5173\u7cfb\uff0c\u7528\u4e8e\u5728\u4e24\u4e2a\u8868\u4e4b\u95f4\u5efa\u7acb\u5173\u8054\u3002\u5b83\u901a\u8fc7\u5f15\u7528\u53e6\u4e00\u4e2a\u8868\u4e2d\u7684\u4e3b\u952e\u5217\u6765\u5f3a\u5236\u6570\u636e\u5b8c\u6574\u6027\u3002<\/p>\n<p><strong>\u5916\u952e\u8bed\u6cd5<\/strong><\/p>\n<p>\u5728 SQL \u4e2d\uff0c\u5916\u952e\u7ea6\u675f\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u8bed\u6cd5\u521b\u5efa\uff1a<\/p>\n<pre>ALTER TABLE child_table\nADD FOREIGN KEY (column_name)\nREFERENCES parent_table(column_name)<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5176\u4e2d\uff1a<\/p>\n<ul>\n<li>child_table \u662f\u5305\u542b\u5916\u952e\u5217\u7684\u8868\u3002<\/li>\n<li>column_name \u662f child_table \u4e2d\u7684\u5916\u952e\u5217\u3002<\/li>\n<li>parent_table \u662f\u5305\u542b\u88ab\u5f15\u7528\u7684\u4e3b\u952e\u5217\u7684\u8868\u3002<\/li>\n<li>column_name \u662f parent_table \u4e2d\u7684\u4e3b\u952e\u5217\u3002<\/li>\n<\/ul>\n<p><strong>\u5916\u952e\u7ea6\u675f\u7c7b\u578b<\/strong><\/p>\n<p>SQL \u63d0\u4f9b\u4ee5\u4e0b\u7c7b\u578b\u7684\u5916\u952e\u7ea6\u675f\uff1a<\/p>\n<ul>\n<li> <strong>ON DELETE CASCADE\uff1a<\/strong>\u5f53\u7236\u8868\u4e2d\u7684\u8bb0\u5f55\u88ab\u5220\u9664\u65f6\uff0c\u5c06\u81ea\u52a8\u5220\u9664\u5b50\u8868\u4e2d\u6240\u6709\u5f15\u7528\u8be5\u8bb0\u5f55\u7684\u8bb0\u5f55\u3002<\/li>\n<li> <strong>ON DELETE SET NULL\uff1a<\/strong>\u5f53\u7236\u8868\u4e2d\u7684\u8bb0\u5f55\u88ab\u5220\u9664\u65f6\uff0c\u5c06\u5b50\u8868\u4e2d\u5f15\u7528\u8be5\u8bb0\u5f55\u7684\u5916\u952e\u5217\u8bbe\u7f6e\u4e3a NULL\u3002<\/li>\n<li> <strong>ON DELETE SET DEFAULT\uff1a<\/strong>\u5f53\u7236\u8868\u4e2d\u7684\u8bb0\u5f55\u88ab\u5220\u9664\u65f6\uff0c\u5c06\u5b50\u8868\u4e2d\u5f15\u7528\u8be5\u8bb0\u5f55\u7684\u5916\u952e\u5217\u8bbe\u7f6e\u4e3a\u9ed8\u8ba4\u503c\u3002<\/li>\n<li> <strong>ON UPDATE CASCADE\uff1a<\/strong>\u5f53\u7236\u8868\u4e2d\u7684\u8bb0\u5f55\u88ab\u66f4\u65b0\u65f6\uff0c\u5c06\u81ea\u52a8\u66f4\u65b0\u5b50\u8868\u4e2d\u6240\u6709\u5f15\u7528\u8be5\u8bb0\u5f55\u7684\u8bb0\u5f55\u3002<\/li>\n<li> <strong>ON UPDATE SET NULL\uff1a<\/strong>\u5f53\u7236\u8868\u4e2d\u7684\u8bb0\u5f55\u88ab\u66f4\u65b0\u65f6\uff0c\u5c06\u5b50\u8868\u4e2d\u5f15\u7528\u8be5\u8bb0\u5f55\u7684\u5916\u952e\u5217\u8bbe\u7f6e\u4e3a NULL\u3002<\/li>\n<li> <strong>ON UPDATE SET DEFAULT\uff1a<\/strong>\u5f53\u7236\u8868\u4e2d\u7684\u8bb0\u5f55\u88ab\u66f4\u65b0\u65f6\uff0c\u5c06\u5b50\u8868\u4e2d\u5f15\u7528\u8be5\u8bb0\u5f55\u7684\u5916\u952e\u5217\u8bbe\u7f6e\u4e3a\u9ed8\u8ba4\u503c\u3002<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b<\/strong><\/p>\n<p>\u4ee5\u4e0b SQL \u8bed\u53e5\u5728 orders \u8868\u548c customers \u8868\u4e4b\u95f4\u521b\u5efa\u5916\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>\u8fd9\u610f\u5473\u7740 orders \u8868\u4e2d\u7684 customer_id \u5217\u5c06\u5f15\u7528 customers \u8868\u4e2d\u7684 customer_id \u4e3b\u952e\u3002\u5f53 customers \u8868\u4e2d\u7684\u4e00\u6761\u8bb0\u5f55\u88ab\u5220\u9664\u65f6\uff0corders \u8868\u4e2d\u6240\u6709\u5f15\u7528\u8be5\u8bb0\u5f55\u7684\u8bb0\u5f55\u4e5f\u5c06\u88ab\u5220\u9664\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fsql\u5916\u952e\u8bed\u53e5\u600e\u4e48\u5199\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\u662f\u6570\u636e\u5e93\u5173\u7cfb\uff0c\u7528\u4e8e\u5efa\u7acb\u4e24\u4e2a\u8868\u4e4b\u95f4\u7684\u5173\u8054\uff0c\u5f3a\u5236\u6570\u636e\u5b8c\u6574\u6027\u3002sql \u4e2d\u7684\u5916\u952e\u7ea6\u675f\u8bed\u6cd5\u4e3a\uff1aalter table child_table add foreign key (column_name) references parent_table(column_name)\u3002sql \u63d0\u4f9b\u7684\u5916\u952e\u7ea6\u675f\u7c7b\u578b\u5305\u62ec\uff1aon delete cascade\u3001on delete set null\u3001on delete set default\u3001on update cascade\u3001on update set null\u3001on SQL \u5916\u952e\u8bed\u53e5 \u4ec0\u4e48\u662f\u5916\u952e\uff1f \u5916\u952e\u662f\u4e00\u79cd\u6570\u636e\u5e93\u5173\u7cfb\uff0c\u7528\u4e8e\u5728\u4e24\u4e2a\u8868\u4e4b\u95f4\u5efa\u7acb\u5173\u8054\u3002\u5b83\u901a\u8fc7\u5f15\u7528\u53e6\u4e00\u4e2a\u8868\u4e2d\u7684\u4e3b\u952e\u5217\u6765\u5f3a\u5236\u6570\u636e\u5b8c\u6574\u6027\u3002 \u5916\u952e\u8bed\u6cd5 \u5728 SQL \u4e2d\uff0c\u5916\u952e\u7ea6\u675f\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u8bed\u6cd5\u521b\u5efa\uff1a ALTER TABLE child_table ADD FOREIGN KEY (column_name) REFERENCES parent_table(column_name) \u767b\u5f55\u540e\u590d\u5236 \u5176\u4e2d\uff1a child_table \u662f\u5305\u542b\u5916\u952e\u5217\u7684\u8868\u3002 column_name \u662f child_table \u4e2d\u7684\u5916\u952e\u5217\u3002 parent_table \u662f\u5305\u542b\u88ab\u5f15\u7528\u7684\u4e3b\u952e\u5217\u7684\u8868\u3002 column_name \u662f parent_table \u4e2d\u7684\u4e3b\u952e\u5217\u3002 [&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-5931","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5931","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=5931"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5931\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=5931"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=5931"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=5931"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}