{"id":5408,"date":"2024-11-14T12:37:29","date_gmt":"2024-11-14T04:37:29","guid":{"rendered":"https:\/\/fwq.ai\/blog\/5408\/"},"modified":"2024-11-14T12:37:29","modified_gmt":"2024-11-14T04:37:29","slug":"mysql%e5%a4%96%e9%94%ae%e7%ba%a6%e6%9d%9f%e6%80%8e%e4%b9%88%e5%bc%84","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/5408\/","title":{"rendered":"mysql\u5916\u952e\u7ea6\u675f\u600e\u4e48\u5f04"},"content":{"rendered":"<blockquote><p>\n  \u5982\u4f55\u521b\u5efa mysql \u5916\u952e\u7ea6\u675f\uff1f\u786e\u5b9a\u7236\u8868\u548c\u5b50\u8868\u3002\u5728\u7236\u8868\u4e2d\u521b\u5efa\u4e3b\u952e\u7ea6\u675f\u3002\u5728\u5b50\u8868\u4e2d\u521b\u5efa\u5916\u952e\u7ea6\u675f\uff0c\u5f15\u7528\u7236\u8868\u4e2d\u7684\u7279\u5b9a\u5217\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202405\/28\/2024052811272783531.jpg\" class=\"aligncenter\" title=\"mysql\u5916\u952e\u7ea6\u675f\u600e\u4e48\u5f04\u63d2\u56fe\" alt=\"mysql\u5916\u952e\u7ea6\u675f\u600e\u4e48\u5f04\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u521b\u5efa MySQL \u5916\u952e\u7ea6\u675f<\/strong><\/p>\n<p>\u5728 MySQL \u4e2d\u521b\u5efa\u5916\u952e\u7ea6\u675f\u53ef\u786e\u4fdd\u6570\u636e\u5b8c\u6574\u6027\uff0c\u5e76\u963b\u6b62\u5bf9\u7236\u8868\u4e0d\u4e00\u81f4\u7684\u66f4\u6539\u3002\u4ee5\u4e0b\u6b65\u9aa4\u8bf4\u660e\u5982\u4f55\u521b\u5efa\u5916\u952e\u7ea6\u675f\uff1a<\/p>\n<p><strong>\u6b65\u9aa4 1\uff1a\u786e\u5b9a\u7236\u8868\u548c\u5b50\u8868<\/strong><\/p>\n<p>\u786e\u5b9a\u8981\u5efa\u7acb\u5916\u952e\u5173\u7cfb\u7684\u7236\u8868\u548c\u5b50\u8868\u3002\u5916\u952e\u5217\u5c06\u5b58\u5728\u4e8e\u5b50\u8868\u4e2d\uff0c\u5e76\u5f15\u7528\u7236\u8868\u4e2d\u7684\u4e3b\u952e\u5217\u3002<\/p>\n<p><strong>\u6b65\u9aa4 2\uff1a\u521b\u5efa\u7236\u8868\u7ea6\u675f<\/strong><\/p>\n<p>\u5728\u7236\u8868\u4e2d\u521b\u5efa\u4e3b\u952e\u7ea6\u675f\u4ee5\u552f\u4e00\u6807\u8bc6\u6bcf\u884c\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>ALTER TABLE parent_table ADD PRIMARY KEY (id);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u6b65\u9aa4 3\uff1a\u5728\u5b50\u8868\u4e2d\u521b\u5efa\u5916\u952e\u7ea6\u675f<\/strong><\/p>\n<p>\u4f7f\u7528 FOREIGN KEY \u5b50\u53e5\u5728\u5b50\u8868\u4e2d\u521b\u5efa\u5916\u952e\u7ea6\u675f\uff0c\u5f15\u7528\u7236\u8868\u7684\u7279\u5b9a\u5217\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>ALTER TABLE child_table ADD FOREIGN KEY (parent_id) REFERENCES parent_table(id);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u7ea6\u675f\u9009\u9879<\/strong><\/p>\n<p>\u8fd8\u53ef\u4ee5\u914d\u7f6e\u4ee5\u4e0b\u7ea6\u675f\u9009\u9879\uff1a<\/p>\n<ul>\n<li> <strong>ON UPDATE CASCADE\uff1a<\/strong>\u5f53\u7236\u8868\u7684\u76f8\u5173\u8bb0\u5f55\u53d1\u751f\u66f4\u6539\u65f6\uff0c\u5c06\u81ea\u52a8\u66f4\u65b0\u5b50\u8868\u4e2d\u5f15\u7528\u7684\u8bb0\u5f55\u3002<\/li>\n<li> <strong>ON DELETE CASCADE\uff1a<\/strong>\u5f53\u7236\u8868\u7684\u76f8\u5173\u8bb0\u5f55\u88ab\u5220\u9664\u65f6\uff0c\u5c06\u81ea\u52a8\u5220\u9664\u5b50\u8868\u4e2d\u5f15\u7528\u7684\u8bb0\u5f55\u3002<\/li>\n<li> <strong>ON UPDATE RESTRICT\/SET NULL\/SET DEFAULT\uff1a<\/strong>\u6839\u636e\u6307\u5b9a\u7684\u64cd\u4f5c\uff08\u9650\u5236\u3001\u8bbe\u7f6e\u4e3a\u7a7a\u6216\u8bbe\u7f6e\u9ed8\u8ba4\u503c\uff09\u6765\u5904\u7406\u5bf9\u7236\u8868\u4e2d\u76f8\u5173\u8bb0\u5f55\u7684\u66f4\u65b0\u3002<\/li>\n<li> <strong>ON DELETE RESTRICT\/SET NULL\/SET DEFAULT\uff1a<\/strong>\u6839\u636e\u6307\u5b9a\u7684\u64cd\u4f5c\uff08\u9650\u5236\u3001\u8bbe\u7f6e\u4e3a\u7a7a\u6216\u8bbe\u7f6e\u9ed8\u8ba4\u503c\uff09\u6765\u5904\u7406\u5bf9\u7236\u8868\u4e2d\u76f8\u5173\u8bb0\u5f55\u7684\u5220\u9664\u3002<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b<\/strong><\/p>\n<p>\u4ee5\u4e0b\u793a\u4f8b\u521b\u5efa\u4e00\u4e2a\u7236\u8868 customers \u548c\u4e00\u4e2a\u5b50\u8868 orders\uff0c\u5176\u4e2d customer_id \u5217\u662f\u5b50\u8868\u4e2d\u7684\u5916\u952e\uff0c\u5f15\u7528 customers \u8868\u4e2d\u7684 id \u5217\uff1a<\/p>\n<pre>CREATE TABLE customers (\n  id INT NOT NULL AUTO_INCREMENT,\n  name VARCHAR(255) NOT NULL,\n  PRIMARY KEY (id)\n);\n\nCREATE TABLE orders (\n  id INT NOT NULL AUTO_INCREMENT,\n  customer_id INT NOT NULL,\n  product_id INT NOT NULL,\n  FOREIGN KEY (customer_id) REFERENCES customers(id)\n);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u901a\u8fc7\u521b\u5efa\u5916\u952e\u7ea6\u675f\uff0c\u53ef\u4ee5\u786e\u4fdd orders \u8868\u4e2d\u7684\u8bb0\u5f55\u4e0e customers \u8868\u4e2d\u7684\u6709\u6548\u8bb0\u5f55\u76f8\u5173\u8054\u3002\u8fd9\u5c06\u6709\u52a9\u4e8e\u9632\u6b62\u6570\u636e\u4e0d\u4e00\u81f4\uff0c\u5e76\u7ef4\u62a4\u6570\u636e\u5e93\u7684\u5b8c\u6574\u6027\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5916\u952e\u7ea6\u675f\u600e\u4e48\u5f04\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\u521b\u5efa mysql \u5916\u952e\u7ea6\u675f\uff1f\u786e\u5b9a\u7236\u8868\u548c\u5b50\u8868\u3002\u5728\u7236\u8868\u4e2d\u521b\u5efa\u4e3b\u952e\u7ea6\u675f\u3002\u5728\u5b50\u8868\u4e2d\u521b\u5efa\u5916\u952e\u7ea6\u675f\uff0c\u5f15\u7528\u7236\u8868\u4e2d\u7684\u7279\u5b9a\u5217\u3002 \u5982\u4f55\u521b\u5efa MySQL \u5916\u952e\u7ea6\u675f \u5728 MySQL \u4e2d\u521b\u5efa\u5916\u952e\u7ea6\u675f\u53ef\u786e\u4fdd\u6570\u636e\u5b8c\u6574\u6027\uff0c\u5e76\u963b\u6b62\u5bf9\u7236\u8868\u4e0d\u4e00\u81f4\u7684\u66f4\u6539\u3002\u4ee5\u4e0b\u6b65\u9aa4\u8bf4\u660e\u5982\u4f55\u521b\u5efa\u5916\u952e\u7ea6\u675f\uff1a \u6b65\u9aa4 1\uff1a\u786e\u5b9a\u7236\u8868\u548c\u5b50\u8868 \u786e\u5b9a\u8981\u5efa\u7acb\u5916\u952e\u5173\u7cfb\u7684\u7236\u8868\u548c\u5b50\u8868\u3002\u5916\u952e\u5217\u5c06\u5b58\u5728\u4e8e\u5b50\u8868\u4e2d\uff0c\u5e76\u5f15\u7528\u7236\u8868\u4e2d\u7684\u4e3b\u952e\u5217\u3002 \u6b65\u9aa4 2\uff1a\u521b\u5efa\u7236\u8868\u7ea6\u675f \u5728\u7236\u8868\u4e2d\u521b\u5efa\u4e3b\u952e\u7ea6\u675f\u4ee5\u552f\u4e00\u6807\u8bc6\u6bcf\u884c\u3002\u4f8b\u5982\uff1a ALTER TABLE parent_table ADD PRIMARY KEY (id); \u767b\u5f55\u540e\u590d\u5236 \u6b65\u9aa4 3\uff1a\u5728\u5b50\u8868\u4e2d\u521b\u5efa\u5916\u952e\u7ea6\u675f \u4f7f\u7528 FOREIGN KEY \u5b50\u53e5\u5728\u5b50\u8868\u4e2d\u521b\u5efa\u5916\u952e\u7ea6\u675f\uff0c\u5f15\u7528\u7236\u8868\u7684\u7279\u5b9a\u5217\u3002\u4f8b\u5982\uff1a ALTER TABLE child_table ADD FOREIGN KEY (parent_id) REFERENCES parent_table(id); \u767b\u5f55\u540e\u590d\u5236 \u7ea6\u675f\u9009\u9879 \u8fd8\u53ef\u4ee5\u914d\u7f6e\u4ee5\u4e0b\u7ea6\u675f\u9009\u9879\uff1a ON UPDATE CASCADE\uff1a\u5f53\u7236\u8868\u7684\u76f8\u5173\u8bb0\u5f55\u53d1\u751f\u66f4\u6539\u65f6\uff0c\u5c06\u81ea\u52a8\u66f4\u65b0\u5b50\u8868\u4e2d\u5f15\u7528\u7684\u8bb0\u5f55\u3002 ON DELETE CASCADE\uff1a\u5f53\u7236\u8868\u7684\u76f8\u5173\u8bb0\u5f55\u88ab\u5220\u9664\u65f6\uff0c\u5c06\u81ea\u52a8\u5220\u9664\u5b50\u8868\u4e2d\u5f15\u7528\u7684\u8bb0\u5f55\u3002 ON UPDATE RESTRICT\/SET NULL\/SET DEFAULT\uff1a\u6839\u636e\u6307\u5b9a\u7684\u64cd\u4f5c\uff08\u9650\u5236\u3001\u8bbe\u7f6e\u4e3a\u7a7a\u6216\u8bbe\u7f6e\u9ed8\u8ba4\u503c\uff09\u6765\u5904\u7406\u5bf9\u7236\u8868\u4e2d\u76f8\u5173\u8bb0\u5f55\u7684\u66f4\u65b0\u3002 ON DELETE RESTRICT\/SET [&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-5408","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5408","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=5408"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5408\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=5408"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=5408"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=5408"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}