{"id":5907,"date":"2024-11-14T10:37:51","date_gmt":"2024-11-14T02:37:51","guid":{"rendered":"https:\/\/fwq.ai\/blog\/5907\/"},"modified":"2024-11-14T10:37:51","modified_gmt":"2024-11-14T02:37:51","slug":"sql%e6%80%8e%e4%b9%88%e6%b7%bb%e5%8a%a0%e4%b8%bb%e9%94%ae%e7%ba%a6%e6%9d%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/5907\/","title":{"rendered":"sql\u600e\u4e48\u6dfb\u52a0\u4e3b\u952e\u7ea6\u675f"},"content":{"rendered":"<blockquote><p>\n  \u901a\u8fc7 sql \u6dfb\u52a0\u4e3b\u952e\u7ea6\u675f\u53ef\u786e\u4fdd\u6570\u636e\u7684\u5b8c\u6574\u6027\u548c\u4e00\u81f4\u6027\u3002\u6b65\u9aa4\u5305\u62ec\uff1a1. \u786e\u5b9a\u4e3b\u952e\u5217\uff0c\u5fc5\u987b\u552f\u4e00\u4e14\u4e0d\u53ef\u4e3a\u7a7a\uff1b2. \u4f7f\u7528 alter table table_name add primary key (column_name1, column_name2, &#8230;) \u8bed\u53e5\u6dfb\u52a0\u7ea6\u675f\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202406\/03\/2024060322070238927.jpg\" class=\"aligncenter\" title=\"sql\u600e\u4e48\u6dfb\u52a0\u4e3b\u952e\u7ea6\u675f\u63d2\u56fe\" alt=\"sql\u600e\u4e48\u6dfb\u52a0\u4e3b\u952e\u7ea6\u675f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528 SQL \u6dfb\u52a0\u4e3b\u952e\u7ea6\u675f<\/strong><\/p>\n<p>\u4e3b\u952e\u7ea6\u675f\u662f\u552f\u4e00\u6807\u8bc6\u8868\u4e2d\u6bcf\u6761\u8bb0\u5f55\u7684\u5217\u6216\u5217\u7ec4\u3002\u6dfb\u52a0\u4e3b\u952e\u7ea6\u675f\u53ef\u786e\u4fdd\u6570\u636e\u5e93\u4e2d\u7684\u6570\u636e\u5b8c\u6574\u6027\u548c\u4e00\u81f4\u6027\u3002<\/p>\n<p><strong>\u6dfb\u52a0\u4e3b\u952e\u7ea6\u675f\u7684\u6b65\u9aa4\uff1a<\/strong><\/p>\n<ol>\n<li> <strong>\u786e\u5b9a\u4e3b\u952e\u5217\uff1a<\/strong>\u9009\u62e9\u4e00\u4e2a\u6216\u591a\u4e2a\u5217\u4f5c\u4e3a\u4e3b\u952e\u3002\u4e3b\u952e\u5217\u5fc5\u987b\u5177\u6709\u552f\u4e00\u4e14\u4e0d\u53ef\u4e3a\u7a7a\u7684\u503c\u3002<\/li>\n<li> <strong>\u7f16\u5199 SQL \u8bed\u53e5\uff1a<\/strong>\u4f7f\u7528\u4ee5\u4e0b\u8bed\u6cd5\u6dfb\u52a0\u4e3b\u952e\u7ea6\u675f\uff1a<\/li>\n<\/ol>\n<pre>ALTER TABLE table_name ADD PRIMARY KEY (column_name1, column_name2, ...);<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5176\u4e2d\uff1a<\/p>\n<ul>\n<li>table_name \u662f\u60a8\u8981\u6dfb\u52a0\u4e3b\u952e\u7ea6\u675f\u7684\u8868\u540d\u3002<\/li>\n<li>column_name1, column_name2, &#8230; \u662f\u6784\u6210\u4e3b\u952e\u7684\u5217\u540d\u3002<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<p>\u5047\u8bbe\u60a8\u6709\u4e00\u4e2a\u540d\u4e3a students \u7684\u8868\uff0c\u5176\u4e2d\u5305\u542b student_id, name, age \u5217\u3002\u8981\u5c06 student_id \u5217\u8bbe\u4e3a\u4e3b\u952e\uff0c\u8bf7\u4f7f\u7528\u4ee5\u4e0b SQL \u8bed\u53e5\uff1a<\/p>\n<pre>ALTER TABLE students ADD PRIMARY KEY (student_id);<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u6ce8\u610f\u4e8b\u9879\uff1a<\/strong><\/p>\n<ul>\n<li>\u4e00\u65e6\u6dfb\u52a0\u4e86\u4e3b\u952e\u7ea6\u675f\uff0c\u65b0\u63d2\u5165\u7684\u6570\u636e\u5fc5\u987b\u5177\u6709\u552f\u4e00\u7684\u4e3b\u952e\u503c\u3002<\/li>\n<li>\u5220\u9664\u6216\u66f4\u65b0\u4e3b\u952e\u503c\u4f1a\u5bfc\u81f4\u8fdd\u53cd\u4e3b\u952e\u7ea6\u675f\u9519\u8bef\u3002<\/li>\n<li>\u53ef\u4ee5\u4f7f\u7528 DROP PRIMARY KEY \u7ea6\u675f\u4ece\u8868\u4e2d\u5220\u9664\u4e3b\u952e\u7ea6\u675f\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662fsql\u600e\u4e48\u6dfb\u52a0\u4e3b\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>\u901a\u8fc7 sql \u6dfb\u52a0\u4e3b\u952e\u7ea6\u675f\u53ef\u786e\u4fdd\u6570\u636e\u7684\u5b8c\u6574\u6027\u548c\u4e00\u81f4\u6027\u3002\u6b65\u9aa4\u5305\u62ec\uff1a1. \u786e\u5b9a\u4e3b\u952e\u5217\uff0c\u5fc5\u987b\u552f\u4e00\u4e14\u4e0d\u53ef\u4e3a\u7a7a\uff1b2. \u4f7f\u7528 alter table table_name add primary key (column_name1, column_name2, &#8230;) \u8bed\u53e5\u6dfb\u52a0\u7ea6\u675f\u3002 \u5982\u4f55\u4f7f\u7528 SQL \u6dfb\u52a0\u4e3b\u952e\u7ea6\u675f \u4e3b\u952e\u7ea6\u675f\u662f\u552f\u4e00\u6807\u8bc6\u8868\u4e2d\u6bcf\u6761\u8bb0\u5f55\u7684\u5217\u6216\u5217\u7ec4\u3002\u6dfb\u52a0\u4e3b\u952e\u7ea6\u675f\u53ef\u786e\u4fdd\u6570\u636e\u5e93\u4e2d\u7684\u6570\u636e\u5b8c\u6574\u6027\u548c\u4e00\u81f4\u6027\u3002 \u6dfb\u52a0\u4e3b\u952e\u7ea6\u675f\u7684\u6b65\u9aa4\uff1a \u786e\u5b9a\u4e3b\u952e\u5217\uff1a\u9009\u62e9\u4e00\u4e2a\u6216\u591a\u4e2a\u5217\u4f5c\u4e3a\u4e3b\u952e\u3002\u4e3b\u952e\u5217\u5fc5\u987b\u5177\u6709\u552f\u4e00\u4e14\u4e0d\u53ef\u4e3a\u7a7a\u7684\u503c\u3002 \u7f16\u5199 SQL \u8bed\u53e5\uff1a\u4f7f\u7528\u4ee5\u4e0b\u8bed\u6cd5\u6dfb\u52a0\u4e3b\u952e\u7ea6\u675f\uff1a ALTER TABLE table_name ADD PRIMARY KEY (column_name1, column_name2, &#8230;); \u767b\u5f55\u540e\u590d\u5236 \u5176\u4e2d\uff1a table_name \u662f\u60a8\u8981\u6dfb\u52a0\u4e3b\u952e\u7ea6\u675f\u7684\u8868\u540d\u3002 column_name1, column_name2, &#8230; \u662f\u6784\u6210\u4e3b\u952e\u7684\u5217\u540d\u3002 \u793a\u4f8b\uff1a \u5047\u8bbe\u60a8\u6709\u4e00\u4e2a\u540d\u4e3a students \u7684\u8868\uff0c\u5176\u4e2d\u5305\u542b student_id, name, age \u5217\u3002\u8981\u5c06 student_id \u5217\u8bbe\u4e3a\u4e3b\u952e\uff0c\u8bf7\u4f7f\u7528\u4ee5\u4e0b SQL \u8bed\u53e5\uff1a ALTER TABLE [&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-5907","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5907","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=5907"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5907\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=5907"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=5907"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=5907"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}