{"id":5488,"date":"2024-11-14T10:02:21","date_gmt":"2024-11-14T02:02:21","guid":{"rendered":"https:\/\/fwq.ai\/blog\/5488\/"},"modified":"2024-11-14T10:02:21","modified_gmt":"2024-11-14T02:02:21","slug":"mysql%e6%80%8e%e4%b9%88%e6%b7%bb%e5%8a%a0%e5%a4%96%e9%94%ae","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/5488\/","title":{"rendered":"mysql\u600e\u4e48\u6dfb\u52a0\u5916\u952e"},"content":{"rendered":"<p><strong>\u5982\u4f55\u4e3a  \u6570\u636e\u5e93\u6dfb\u52a0\u5916\u952e<\/strong><\/p>\n<p><strong>\u5916\u952e\u662f\u4ec0\u4e48\uff1f<\/strong><\/p>\n<p>\u5916\u952e\u662f\u4e00\u5217\uff0c\u5b83\u5f15\u7528\u53e6\u4e00\u5f20\u8868\u4e2d\u7684\u4e3b\u952e\uff0c\u4ee5\u5f3a\u5236\u4e24\u5f20\u8868\u4e4b\u95f4\u7684\u5173\u7cfb\u5b8c\u6574\u6027\u3002<\/p>\n<p><strong>\u6dfb\u52a0\u5916\u952e\u7684\u6b65\u9aa4\uff1a<\/strong><\/p>\n<ol>\n<li><strong>\u786e\u5b9a\u8981\u6dfb\u52a0\u5916\u952e\u7684\u8868\u548c\u5217\u3002<\/strong><\/li>\n<li><strong>\u5728\u521b\u5efa\u8868\u65f6\u6216\u901a\u8fc7 ALTER TABLE \u8bed\u53e5\u6dfb\u52a0\u5916\u952e\u7ea6\u675f\u3002<\/strong><\/li>\n<\/ol>\n<p><strong>\u901a\u8fc7\u521b\u5efa\u8868\u6dfb\u52a0\u5916\u952e\uff1a<\/strong><\/p>\n<pre>CREATE TABLE child_table (\n    id INT NOT NULL,\n    parent_id INT NOT NULL,\n    FOREIGN KEY (parent_id) REFERENCES parent_table(id)\n);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u901a\u8fc7 ALTER TABLE \u6dfb\u52a0\u5916\u952e\uff1a<\/strong><\/p>\n<pre>ALTER TABLE child_table\nADD FOREIGN KEY (parent_id) REFERENCES parent_table(id);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u5916\u952e\u5c5e\u6027\uff1a<\/strong><\/p>\n<ul>\n<li> <strong>ON DELETE\uff1a<\/strong>\u6307\u5b9a\u5f53\u5f15\u7528\u8868\u7684\u8bb0\u5f55\u88ab\u5220\u9664\u65f6\u5982\u4f55\u5904\u7406\u5b50\u8868\u8bb0\u5f55\u3002<\/li>\n<li> <strong>ON UPDATE\uff1a<\/strong>\u6307\u5b9a\u5f53\u5f15\u7528\u8868\u7684\u8bb0\u5f55\u88ab\u66f4\u65b0\u65f6\u5982\u4f55\u5904\u7406\u5b50\u8868\u8bb0\u5f55\u3002<\/li>\n<li> <strong>RESTRICT\uff1a<\/strong>\u963b\u6b62\u5220\u9664\u6216\u66f4\u65b0\u5f15\u7528\u8868\u7684\u8bb0\u5f55\uff0c\u9664\u975e\u5b50\u8868\u4e2d\u6ca1\u6709\u5f15\u7528\u5b83\u7684\u8bb0\u5f55\u3002<\/li>\n<li> <strong>CASCADE\uff1a<\/strong>\u81ea\u52a8\u7ea7\u8054\u5220\u9664\u6216\u66f4\u65b0\u5b50\u8868\u8bb0\u5f55\u3002<\/li>\n<li> <strong>SET NULL\uff1a<\/strong>\u5c06\u5b50\u8868\u4e2d\u5f15\u7528\u88ab\u5220\u9664\u6216\u66f4\u65b0\u7684\u8bb0\u5f55\u7684\u5217\u8bbe\u7f6e\u4e3a NULL\u3002<\/li>\n<\/ul>\n<p><strong>\u6ce8\u610f\u4e8b\u9879\uff1a<\/strong><\/p>\n<ul>\n<li>\u786e\u4fdd\u5916\u952e\u5217\u7684\u6570\u636e\u7c7b\u578b\u4e0e\u88ab\u5f15\u7528\u7684\u4e3b\u952e\u5217\u76f8\u540c\u3002<\/li>\n<li>\u5728\u6dfb\u52a0\u5916\u952e\u4e4b\u524d\uff0c\u5fc5\u987b\u5b58\u5728\u88ab\u5f15\u7528\u7684\u8868\u548c\u4e3b\u952e\u3002<\/li>\n<li>\u6dfb\u52a0\u5916\u952e\u540e\uff0c\u65e0\u6cd5\u5220\u9664\u6216\u66f4\u65b0\u88ab\u5f15\u7528\u7684\u8868\u7684\u8bb0\u5f55\uff0c\u9664\u975e\u5148\u7ea7\u8054\u5220\u9664\u6216\u66f4\u65b0\u5b50\u8868\u8bb0\u5f55\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662f\u600e\u4e48\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>\u5982\u4f55\u4e3a \u6570\u636e\u5e93\u6dfb\u52a0\u5916\u952e \u5916\u952e\u662f\u4ec0\u4e48\uff1f \u5916\u952e\u662f\u4e00\u5217\uff0c\u5b83\u5f15\u7528\u53e6\u4e00\u5f20\u8868\u4e2d\u7684\u4e3b\u952e\uff0c\u4ee5\u5f3a\u5236\u4e24\u5f20\u8868\u4e4b\u95f4\u7684\u5173\u7cfb\u5b8c\u6574\u6027\u3002 \u6dfb\u52a0\u5916\u952e\u7684\u6b65\u9aa4\uff1a \u786e\u5b9a\u8981\u6dfb\u52a0\u5916\u952e\u7684\u8868\u548c\u5217\u3002 \u5728\u521b\u5efa\u8868\u65f6\u6216\u901a\u8fc7 ALTER TABLE \u8bed\u53e5\u6dfb\u52a0\u5916\u952e\u7ea6\u675f\u3002 \u901a\u8fc7\u521b\u5efa\u8868\u6dfb\u52a0\u5916\u952e\uff1a CREATE TABLE child_table ( id INT NOT NULL, parent_id INT NOT NULL, FOREIGN KEY (parent_id) REFERENCES parent_table(id) ); \u767b\u5f55\u540e\u590d\u5236 \u901a\u8fc7 ALTER TABLE \u6dfb\u52a0\u5916\u952e\uff1a ALTER TABLE child_table ADD FOREIGN KEY (parent_id) REFERENCES parent_table(id); \u767b\u5f55\u540e\u590d\u5236 \u5916\u952e\u5c5e\u6027\uff1a ON DELETE\uff1a\u6307\u5b9a\u5f53\u5f15\u7528\u8868\u7684\u8bb0\u5f55\u88ab\u5220\u9664\u65f6\u5982\u4f55\u5904\u7406\u5b50\u8868\u8bb0\u5f55\u3002 ON UPDATE\uff1a\u6307\u5b9a\u5f53\u5f15\u7528\u8868\u7684\u8bb0\u5f55\u88ab\u66f4\u65b0\u65f6\u5982\u4f55\u5904\u7406\u5b50\u8868\u8bb0\u5f55\u3002 RESTRICT\uff1a\u963b\u6b62\u5220\u9664\u6216\u66f4\u65b0\u5f15\u7528\u8868\u7684\u8bb0\u5f55\uff0c\u9664\u975e\u5b50\u8868\u4e2d\u6ca1\u6709\u5f15\u7528\u5b83\u7684\u8bb0\u5f55\u3002 CASCADE\uff1a\u81ea\u52a8\u7ea7\u8054\u5220\u9664\u6216\u66f4\u65b0\u5b50\u8868\u8bb0\u5f55\u3002 SET NULL\uff1a\u5c06\u5b50\u8868\u4e2d\u5f15\u7528\u88ab\u5220\u9664\u6216\u66f4\u65b0\u7684\u8bb0\u5f55\u7684\u5217\u8bbe\u7f6e\u4e3a NULL\u3002 \u6ce8\u610f\u4e8b\u9879\uff1a [&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-5488","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5488","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=5488"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5488\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=5488"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=5488"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=5488"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}