{"id":5537,"date":"2024-11-14T12:31:24","date_gmt":"2024-11-14T04:31:24","guid":{"rendered":"https:\/\/fwq.ai\/blog\/5537\/"},"modified":"2024-11-14T12:31:24","modified_gmt":"2024-11-14T04:31:24","slug":"mysql%e8%87%aa%e5%8a%a8%e5%a2%9e%e9%95%bf%e6%80%8e%e4%b9%88%e8%ae%be%e7%bd%ae","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/5537\/","title":{"rendered":"mysql\u81ea\u52a8\u589e\u957f\u600e\u4e48\u8bbe\u7f6e"},"content":{"rendered":"<blockquote><p>\n  mysql \u4e2d\u8bbe\u7f6e\u81ea\u52a8\u589e\u957f\u9700\u8981\u4ee5\u4e0b\u64cd\u4f5c\uff1a\u521b\u5efa\u8868\u683c\u65f6\uff0c\u6307\u5b9a auto_increment \u5173\u952e\u5b57\uff0c\u5982 create table table_name (id int not null auto_increment, name varchar(255) not null);\u8bbe\u7f6e\u521d\u59cb\u503c\u548c\u6b65\u957f\uff0c\u5982 create table table_name (id int not null auto_increment default 10, name varchar(255) not null);\u4f7f\u7528 alter\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202405\/29\/2024052908362550060.jpg\" class=\"aligncenter\" title=\"mysql\u81ea\u52a8\u589e\u957f\u600e\u4e48\u8bbe\u7f6e\u63d2\u56fe\" alt=\"mysql\u81ea\u52a8\u589e\u957f\u600e\u4e48\u8bbe\u7f6e\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u8bbe\u7f6e MySQL \u7684\u81ea\u52a8\u589e\u957f<\/strong><\/p>\n<p>\u5728 MySQL \u4e2d\uff0c\u81ea\u52a8\u589e\u957f\u53ef\u4ee5\u8ba9\u8868\u683c\u4e2d\u7684\u5217\u503c\u5728\u63d2\u5165\u65b0\u884c\u65f6\u81ea\u52a8\u589e\u52a0\u3002\u8981\u8bbe\u7f6e\u81ea\u52a8\u589e\u957f\uff0c\u9700\u8981\u6309\u7167\u4ee5\u4e0b\u6b65\u9aa4\u64cd\u4f5c\uff1a<\/p>\n<p><strong>1. \u521b\u5efa\u8868\u683c<\/strong><\/p>\n<pre>CREATE TABLE table_name (\n  id INT NOT NULL AUTO_INCREMENT,\n  name VARCHAR(255) NOT NULL\n);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4e0a\u8ff0\u8bed\u53e5\u4e2d\uff0cid \u5217\u88ab\u6307\u5b9a\u4e3a\u81ea\u52a8\u589e\u957f\u5217\uff0c\u5176\u6570\u636e\u7c7b\u578b\u4e3a\u6574\u6570 (INT)\uff0c\u4e14\u4e0d\u80fd\u4e3a\u7a7a (NOT NULL)\u3002AUTO_INCREMENT \u5173\u952e\u5b57\u8868\u793a\u8be5\u5217\u5728\u63d2\u5165\u65b0\u884c\u65f6\u5c06\u81ea\u52a8\u589e\u52a0\u3002<\/p>\n<p><strong>2. \u8bbe\u7f6e\u521d\u59cb\u503c\u548c\u6b65\u957f\uff08\u53ef\u9009\uff09<\/strong><\/p>\n<p>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u81ea\u52a8\u589e\u957f\u5217\u4f1a\u4ece 1 \u5f00\u59cb\u9012\u589e\u3002\u53ef\u4ee5\u901a\u8fc7 DEFAULT \u548c AUTO_INCREMENT \u5b50\u53e5\u6765\u8bbe\u7f6e\u521d\u59cb\u503c\u548c\u6b65\u957f\uff1a<\/p>\n<pre>CREATE TABLE table_name (\n  id INT NOT NULL AUTO_INCREMENT DEFAULT 10,\n  name VARCHAR(255) NOT NULL\n);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4e0a\u8ff0\u8bed\u53e5\u5c06 id \u5217\u7684\u521d\u59cb\u503c\u8bbe\u7f6e\u4e3a 10\uff0c\u5e76\u4e14\u6bcf\u6b21\u63d2\u5165\u65b0\u884c\u65f6\u9012\u589e 1\u3002<\/p>\n<p><strong>3. \u66f4\u6539\u81ea\u52a8\u589e\u957f\u5217<\/strong><\/p>\n<p>\u5982\u679c\u9700\u8981\u66f4\u6539\u73b0\u6709\u81ea\u52a8\u589e\u957f\u5217\u7684\u8bbe\u7f6e\uff0c\u53ef\u4ee5\u4f7f\u7528 ALTER TABLE \u8bed\u53e5\uff1a<\/p>\n<pre>ALTER TABLE table_name MODIFY COLUMN id INT NOT NULL AUTO_INCREMENT DEFAULT 20,<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4e0a\u8ff0\u8bed\u53e5\u5c06 id \u5217\u7684\u521d\u59cb\u503c\u66f4\u6539\u4e3a 20\u3002<\/p>\n<p><strong>4. \u7981\u7528\u81ea\u52a8\u589e\u957f<\/strong><\/p>\n<p>\u8981\u7981\u7528\u81ea\u52a8\u589e\u957f\uff0c\u53ef\u4ee5\u5c06 AUTO_INCREMENT \u5173\u952e\u5b57\u4ece\u5217\u5b9a\u4e49\u4e2d\u5220\u9664\uff1a<\/p>\n<pre>ALTER TABLE table_name MODIFY COLUMN id INT NOT NULL,<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u7981\u7528\u81ea\u52a8\u589e\u957f\u540e\uff0c\u63d2\u5165\u65b0\u884c\u65f6\u5fc5\u987b\u660e\u786e\u6307\u5b9a id \u5217\u7684\u503c\u3002<\/p>\n<p><strong>5. \u67e5\u770b\u81ea\u52a8\u589e\u957f\u8bbe\u7f6e<\/strong><\/p>\n<p>\u53ef\u4ee5\u4f7f\u7528 SHOW COLUMNS \u547d\u4ee4\u6765\u67e5\u770b\u81ea\u52a8\u589e\u957f\u5217\u7684\u8bbe\u7f6e\uff1a<\/p>\n<pre>SHOW COLUMNS FROM table_name;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u81ea\u52a8\u589e\u957f\u600e\u4e48\u8bbe\u7f6e\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>mysql \u4e2d\u8bbe\u7f6e\u81ea\u52a8\u589e\u957f\u9700\u8981\u4ee5\u4e0b\u64cd\u4f5c\uff1a\u521b\u5efa\u8868\u683c\u65f6\uff0c\u6307\u5b9a auto_increment \u5173\u952e\u5b57\uff0c\u5982 create table table_name (id int not null auto_increment, name varchar(255) not null);\u8bbe\u7f6e\u521d\u59cb\u503c\u548c\u6b65\u957f\uff0c\u5982 create table table_name (id int not null auto_increment default 10, name varchar(255) not null);\u4f7f\u7528 alter \u5982\u4f55\u8bbe\u7f6e MySQL \u7684\u81ea\u52a8\u589e\u957f \u5728 MySQL \u4e2d\uff0c\u81ea\u52a8\u589e\u957f\u53ef\u4ee5\u8ba9\u8868\u683c\u4e2d\u7684\u5217\u503c\u5728\u63d2\u5165\u65b0\u884c\u65f6\u81ea\u52a8\u589e\u52a0\u3002\u8981\u8bbe\u7f6e\u81ea\u52a8\u589e\u957f\uff0c\u9700\u8981\u6309\u7167\u4ee5\u4e0b\u6b65\u9aa4\u64cd\u4f5c\uff1a 1. \u521b\u5efa\u8868\u683c CREATE TABLE table_name ( id INT NOT NULL AUTO_INCREMENT, name VARCHAR(255) NOT NULL ); \u767b\u5f55\u540e\u590d\u5236 \u4e0a\u8ff0\u8bed\u53e5\u4e2d\uff0cid [&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-5537","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5537","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=5537"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5537\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=5537"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=5537"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=5537"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}