{"id":5432,"date":"2024-11-14T12:38:47","date_gmt":"2024-11-14T04:38:47","guid":{"rendered":"https:\/\/fwq.ai\/blog\/5432\/"},"modified":"2024-11-14T12:38:47","modified_gmt":"2024-11-14T04:38:47","slug":"mysql%e8%ae%be%e7%bd%ae%e5%a4%96%e9%94%ae%e6%80%8e%e4%b9%88%e5%86%99","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/5432\/","title":{"rendered":"mysql\u8bbe\u7f6e\u5916\u952e\u600e\u4e48\u5199"},"content":{"rendered":"<blockquote><p>\n  \u5728 mysql \u4e2d\u901a\u8fc7\u5916\u952e\u8bbe\u7f6e\u6570\u636e\u5b8c\u6574\u6027\uff0c\u786e\u4fdd\u7236\u5b50\u8868\u5f15\u7528\u5173\u7cfb\u3002\u521b\u5efa\u7236\u8868\u540e\uff0c\u521b\u5efa\u5b50\u8868\u5e76\u6dfb\u52a0\u5916\u952e\u5217\uff0c\u8be5\u5217\u5f15\u7528\u7236\u8868\u4e2d\u7684\u4e3b\u952e\u3002\u4f18\u52bf\u5305\u62ec\u6570\u636e\u5b8c\u6574\u6027\u3001\u7ea7\u8054\u5220\u9664\/\u66f4\u65b0\u64cd\u4f5c\u548c\u67e5\u8be2\u4f18\u5316\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202405\/28\/2024052812482434826.jpg\" class=\"aligncenter\" title=\"mysql\u8bbe\u7f6e\u5916\u952e\u600e\u4e48\u5199\u63d2\u56fe\" alt=\"mysql\u8bbe\u7f6e\u5916\u952e\u600e\u4e48\u5199\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u8bbe\u7f6e MySQL \u5916\u952e<\/strong><\/p>\n<p>\u5728 MySQL \u4e2d\u8bbe\u7f6e\u5916\u952e\u53ef\u4ee5\u786e\u4fdd\u6570\u636e\u5b8c\u6574\u6027\uff0c\u5b83\u5f3a\u5236\u6267\u884c\u7236\u5b50\u8868\u4e4b\u95f4\u7684\u5f15\u7528\u5173\u7cfb\u3002<\/p>\n<p><strong>\u8bed\u6cd5<\/strong><\/p>\n<pre>ALTER TABLE \u5b50\u8868 ADD FOREIGN KEY (\u5b50\u8868\u5217) REFERENCES \u7236\u8868 (\u7236\u8868\u5217)<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u6b65\u9aa4<\/strong><\/p>\n<ol>\n<li>\n<p><strong>\u521b\u5efa\u7236\u8868<\/strong><\/p>\n<p>\u9996\u5148\uff0c\u521b\u5efa\u5305\u542b\u7236\u6570\u636e\u7684\u7236\u8868\uff1a<\/p>\n<pre>CREATE TABLE \u7236\u8868 (\n  id INT NOT NULL AUTO_INCREMENT,\n  name VARCHAR(255) NOT NULL,\n  PRIMARY KEY (id)\n);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/li>\n<li>\n<p><strong>\u521b\u5efa\u5b50\u8868<\/strong><\/p>\n<p>\u63a5\u4e0b\u6765\uff0c\u521b\u5efa\u5305\u542b\u5b50\u6570\u636e\u7684\u5b50\u8868\uff0c\u5e76\u6dfb\u52a0\u5916\u952e\u5217\uff1a<\/p>\n<pre>CREATE TABLE \u5b50\u8868 (\n  id INT NOT NULL AUTO_INCREMENT,\n  name VARCHAR(255) NOT NULL,\n  \u7236\u8868_id INT NOT NULL,\n  FOREIGN KEY (\u7236\u8868_id) REFERENCES \u7236\u8868 (id),\n  PRIMARY KEY (id)\n);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/li>\n<li>\n<p><strong>\u63d2\u5165\u6570\u636e<\/strong><\/p>\n<p>\u63d2\u5165\u6570\u636e\u65f6\uff0c\u786e\u4fdd\u5b50\u8868\u7684 \u7236\u8868_id \u5217\u503c\u5f15\u7528\u7236\u8868\u4e2d\u7684\u73b0\u6709\u8bb0\u5f55\uff1a<\/p>\n<pre>INSERT INTO \u7236\u8868 (name) VALUES ('\u7236\u8868\u8bb0\u5f55 1');\nINSERT INTO \u5b50\u8868 (name, \u7236\u8868_id) VALUES ('\u5b50\u8868\u8bb0\u5f55 1', 1);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/li>\n<\/ol>\n<p><strong>\u4f18\u52bf<\/strong><\/p>\n<p>\u8bbe\u7f6e\u5916\u952e\u6709\u4ee5\u4e0b\u4f18\u52bf\uff1a<\/p>\n<ul>\n<li> <strong>\u6570\u636e\u5b8c\u6574\u6027\uff1a<\/strong>\u786e\u4fdd\u5b50\u8868\u4e2d\u7684\u6570\u636e\u4e0e\u7236\u8868\u4e2d\u7684\u6570\u636e\u4e00\u81f4\u3002<\/li>\n<li> <strong>\u7ea7\u8054\u64cd\u4f5c\uff1a<\/strong>\u5f53\u7236\u8868\u8bb0\u5f55\u88ab\u5220\u9664\u6216\u66f4\u65b0\u65f6\uff0c\u7ea7\u8054\u64cd\u4f5c\u5141\u8bb8\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>\u901a\u8fc7\u4f7f\u7528\u5916\u952e\u7d22\u5f15\uff0c\u53ef\u4ee5\u4f18\u5316\u6d89\u53ca\u5b50\u8868\u548c\u7236\u8868\u8054\u63a5\u7684\u67e5\u8be2\u3002<\/li>\n<\/ul>\n<p><strong>\u63d0\u793a<\/strong><\/p>\n<ul>\n<li>\u7236\u8868\u4e2d\u7684\u5217\u7c7b\u578b\u5e94\u4e0e\u5b50\u8868\u4e2d\u7684\u5916\u952e\u5217\u7c7b\u578b\u5339\u914d\u3002<\/li>\n<li>\u7236\u8868\u4e2d\u7684\u5217\u5e94\u5177\u6709\u552f\u4e00\u7ea6\u675f\u6216\u4e3b\u952e\u3002<\/li>\n<li>\u5916\u952e\u5217\u5728\u5b50\u8868\u4e2d\u901a\u5e38\u975e\u7a7a\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662f\u8bbe\u7f6e\u5916\u952e\u600e\u4e48\u5199\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>\u5728 mysql \u4e2d\u901a\u8fc7\u5916\u952e\u8bbe\u7f6e\u6570\u636e\u5b8c\u6574\u6027\uff0c\u786e\u4fdd\u7236\u5b50\u8868\u5f15\u7528\u5173\u7cfb\u3002\u521b\u5efa\u7236\u8868\u540e\uff0c\u521b\u5efa\u5b50\u8868\u5e76\u6dfb\u52a0\u5916\u952e\u5217\uff0c\u8be5\u5217\u5f15\u7528\u7236\u8868\u4e2d\u7684\u4e3b\u952e\u3002\u4f18\u52bf\u5305\u62ec\u6570\u636e\u5b8c\u6574\u6027\u3001\u7ea7\u8054\u5220\u9664\/\u66f4\u65b0\u64cd\u4f5c\u548c\u67e5\u8be2\u4f18\u5316\u3002 \u5982\u4f55\u8bbe\u7f6e MySQL \u5916\u952e \u5728 MySQL \u4e2d\u8bbe\u7f6e\u5916\u952e\u53ef\u4ee5\u786e\u4fdd\u6570\u636e\u5b8c\u6574\u6027\uff0c\u5b83\u5f3a\u5236\u6267\u884c\u7236\u5b50\u8868\u4e4b\u95f4\u7684\u5f15\u7528\u5173\u7cfb\u3002 \u8bed\u6cd5 ALTER TABLE \u5b50\u8868 ADD FOREIGN KEY (\u5b50\u8868\u5217) REFERENCES \u7236\u8868 (\u7236\u8868\u5217) \u767b\u5f55\u540e\u590d\u5236 \u6b65\u9aa4 \u521b\u5efa\u7236\u8868 \u9996\u5148\uff0c\u521b\u5efa\u5305\u542b\u7236\u6570\u636e\u7684\u7236\u8868\uff1a CREATE TABLE \u7236\u8868 ( id INT NOT NULL AUTO_INCREMENT, name VARCHAR(255) NOT NULL, PRIMARY KEY (id) ); \u767b\u5f55\u540e\u590d\u5236 \u521b\u5efa\u5b50\u8868 \u63a5\u4e0b\u6765\uff0c\u521b\u5efa\u5305\u542b\u5b50\u6570\u636e\u7684\u5b50\u8868\uff0c\u5e76\u6dfb\u52a0\u5916\u952e\u5217\uff1a CREATE TABLE \u5b50\u8868 ( id INT NOT NULL AUTO_INCREMENT, name VARCHAR(255) [&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-5432","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5432","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=5432"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5432\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=5432"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=5432"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=5432"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}