{"id":5167,"date":"2024-11-14T10:02:58","date_gmt":"2024-11-14T02:02:58","guid":{"rendered":"https:\/\/fwq.ai\/blog\/5167\/"},"modified":"2024-11-14T10:02:58","modified_gmt":"2024-11-14T02:02:58","slug":"mysql%e8%81%94%e5%90%88%e4%b8%bb%e9%94%ae%e6%80%8e%e4%b9%88%e7%94%a8","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/5167\/","title":{"rendered":"mysql\u8054\u5408\u4e3b\u952e\u600e\u4e48\u7528"},"content":{"rendered":"<blockquote><p>\n  \u8054\u5408\u4e3b\u952e\u662f\u5728\u4e00\u4e2a\u8868\u4e2d\u4f7f\u7528\u591a\u4e2a\u5217\u5f62\u6210\u4e00\u4e2a\u552f\u4e00\u6807\u8bc6\u7b26\uff0c\u4ece\u800c\u786e\u4fdd\u6bcf\u884c\u90fd\u662f\u552f\u4e00\u7684\u3002\u5b83\u5728\u65e0\u6cd5\u7528\u5355\u4e00\u5217\u552f\u4e00\u6807\u8bc6\u65f6\uff0c\u9700\u8981\u5feb\u901f\u67e5\u627e\u548c\u8bbf\u95ee\u591a\u4e2a\u5b57\u6bb5\uff0c\u6216\u5f3a\u5236\u7279\u5b9a\u6570\u636e\u7ec4\u5408\u65f6\u4f7f\u7528\u3002\u521b\u5efa\u8054\u5408\u4e3b\u952e\u4f7f\u7528 alter table table_name add primary key (column1, column2, &#8230;); \u8bed\u6cd5\u3002\u5b83\u7684\u4f18\u52bf\u5305\u62ec\u552f\u4e00\u6027\u3001\u67e5\u8be2\u6027\u80fd\u4f18\u5316\u548c\u91cd\u590d\u6570\u636e\u9632\u6b62\uff0c\u4f46\u7f3a\u70b9\u662f\u4e3b\u952e\u5217\u4fee\u6539\u53ef\u80fd\u5bfc\u81f4\u6570\u636e\u4e22\u5931\uff0c\u9891\u7e41\u66f4\u6539\u4e3b\u952e\u5217\u4f1a\u964d\u4f4e\u6027\u80fd\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202405\/22\/2024052218092656629.jpg\" class=\"aligncenter\" title=\"mysql\u8054\u5408\u4e3b\u952e\u600e\u4e48\u7528\u63d2\u56fe\" alt=\"mysql\u8054\u5408\u4e3b\u952e\u600e\u4e48\u7528\u63d2\u56fe\" \/><\/p>\n<p><strong>MySQL \u8054\u5408\u4e3b\u952e\u7684\u4f7f\u7528<\/strong><\/p>\n<p><strong>\u4ec0\u4e48\u662f\u8054\u5408\u4e3b\u952e\uff1f<\/strong><\/p>\n<p>\u8054\u5408\u4e3b\u952e\u662f\u5728\u4e00\u4e2a\u8868\u4e2d\u4f7f\u7528\u591a\u4e2a\u5217\u5f62\u6210\u4e00\u4e2a\u552f\u4e00\u6807\u8bc6\u7b26\uff0c\u4ece\u800c\u786e\u4fdd\u8868\u7684\u6bcf\u884c\u90fd\u662f\u72ec\u4e00\u65e0\u4e8c\u7684\u3002\u5b83\u4e0e\u5355\u4e3b\u952e\u7c7b\u4f3c\uff0c\u4f46\u6d89\u53ca\u591a\u4e2a\u5217\u3002<\/p>\n<p><strong>\u4f55\u65f6\u4f7f\u7528\u8054\u5408\u4e3b\u952e\uff1f<\/strong><\/p>\n<p>\u8054\u5408\u4e3b\u952e\u5728\u4ee5\u4e0b\u60c5\u51b5\u4e0b\u4f7f\u7528\uff1a<\/p>\n<ul>\n<li>\u5f53\u4f7f\u7528\u5355\u4e3b\u952e\u65e0\u6cd5\u552f\u4e00\u6807\u8bc6\u884c\u65f6\u3002<\/li>\n<li>\u5f53\u9700\u8981\u6839\u636e\u591a\u4e2a\u5b57\u6bb5\u8fdb\u884c\u5feb\u901f\u67e5\u627e\u548c\u8bbf\u95ee\u65f6\u3002<\/li>\n<li>\u5f53\u5e0c\u671b\u5f3a\u5236\u8868\u4e2d\u5b58\u5728\u7279\u5b9a\u7ec4\u5408\u7684\u6570\u636e\u65f6\u3002<\/li>\n<\/ul>\n<p><strong>\u5982\u4f55\u521b\u5efa\u8054\u5408\u4e3b\u952e\uff1f<\/strong><\/p>\n<p>\u5728 MySQL \u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u8bed\u6cd5\u521b\u5efa\u8054\u5408\u4e3b\u952e\uff1a<\/p>\n<pre>ALTER TABLE table_name ADD PRIMARY KEY (column1, column2, ...);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5176\u4e2d\uff0ctable_name \u662f\u8868\u7684\u540d\u79f0\uff0ccolumn1\u3001column2 \u7b49\u662f\u7ec4\u6210\u8054\u5408\u4e3b\u952e\u7684\u5217\u540d\u3002<\/p>\n<p><strong>\u793a\u4f8b<\/strong><\/p>\n<p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u540d\u4e3a Students \u7684\u8868\uff0c\u5176\u4e2d\u5305\u542b\u4ee5\u4e0b\u5217\uff1a<\/p>\n<ul>\n<li>student_id\uff08\u5355\u4e3b\u952e\uff09<\/li>\n<li>first_name<\/li>\n<li>last_name<\/li>\n<\/ul>\n<p>\u4e3a\u4e86\u521b\u5efa\u4e00\u4e2a\u4f7f\u7528 first_name \u548c last_name \u7684\u8054\u5408\u4e3b\u952e\uff0c\u6211\u4eec\u53ef\u4ee5\u6267\u884c\u4ee5\u4e0b\u67e5\u8be2\uff1a<\/p>\n<pre>ALTER TABLE Students ADD PRIMARY KEY (first_name, last_name);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8fd9\u6837\u4e00\u6765\uff0cfirst_name \u548c last_name \u5217\u7684\u7ec4\u5408\u5c06\u5f62\u6210\u4e00\u4e2a\u552f\u4e00\u6807\u8bc6\u7b26\uff0c\u786e\u4fdd\u8868\u4e2d\u7684\u6bcf\u4f4d\u5b66\u751f\u90fd\u662f\u72ec\u4e00\u65e0\u4e8c\u7684\u3002<\/p>\n<p><strong>\u4f18\u70b9\u548c\u7f3a\u70b9<\/strong><\/p>\n<p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<ul>\n<li>\u786e\u4fdd\u6570\u636e\u7684\u552f\u4e00\u6027\u548c\u5b8c\u6574\u6027\u3002<\/li>\n<li>\u4f18\u5316\u57fa\u4e8e\u591a\u4e2a\u5217\u7684\u67e5\u8be2\u6027\u80fd\u3002<\/li>\n<li>\u9632\u6b62\u91cd\u590d\u6570\u636e\u7684\u63d2\u5165\u3002<\/li>\n<\/ul>\n<p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<ul>\n<li>\u5728\u4e3b\u952e\u4e2d\u6dfb\u52a0\u6216\u5220\u9664\u5217\u53ef\u80fd\u5bfc\u81f4\u3002<\/li>\n<li>\u5982\u679c\u8054\u5408\u4e3b\u952e\u7684\u5217\u7ecf\u5e38\u66f4\u6539\uff0c\u53ef\u80fd\u4f1a\u964d\u4f4e\u63d2\u5165\u548c\u66f4\u65b0\u6027\u80fd\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662f\u8054\u5408\u4e3b\u952e\u600e\u4e48\u7528\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>\u8054\u5408\u4e3b\u952e\u662f\u5728\u4e00\u4e2a\u8868\u4e2d\u4f7f\u7528\u591a\u4e2a\u5217\u5f62\u6210\u4e00\u4e2a\u552f\u4e00\u6807\u8bc6\u7b26\uff0c\u4ece\u800c\u786e\u4fdd\u6bcf\u884c\u90fd\u662f\u552f\u4e00\u7684\u3002\u5b83\u5728\u65e0\u6cd5\u7528\u5355\u4e00\u5217\u552f\u4e00\u6807\u8bc6\u65f6\uff0c\u9700\u8981\u5feb\u901f\u67e5\u627e\u548c\u8bbf\u95ee\u591a\u4e2a\u5b57\u6bb5\uff0c\u6216\u5f3a\u5236\u7279\u5b9a\u6570\u636e\u7ec4\u5408\u65f6\u4f7f\u7528\u3002\u521b\u5efa\u8054\u5408\u4e3b\u952e\u4f7f\u7528 alter table table_name add primary key (column1, column2, &#8230;); \u8bed\u6cd5\u3002\u5b83\u7684\u4f18\u52bf\u5305\u62ec\u552f\u4e00\u6027\u3001\u67e5\u8be2\u6027\u80fd\u4f18\u5316\u548c\u91cd\u590d\u6570\u636e\u9632\u6b62\uff0c\u4f46\u7f3a\u70b9\u662f\u4e3b\u952e\u5217\u4fee\u6539\u53ef\u80fd\u5bfc\u81f4\u6570\u636e\u4e22\u5931\uff0c\u9891\u7e41\u66f4\u6539\u4e3b\u952e\u5217\u4f1a\u964d\u4f4e\u6027\u80fd\u3002 MySQL \u8054\u5408\u4e3b\u952e\u7684\u4f7f\u7528 \u4ec0\u4e48\u662f\u8054\u5408\u4e3b\u952e\uff1f \u8054\u5408\u4e3b\u952e\u662f\u5728\u4e00\u4e2a\u8868\u4e2d\u4f7f\u7528\u591a\u4e2a\u5217\u5f62\u6210\u4e00\u4e2a\u552f\u4e00\u6807\u8bc6\u7b26\uff0c\u4ece\u800c\u786e\u4fdd\u8868\u7684\u6bcf\u884c\u90fd\u662f\u72ec\u4e00\u65e0\u4e8c\u7684\u3002\u5b83\u4e0e\u5355\u4e3b\u952e\u7c7b\u4f3c\uff0c\u4f46\u6d89\u53ca\u591a\u4e2a\u5217\u3002 \u4f55\u65f6\u4f7f\u7528\u8054\u5408\u4e3b\u952e\uff1f \u8054\u5408\u4e3b\u952e\u5728\u4ee5\u4e0b\u60c5\u51b5\u4e0b\u4f7f\u7528\uff1a \u5f53\u4f7f\u7528\u5355\u4e3b\u952e\u65e0\u6cd5\u552f\u4e00\u6807\u8bc6\u884c\u65f6\u3002 \u5f53\u9700\u8981\u6839\u636e\u591a\u4e2a\u5b57\u6bb5\u8fdb\u884c\u5feb\u901f\u67e5\u627e\u548c\u8bbf\u95ee\u65f6\u3002 \u5f53\u5e0c\u671b\u5f3a\u5236\u8868\u4e2d\u5b58\u5728\u7279\u5b9a\u7ec4\u5408\u7684\u6570\u636e\u65f6\u3002 \u5982\u4f55\u521b\u5efa\u8054\u5408\u4e3b\u952e\uff1f \u5728 MySQL \u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u8bed\u6cd5\u521b\u5efa\u8054\u5408\u4e3b\u952e\uff1a ALTER TABLE table_name ADD PRIMARY KEY (column1, column2, &#8230;); \u767b\u5f55\u540e\u590d\u5236 \u5176\u4e2d\uff0ctable_name \u662f\u8868\u7684\u540d\u79f0\uff0ccolumn1\u3001column2 \u7b49\u662f\u7ec4\u6210\u8054\u5408\u4e3b\u952e\u7684\u5217\u540d\u3002 \u793a\u4f8b \u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u540d\u4e3a Students \u7684\u8868\uff0c\u5176\u4e2d\u5305\u542b\u4ee5\u4e0b\u5217\uff1a student_id\uff08\u5355\u4e3b\u952e\uff09 first_name last_name \u4e3a\u4e86\u521b\u5efa\u4e00\u4e2a\u4f7f\u7528 first_name \u548c last_name \u7684\u8054\u5408\u4e3b\u952e\uff0c\u6211\u4eec\u53ef\u4ee5\u6267\u884c\u4ee5\u4e0b\u67e5\u8be2\uff1a ALTER TABLE Students ADD PRIMARY KEY [&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-5167","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5167","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=5167"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5167\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=5167"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=5167"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=5167"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}