{"id":5486,"date":"2024-11-14T10:32:26","date_gmt":"2024-11-14T02:32:26","guid":{"rendered":"https:\/\/fwq.ai\/blog\/5486\/"},"modified":"2024-11-14T10:32:26","modified_gmt":"2024-11-14T02:32:26","slug":"mysql%e6%80%8e%e4%b9%88%e5%8a%a0%e5%a4%96%e9%94%ae%e7%ba%a6%e6%9d%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/5486\/","title":{"rendered":"mysql\u600e\u4e48\u52a0\u5916\u952e\u7ea6\u675f"},"content":{"rendered":"<blockquote><p>\n  \u5982\u4f55\u4f7f\u7528 mysql \u6dfb\u52a0\u5916\u952e\u7ea6\u675f\uff1f\u786e\u5b9a\u4e3b\u5916\u952e\u5b57\u6bb5\u3002\u5728\u5b50\u8868\u4e2d\u5b9a\u4e49\u5916\u952e\u7ea6\u675f\uff0c\u5f15\u7528\u4e3b\u8868\u7684\u4e3b\u952e\u5b57\u6bb5\u3002\u8bed\u6cd5\uff1aalter table \u5b50\u8868_\u540d\u79f0 add foreign key (\u5916\u952e_\u5b57\u6bb5) references \u4e3b\u8868_\u540d\u79f0 (\u4e3b\u952e_\u5b57\u6bb5)\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202405\/29\/2024052903572539644.jpg\" class=\"aligncenter\" title=\"mysql\u600e\u4e48\u52a0\u5916\u952e\u7ea6\u675f\u63d2\u56fe\" alt=\"mysql\u600e\u4e48\u52a0\u5916\u952e\u7ea6\u675f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528 MySQL \u6dfb\u52a0\u5916\u952e\u7ea6\u675f<\/strong><\/p>\n<p><strong>\u6dfb\u52a0\u5916\u952e\u7ea6\u675f\u7684\u6b65\u9aa4\uff1a<\/strong><\/p>\n<ol>\n<li> <strong>\u786e\u5b9a\u4e3b\u5916\u952e\u5b57\u6bb5\uff1a<\/strong>\u786e\u5b9a\u9700\u8981\u5efa\u7acb\u5173\u7cfb\u7684\u4e24\u4e2a\u8868\u7684\u4e3b\u952e\u548c\u5916\u952e\u5b57\u6bb5\u3002<\/li>\n<li> <strong>\u5728\u5b50\u8868\u4e2d\u5b9a\u4e49\u5916\u952e\u7ea6\u675f\uff1a<\/strong>\u5728\u5305\u542b\u5916\u952e\u5b57\u6bb5\u7684\u5b50\u8868\u4e2d\u6dfb\u52a0 FOREIGN KEY \u7ea6\u675f\u3002<\/li>\n<li> <strong>\u5f15\u7528\u4e3b\u8868\u7684\u4e3b\u952e\uff1a<\/strong>\u5728 FOREIGN KEY \u7ea6\u675f\u4e2d\uff0c\u5c06\u5916\u952e\u5b57\u6bb5\u5f15\u7528\u5230\u4e3b\u8868\u4e2d\u7684\u4e3b\u952e\u5b57\u6bb5\u3002<\/li>\n<\/ol>\n<p><strong>\u8bed\u6cd5\uff1a<\/strong><\/p>\n<pre>ALTER TABLE \u5b50\u8868_\u540d\u79f0\nADD FOREIGN KEY (\u5916\u952e_\u5b57\u6bb5) REFERENCES \u4e3b\u8868_\u540d\u79f0 (\u4e3b\u952e_\u5b57\u6bb5)<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<p>\u5047\u8bbe\u6211\u4eec\u6709\u4ee5\u4e0b\u4e24\u4e2a\u8868\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th>\u8868<\/th>\n<th>\u5217<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\u8ba2\u5355<\/td>\n<td>\u8ba2\u5355\u53f7<\/td>\n<\/tr>\n<tr>\n<td>\u8ba2\u5355\u660e\u7ec6<\/td>\n<td>\u8ba2\u5355\u53f7<\/td>\n<td>\u4ea7\u54c1\u7f16\u53f7<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u8981\u5c06 \u8ba2\u5355\u660e\u7ec6 \u8868\u4e2d\u7684 \u8ba2\u5355\u53f7 \u5217\u4e0e \u8ba2\u5355 \u8868\u4e2d\u7684 \u8ba2\u5355\u53f7 \u5217\u5efa\u7acb\u5916\u952e\u5173\u7cfb\uff0c\u53ef\u4ee5\u6267\u884c\u4ee5\u4e0b\u8bed\u53e5\uff1a<\/p>\n<pre>ALTER TABLE \u8ba2\u5355\u660e\u7ec6\nADD FOREIGN KEY (\u8ba2\u5355\u53f7) REFERENCES \u8ba2\u5355 (\u8ba2\u5355\u53f7)<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u5916\u952e\u7ea6\u675f\u7684\u597d\u5904\uff1a<\/strong><\/p>\n<ul>\n<li> <strong>\u4fdd\u6301\u6570\u636e\u5b8c\u6574\u6027\uff1a<\/strong>\u5916\u952e\u7ea6\u675f\u786e\u4fdd\u5b50\u8868\u4e2d\u5f15\u7528\u7684\u6240\u6709\u5916\u952e\u503c\u90fd\u5b58\u5728\u4e8e\u4e3b\u8868\u4e2d\u3002<\/li>\n<li> <strong>\u5f3a\u5236\u5173\u7cfb\uff1a<\/strong>\u5916\u952e\u7ea6\u675f\u5f3a\u5236\u4e3b\u8868\u548c\u5b50\u8868\u4e4b\u95f4\u7684\u5173\u7cfb\uff0c\u9632\u6b62\u4e0d\u4e00\u81f4\u7684\u6570\u636e\u3002<\/li>\n<li> <strong>\u7b80\u5316\u6570\u636e\u67e5\u8be2\uff1a<\/strong>\u5916\u952e\u7ea6\u675f\u5141\u8bb8\u901a\u8fc7\u5916\u952e\u5b57\u6bb5\u5feb\u901f\u67e5\u627e\u76f8\u5173\u8bb0\u5f55\u3002<\/li>\n<\/ul>\n<p><strong>\u6ce8\u610f\uff1a<\/strong><\/p>\n<ul>\n<li>\u6dfb\u52a0\u5916\u952e\u7ea6\u675f\u540e\uff0c\u5b50\u8868\u4e2d\u5c06\u65e0\u6cd5\u63d2\u5165\u6216\u66f4\u65b0\u4e3a\u4e0d\u5b58\u5728\u4e8e\u4e3b\u8868\u4e2d\u7684\u503c\u3002<\/li>\n<li>\u5220\u9664\u4e3b\u8868\u4e2d\u7684\u8bb0\u5f55\u5c06\u81ea\u52a8\u7ea7\u8054\u5220\u9664\u5b50\u8868\u4e2d\u5f15\u7528\u8be5\u8bb0\u5f55\u7684\u6240\u6709\u8bb0\u5f55\uff08\u5982\u679c\u542f\u7528\u4e86\u7ea7\u8054\u5220\u9664\uff09\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662f\u600e\u4e48\u52a0\u5916\u952e\u7ea6\u675f\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\u4f7f\u7528 mysql \u6dfb\u52a0\u5916\u952e\u7ea6\u675f\uff1f\u786e\u5b9a\u4e3b\u5916\u952e\u5b57\u6bb5\u3002\u5728\u5b50\u8868\u4e2d\u5b9a\u4e49\u5916\u952e\u7ea6\u675f\uff0c\u5f15\u7528\u4e3b\u8868\u7684\u4e3b\u952e\u5b57\u6bb5\u3002\u8bed\u6cd5\uff1aalter table \u5b50\u8868_\u540d\u79f0 add foreign key (\u5916\u952e_\u5b57\u6bb5) references \u4e3b\u8868_\u540d\u79f0 (\u4e3b\u952e_\u5b57\u6bb5) \u5982\u4f55\u4f7f\u7528 MySQL \u6dfb\u52a0\u5916\u952e\u7ea6\u675f \u6dfb\u52a0\u5916\u952e\u7ea6\u675f\u7684\u6b65\u9aa4\uff1a \u786e\u5b9a\u4e3b\u5916\u952e\u5b57\u6bb5\uff1a\u786e\u5b9a\u9700\u8981\u5efa\u7acb\u5173\u7cfb\u7684\u4e24\u4e2a\u8868\u7684\u4e3b\u952e\u548c\u5916\u952e\u5b57\u6bb5\u3002 \u5728\u5b50\u8868\u4e2d\u5b9a\u4e49\u5916\u952e\u7ea6\u675f\uff1a\u5728\u5305\u542b\u5916\u952e\u5b57\u6bb5\u7684\u5b50\u8868\u4e2d\u6dfb\u52a0 FOREIGN KEY \u7ea6\u675f\u3002 \u5f15\u7528\u4e3b\u8868\u7684\u4e3b\u952e\uff1a\u5728 FOREIGN KEY \u7ea6\u675f\u4e2d\uff0c\u5c06\u5916\u952e\u5b57\u6bb5\u5f15\u7528\u5230\u4e3b\u8868\u4e2d\u7684\u4e3b\u952e\u5b57\u6bb5\u3002 \u8bed\u6cd5\uff1a ALTER TABLE \u5b50\u8868_\u540d\u79f0 ADD FOREIGN KEY (\u5916\u952e_\u5b57\u6bb5) REFERENCES \u4e3b\u8868_\u540d\u79f0 (\u4e3b\u952e_\u5b57\u6bb5) \u767b\u5f55\u540e\u590d\u5236 \u793a\u4f8b\uff1a \u5047\u8bbe\u6211\u4eec\u6709\u4ee5\u4e0b\u4e24\u4e2a\u8868\uff1a \u8868 \u5217 \u8ba2\u5355 \u8ba2\u5355\u53f7 \u8ba2\u5355\u660e\u7ec6 \u8ba2\u5355\u53f7 \u4ea7\u54c1\u7f16\u53f7 \u8981\u5c06 \u8ba2\u5355\u660e\u7ec6 \u8868\u4e2d\u7684 \u8ba2\u5355\u53f7 \u5217\u4e0e \u8ba2\u5355 \u8868\u4e2d\u7684 \u8ba2\u5355\u53f7 \u5217\u5efa\u7acb\u5916\u952e\u5173\u7cfb\uff0c\u53ef\u4ee5\u6267\u884c\u4ee5\u4e0b\u8bed\u53e5\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-5486","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5486","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=5486"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5486\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=5486"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=5486"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=5486"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}