{"id":5781,"date":"2024-11-14T10:58:06","date_gmt":"2024-11-14T02:58:06","guid":{"rendered":"https:\/\/fwq.ai\/blog\/5781\/"},"modified":"2024-11-14T10:58:06","modified_gmt":"2024-11-14T02:58:06","slug":"mysql%e5%a4%96%e9%94%ae%e4%bb%80%e4%b9%88%e6%84%8f%e6%80%9d","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/5781\/","title":{"rendered":"mysql\u5916\u952e\u4ec0\u4e48\u610f\u601d"},"content":{"rendered":"<blockquote><p>\n  \u5916\u952e\u662f\u4e00\u79cd\u6570\u636e\u5e93\u5173\u7cfb\uff0c\u7528\u4e8e\u5c06\u4e00\u4e2a\u8868\u4e2d\u7684\u5217\u4e0e\u53e6\u4e00\u4e2a\u8868\u4e2d\u7684\u5217\u5173\u8054\uff0c\u4ee5\u786e\u4fdd\u5b50\u8868\u6570\u636e\u4e0e\u4e3b\u8868\u4e00\u81f4\u3002\u5728 mysql \u4e2d\u4f7f\u7528 foreign key \u7ea6\u675f\u521b\u5efa\u5916\u952e\uff0c\u5b83\u7684\u597d\u5904\u5305\u62ec\u6570\u636e\u5b8c\u6574\u6027\u3001\u7ea7\u8054\u64cd\u4f5c\u3001\u67e5\u8be2\u4f18\u5316\u548c\u6570\u636e\u5efa\u6a21\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202406\/02\/2024060201184432145.jpg\" class=\"aligncenter\" title=\"mysql\u5916\u952e\u4ec0\u4e48\u610f\u601d\u63d2\u56fe\" alt=\"mysql\u5916\u952e\u4ec0\u4e48\u610f\u601d\u63d2\u56fe\" \/><\/p>\n<p><strong>MySQL \u5916\u952e<\/strong><\/p>\n<p><strong>\u5916\u952e\u662f\u4ec0\u4e48\u610f\u601d\uff1f<\/strong><\/p>\n<p>\u5916\u952e\u662f\u4e00\u79cd\u6570\u636e\u5e93\u5173\u7cfb\u7c7b\u578b\uff0c\u7528\u4e8e\u5c06\u4e00\u4e2a\u8868\uff08<strong>\u5b50\u8868<\/strong>\uff09\u4e2d\u7684\u5217\u4e0e\u53e6\u4e00\u4e2a\u8868\uff08<strong>\u4e3b\u8868<\/strong>\uff09\u4e2d\u7684\u5217\u5173\u8054\u3002\u5b83\u786e\u4fdd\u5b50\u8868\u4e2d\u7684\u6570\u636e\u4e0e\u4e3b\u8868\u4e2d\u76f8\u5e94\u6570\u636e\u4e00\u81f4\u3002<\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528\u5916\u952e\uff1f<\/strong><\/p>\n<p>\u5728 MySQL \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 FOREIGN KEY \u7ea6\u675f\u521b\u5efa\u5916\u952e\u3002FOREIGN KEY \u7ea6\u675f\u6307\u5b9a\u5b50\u8868\u4e2d\u7684\u5217\u4e0e\u4e3b\u8868\u4e2d\u7684\u5217\u4e4b\u95f4\u7684\u5173\u7cfb\u3002<\/p>\n<pre>CREATE TABLE sub_table (\n    id INT NOT NULL,\n    main_table_id INT NOT NULL,\n    FOREIGN KEY (main_table_id) REFERENCES main_table(id)\n);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0csub_table \u4e2d\u7684 main_table_id \u5217\u662f\u5916\u952e\uff0c\u5b83\u5f15\u7528 main_table \u8868\u4e2d\u7684 id \u5217\u3002\u8fd9\u610f\u5473\u7740 sub_table \u4e2d\u7684\u6bcf\u4e00\u884c\u90fd\u5fc5\u987b\u5177\u6709 main_table \u4e2d\u5b58\u5728\u4e14\u552f\u4e00\u7684 id \u503c\u3002<\/p>\n<p><strong>\u5916\u952e\u7684\u597d\u5904<\/strong><\/p>\n<p>\u4f7f\u7528\u5916\u952e\u7684\u597d\u5904\u5305\u62ec\uff1a<\/p>\n<ul>\n<li> <strong>\u6570\u636e\u5b8c\u6574\u6027\uff1a<\/strong>\u5916\u952e\u786e\u4fdd\u5b50\u8868\u4e2d\u7684\u6570\u636e\u4e0e\u4e3b\u8868\u4e2d\u76f8\u5e94\u6570\u636e\u4e00\u81f4\u3002<\/li>\n<li> <strong>\u7ea7\u8054\u64cd\u4f5c\uff1a<\/strong>\u5f53\u5728\u4e3b\u8868\u4e2d\u66f4\u6539\u8bb0\u5f55\u65f6\uff0c\u6570\u636e\u5e93\u53ef\u4ee5\u81ea\u52a8\u66f4\u65b0\u6216\u5220\u9664\u5b50\u8868\u4e2d\u7684\u76f8\u5173\u8bb0\u5f55\u3002<\/li>\n<li> <strong>\u67e5\u8be2\u4f18\u5316\uff1a<\/strong>\u5916\u952e\u53ef\u4ee5\u5e2e\u52a9\u6570\u636e\u5e93\u4f18\u5316\u67e5\u8be2\uff0c\u56e0\u4e3a\u5b83\u5229\u7528\u7d22\u5f15\u67e5\u627e\u76f8\u5173\u6570\u636e\u3002<\/li>\n<li> <strong>\u6570\u636e\u5efa\u6a21\uff1a<\/strong>\u5916\u952e\u662f\u5173\u7cfb\u6570\u636e\u5e93\u6570\u636e\u5efa\u6a21\u7684\u5173\u952e\u90e8\u5206\uff0c\u6709\u52a9\u4e8e\u521b\u5efa\u903b\u8f91\u4e00\u81f4\u7684\u8868\u5173\u7cfb\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5916\u952e\u4ec0\u4e48\u610f\u601d\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\u4e00\u79cd\u6570\u636e\u5e93\u5173\u7cfb\uff0c\u7528\u4e8e\u5c06\u4e00\u4e2a\u8868\u4e2d\u7684\u5217\u4e0e\u53e6\u4e00\u4e2a\u8868\u4e2d\u7684\u5217\u5173\u8054\uff0c\u4ee5\u786e\u4fdd\u5b50\u8868\u6570\u636e\u4e0e\u4e3b\u8868\u4e00\u81f4\u3002\u5728 mysql \u4e2d\u4f7f\u7528 foreign key \u7ea6\u675f\u521b\u5efa\u5916\u952e\uff0c\u5b83\u7684\u597d\u5904\u5305\u62ec\u6570\u636e\u5b8c\u6574\u6027\u3001\u7ea7\u8054\u64cd\u4f5c\u3001\u67e5\u8be2\u4f18\u5316\u548c\u6570\u636e\u5efa\u6a21\u3002 MySQL \u5916\u952e \u5916\u952e\u662f\u4ec0\u4e48\u610f\u601d\uff1f \u5916\u952e\u662f\u4e00\u79cd\u6570\u636e\u5e93\u5173\u7cfb\u7c7b\u578b\uff0c\u7528\u4e8e\u5c06\u4e00\u4e2a\u8868\uff08\u5b50\u8868\uff09\u4e2d\u7684\u5217\u4e0e\u53e6\u4e00\u4e2a\u8868\uff08\u4e3b\u8868\uff09\u4e2d\u7684\u5217\u5173\u8054\u3002\u5b83\u786e\u4fdd\u5b50\u8868\u4e2d\u7684\u6570\u636e\u4e0e\u4e3b\u8868\u4e2d\u76f8\u5e94\u6570\u636e\u4e00\u81f4\u3002 \u5982\u4f55\u4f7f\u7528\u5916\u952e\uff1f \u5728 MySQL \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 FOREIGN KEY \u7ea6\u675f\u521b\u5efa\u5916\u952e\u3002FOREIGN KEY \u7ea6\u675f\u6307\u5b9a\u5b50\u8868\u4e2d\u7684\u5217\u4e0e\u4e3b\u8868\u4e2d\u7684\u5217\u4e4b\u95f4\u7684\u5173\u7cfb\u3002 CREATE TABLE sub_table ( id INT NOT NULL, main_table_id INT NOT NULL, FOREIGN KEY (main_table_id) REFERENCES main_table(id) ); \u767b\u5f55\u540e\u590d\u5236 \u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0csub_table \u4e2d\u7684 main_table_id \u5217\u662f\u5916\u952e\uff0c\u5b83\u5f15\u7528 main_table \u8868\u4e2d\u7684 id \u5217\u3002\u8fd9\u610f\u5473\u7740 sub_table \u4e2d\u7684\u6bcf\u4e00\u884c\u90fd\u5fc5\u987b\u5177\u6709 main_table \u4e2d\u5b58\u5728\u4e14\u552f\u4e00\u7684 id \u503c\u3002 \u5916\u952e\u7684\u597d\u5904 \u4f7f\u7528\u5916\u952e\u7684\u597d\u5904\u5305\u62ec\uff1a \u6570\u636e\u5b8c\u6574\u6027\uff1a\u5916\u952e\u786e\u4fdd\u5b50\u8868\u4e2d\u7684\u6570\u636e\u4e0e\u4e3b\u8868\u4e2d\u76f8\u5e94\u6570\u636e\u4e00\u81f4\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-5781","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5781","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=5781"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5781\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=5781"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=5781"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=5781"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}