{"id":5631,"date":"2024-11-14T13:56:58","date_gmt":"2024-11-14T05:56:58","guid":{"rendered":"https:\/\/fwq.ai\/blog\/5631\/"},"modified":"2024-11-14T13:56:58","modified_gmt":"2024-11-14T05:56:58","slug":"sql%e6%95%b0%e6%8d%ae%e5%ba%93%e6%80%8e%e4%b9%88%e8%ae%be%e7%bd%ae%e5%a4%96%e9%94%ae","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/5631\/","title":{"rendered":"sql\u6570\u636e\u5e93\u600e\u4e48\u8bbe\u7f6e\u5916\u952e"},"content":{"rendered":"<blockquote><p>\n  sql \u4e2d\u5916\u952e\u662f\u4e00\u79cd\u6570\u636e\u5e93\u7ea6\u675f\uff0c\u786e\u4fdd\u5b50\u8868\u8bb0\u5f55\u4e0e\u4e3b\u8868\u76f8\u5173\u8bb0\u5f55\u5173\u8054\u3002\u8981\u8bbe\u7f6e\u5916\u952e\uff0c\u9700\u8981\uff1a1. \u786e\u5b9a\u4e3b\u8868\u548c\u5b50\u8868\uff1b2. \u9009\u62e9\u76f8\u5e94\u5217\u4f5c\u4e3a\u7236\u5217\u548c\u5b50\u5217\uff1b3. \u4f7f\u7528 alter table \u8bed\u53e5\u521b\u5efa\u5916\u952e\u7ea6\u675f\uff0c\u6307\u5b9a\u5b50\u5217\u3001\u4e3b\u8868\u3001\u7236\u5217\u4ee5\u53ca\u9644\u52a0\u9009\u9879\uff08\u5982\u679c\u9700\u8981\uff09\u3002\u4f8b\u5982\uff0calter table \u8ba2\u5355 add foreign key (\u4ea7\u54c1 id) references \u4ea7\u54c1(\u4ea7\u54c1 id) on delete cascade on update cascade\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202405\/30\/2024053019215615746.jpg\" class=\"aligncenter\" title=\"sql\u6570\u636e\u5e93\u600e\u4e48\u8bbe\u7f6e\u5916\u952e\u63d2\u56fe\" alt=\"sql\u6570\u636e\u5e93\u600e\u4e48\u8bbe\u7f6e\u5916\u952e\u63d2\u56fe\" \/><\/p>\n<p><strong>SQL \u6570\u636e\u5e93\u4e2d\u8bbe\u7f6e\u5916\u952e<\/strong><\/p>\n<p><strong>1. \u4ec0\u4e48\u662f\u5916\u952e\uff1f<\/strong><\/p>\n<p>\u5916\u952e\u662f\u4e00\u79cd\u6570\u636e\u5e93\u7ea6\u675f\uff0c\u5b83\u7528\u4e8e\u786e\u4fdd\u5b50\u8868\u4e2d\u7684\u8bb0\u5f55\u4e0e\u4e3b\u8868\u4e2d\u7684\u76f8\u5173\u8bb0\u5f55\u5177\u6709\u5173\u8054\u6027\u3002\u5b83\u672c\u8d28\u4e0a\u5c06\u4e24\u4e2a\u8868\u94fe\u63a5\u5728\u4e00\u8d77\uff0c\u786e\u4fdd\u6570\u636e\u7684\u5b8c\u6574\u6027\u548c\u4e00\u81f4\u6027\u3002<\/p>\n<p><strong>2. \u5982\u4f55\u8bbe\u7f6e\u5916\u952e\uff1f<\/strong><\/p>\n<p>\u5728 SQL \u4e2d\uff0c\u4f7f\u7528 FOREIGN KEY \u7ea6\u675f\u521b\u5efa\u5916\u952e\u3002\u4ee5\u4e0b\u6b65\u9aa4\u8bf4\u660e\u4e86\u5982\u4f55\uff1a<\/p>\n<ul>\n<li> <strong>\u786e\u5b9a\u4e3b\u8868\u548c\u5b50\u8868\uff1a<\/strong>\u786e\u5b9a\u5305\u542b\u7236\u8bb0\u5f55\u7684\u4e3b\u8868\u548c\u5305\u542b\u5b50\u8bb0\u5f55\u7684\u5b50\u8868\u3002<\/li>\n<li> <strong>\u9009\u62e9\u76f8\u5e94\u5217\uff1a<\/strong>\u5728\u4e3b\u8868\u4e2d\u9009\u62e9\u7528\u4e8e\u521b\u5efa\u5916\u952e\u7684\u5217\uff08\u79f0\u4e3a\u7236\u5217\uff09\uff0c\u5728\u5b50\u8868\u4e2d\u9009\u62e9\u4e0e\u7236\u5217\u76f8\u5173\u8054\u7684\u5217\uff08\u79f0\u4e3a\u5b50\u5217\uff09\u3002<\/li>\n<li> <strong>\u521b\u5efa\u5916\u952e\u7ea6\u675f\uff1a<\/strong>\u4f7f\u7528\u4ee5\u4e0b\u8bed\u6cd5\u5728\u5b50\u8868\u4e2d\u521b\u5efa\u5916\u952e\u7ea6\u675f\uff1a<\/li>\n<\/ul>\n<pre>ALTER TABLE \u5b50\u8868\nADD FOREIGN KEY (\u5b50\u5217)\nREFERENCES \u4e3b\u8868(\u7236\u5217)<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>3. \u9644\u52a0\u9009\u9879<\/strong><\/p>\n<p>\u4ee5\u4e0b\u9644\u52a0\u9009\u9879\u53ef\u7528\u4e8e\u81ea\u5b9a\u4e49\u5916\u952e\u7ea6\u675f\uff1a<\/p>\n<ul>\n<li>ON DELETE [\u52a8\u4f5c]\uff1a\u6307\u5b9a\u5728\u4ece\u4e3b\u8868\u4e2d\u5220\u9664\u8bb0\u5f55\u65f6\u5b50\u8868\u4e2d\u76f8\u5e94\u8bb0\u5f55\u7684\u64cd\u4f5c\uff08\u4f8b\u5982\uff0cCASCADE\u3001SET NULL\u3001RESTRICT\uff09\u3002<\/li>\n<li>ON UPDATE [\u52a8\u4f5c]\uff1a\u6307\u5b9a\u5728\u4e3b\u8868\u4e2d\u66f4\u65b0\u8bb0\u5f55\u65f6\u5b50\u8868\u4e2d\u76f8\u5e94\u8bb0\u5f55\u7684\u64cd\u4f5c\uff08\u4f8b\u5982\uff0cCASCADE\u3001SET NULL\u3001RESTRICT\uff09\u3002<\/li>\n<li>NOT NULL\uff1a\u786e\u4fdd\u5b50\u5217\u7684\u503c\u4e0d\u80fd\u4e3a NULL\u3002<\/li>\n<\/ul>\n<p><strong>4. \u793a\u4f8b<\/strong><\/p>\n<p>\u4ee5\u4e0b\u793a\u4f8b\u5728\u5b50\u8868 \u8ba2\u5355 \u4e2d\u521b\u5efa\u4e00\u4e2a\u5916\u952e\u7ea6\u675f\uff0c\u8be5\u7ea6\u675f\u5f15\u7528\u4e3b\u8868 \u4ea7\u54c1 \u4e2d\u7684 \u4ea7\u54c1 ID \u5217\uff1a<\/p>\n<pre>ALTER TABLE \u8ba2\u5355\nADD FOREIGN KEY (\u4ea7\u54c1 ID)\nREFERENCES \u4ea7\u54c1(\u4ea7\u54c1 ID)\nON DELETE CASCADE\nON UPDATE CASCADE<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u4ea7\u54c1 ID \u5217\u662f\u4e3b\u8868 \u4ea7\u54c1 \u4e2d\u7684\u7236\u5217\uff0c\u4ea7\u54c1 ID \u5217\u662f\u5b50\u8868 \u8ba2\u5355 \u4e2d\u7684\u5b50\u5217\u3002\u5982\u679c\u4ece \u4ea7\u54c1 \u8868\u4e2d\u5220\u9664\u4e00\u6761\u8bb0\u5f55\uff0c\u5219\u4f1a\u81ea\u52a8\u4ece \u8ba2\u5355 \u8868\u4e2d\u7ea7\u8054\u5220\u9664\u6240\u6709\u76f8\u5173\u8bb0\u5f55\u3002\u540c\u6837\uff0c\u5982\u679c\u66f4\u65b0 \u4ea7\u54c1 \u8868\u4e2d\u7684 \u4ea7\u54c1 ID \u503c\uff0c\u5219 \u8ba2\u5355 \u8868\u4e2d\u76f8\u5173\u8bb0\u5f55\u7684 \u4ea7\u54c1 ID \u503c\u4e5f\u4f1a\u81ea\u52a8\u66f4\u65b0\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fsql\u6570\u636e\u5e93\u600e\u4e48\u8bbe\u7f6e\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>sql \u4e2d\u5916\u952e\u662f\u4e00\u79cd\u6570\u636e\u5e93\u7ea6\u675f\uff0c\u786e\u4fdd\u5b50\u8868\u8bb0\u5f55\u4e0e\u4e3b\u8868\u76f8\u5173\u8bb0\u5f55\u5173\u8054\u3002\u8981\u8bbe\u7f6e\u5916\u952e\uff0c\u9700\u8981\uff1a1. \u786e\u5b9a\u4e3b\u8868\u548c\u5b50\u8868\uff1b2. \u9009\u62e9\u76f8\u5e94\u5217\u4f5c\u4e3a\u7236\u5217\u548c\u5b50\u5217\uff1b3. \u4f7f\u7528 alter table \u8bed\u53e5\u521b\u5efa\u5916\u952e\u7ea6\u675f\uff0c\u6307\u5b9a\u5b50\u5217\u3001\u4e3b\u8868\u3001\u7236\u5217\u4ee5\u53ca\u9644\u52a0\u9009\u9879\uff08\u5982\u679c\u9700\u8981\uff09\u3002\u4f8b\u5982\uff0calter table \u8ba2\u5355 add foreign key (\u4ea7\u54c1 id) references \u4ea7\u54c1(\u4ea7\u54c1 id) on delete cascade on update cascade\u3002 SQL \u6570\u636e\u5e93\u4e2d\u8bbe\u7f6e\u5916\u952e 1. \u4ec0\u4e48\u662f\u5916\u952e\uff1f \u5916\u952e\u662f\u4e00\u79cd\u6570\u636e\u5e93\u7ea6\u675f\uff0c\u5b83\u7528\u4e8e\u786e\u4fdd\u5b50\u8868\u4e2d\u7684\u8bb0\u5f55\u4e0e\u4e3b\u8868\u4e2d\u7684\u76f8\u5173\u8bb0\u5f55\u5177\u6709\u5173\u8054\u6027\u3002\u5b83\u672c\u8d28\u4e0a\u5c06\u4e24\u4e2a\u8868\u94fe\u63a5\u5728\u4e00\u8d77\uff0c\u786e\u4fdd\u6570\u636e\u7684\u5b8c\u6574\u6027\u548c\u4e00\u81f4\u6027\u3002 2. \u5982\u4f55\u8bbe\u7f6e\u5916\u952e\uff1f \u5728 SQL \u4e2d\uff0c\u4f7f\u7528 FOREIGN KEY \u7ea6\u675f\u521b\u5efa\u5916\u952e\u3002\u4ee5\u4e0b\u6b65\u9aa4\u8bf4\u660e\u4e86\u5982\u4f55\uff1a \u786e\u5b9a\u4e3b\u8868\u548c\u5b50\u8868\uff1a\u786e\u5b9a\u5305\u542b\u7236\u8bb0\u5f55\u7684\u4e3b\u8868\u548c\u5305\u542b\u5b50\u8bb0\u5f55\u7684\u5b50\u8868\u3002 \u9009\u62e9\u76f8\u5e94\u5217\uff1a\u5728\u4e3b\u8868\u4e2d\u9009\u62e9\u7528\u4e8e\u521b\u5efa\u5916\u952e\u7684\u5217\uff08\u79f0\u4e3a\u7236\u5217\uff09\uff0c\u5728\u5b50\u8868\u4e2d\u9009\u62e9\u4e0e\u7236\u5217\u76f8\u5173\u8054\u7684\u5217\uff08\u79f0\u4e3a\u5b50\u5217\uff09\u3002 \u521b\u5efa\u5916\u952e\u7ea6\u675f\uff1a\u4f7f\u7528\u4ee5\u4e0b\u8bed\u6cd5\u5728\u5b50\u8868\u4e2d\u521b\u5efa\u5916\u952e\u7ea6\u675f\uff1a ALTER TABLE \u5b50\u8868 ADD FOREIGN KEY (\u5b50\u5217) REFERENCES \u4e3b\u8868(\u7236\u5217) \u767b\u5f55\u540e\u590d\u5236 3. \u9644\u52a0\u9009\u9879 \u4ee5\u4e0b\u9644\u52a0\u9009\u9879\u53ef\u7528\u4e8e\u81ea\u5b9a\u4e49\u5916\u952e\u7ea6\u675f\uff1a ON DELETE [&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-5631","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5631","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=5631"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5631\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=5631"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=5631"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=5631"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}