{"id":1800,"date":"2025-02-18T23:00:43","date_gmt":"2025-02-18T15:00:43","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=1800"},"modified":"2025-02-18T23:00:43","modified_gmt":"2025-02-18T15:00:43","slug":"mysql-%e5%ba%8f%e5%88%97%e4%bd%bf%e7%94%a8%ef%bc%88auto_increment%ef%bc%89","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/02\/18\/mysql-%e5%ba%8f%e5%88%97%e4%bd%bf%e7%94%a8%ef%bc%88auto_increment%ef%bc%89\/","title":{"rendered":"MySQL \u5e8f\u5217\u4f7f\u7528\uff08AUTO_INCREMENT\uff09"},"content":{"rendered":"\n<p>\u5728 MySQL \u4e2d\uff0c<code>AUTO_INCREMENT<\/code> \u662f\u4e00\u79cd\u7528\u4e8e\u81ea\u52a8\u751f\u6210\u552f\u4e00\u503c\u7684\u673a\u5236\uff0c\u901a\u5e38\u7528\u4e8e\u4e3b\u952e\u5b57\u6bb5\u3002\u5b83\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u5e8f\u5217\u751f\u6210\u65b9\u5f0f\uff0c\u7279\u522b\u9002\u7528\u4e8e\u90a3\u4e9b\u4e0d\u9700\u8981\u624b\u52a8\u6307\u5b9a\u552f\u4e00\u6807\u8bc6\u7b26\u7684\u573a\u666f\uff0c\u50cf\u662f\u8868\u4e2d\u7684 ID \u5217\u3002\u901a\u8fc7 <code>AUTO_INCREMENT<\/code>\uff0c\u6bcf\u5f53\u63d2\u5165\u4e00\u884c\u6570\u636e\u65f6\uff0cMySQL \u4f1a\u81ea\u52a8\u4e3a\u8be5\u5217\u751f\u6210\u4e00\u4e2a\u9012\u589e\u7684\u503c\u3002\u8fd9\u4e2a\u503c\u901a\u5e38\u7528\u4e8e\u8868\u7684\u4e3b\u952e\uff0c\u4ee5\u4fdd\u8bc1\u6bcf\u4e00\u884c\u90fd\u6709\u552f\u4e00\u7684\u6807\u8bc6\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. <strong>\u4f7f\u7528 <code>AUTO_INCREMENT<\/code> \u521b\u5efa\u8868<\/strong><\/h3>\n\n\n\n<p>\u8981\u4f7f\u7528 <code>AUTO_INCREMENT<\/code>\uff0c\u9996\u5148\u9700\u8981\u5728\u8868\u4e2d\u6307\u5b9a\u4e00\u4e2a\u5217\u4f5c\u4e3a\u81ea\u52a8\u9012\u589e\u5217\u3002\u8fd9\u4e2a\u5217\u901a\u5e38\u662f\u4e3b\u952e\uff0c\u5e76\u4e14\u5fc5\u987b\u662f\u6570\u503c\u7c7b\u578b\uff08\u5982 <code>INT<\/code>\uff09\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b\uff1a\u521b\u5efa\u4e00\u4e2a\u5e26 <code>AUTO_INCREMENT<\/code> \u7684\u8868<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>CREATE TABLE employees (\n    emp_id INT AUTO_INCREMENT,\n    emp_name VARCHAR(100),\n    emp_salary DECIMAL(10, 2),\n    PRIMARY KEY (emp_id)\n);<\/code><\/pre>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>emp_id<\/code> \u5217\u662f\u4e3b\u952e\uff0c\u5e76\u4e14\u4f7f\u7528 <code>AUTO_INCREMENT<\/code> \u81ea\u52a8\u751f\u6210\u552f\u4e00\u7684\u5458\u5de5 ID\u3002\u5f53\u4f60\u63d2\u5165\u6570\u636e\u65f6\uff0c\u4e0d\u9700\u8981\u624b\u52a8\u4e3a <code>emp_id<\/code> \u6307\u5b9a\u503c\uff0cMySQL \u4f1a\u81ea\u52a8\u4e3a\u5b83\u5206\u914d\u4e00\u4e2a\u9012\u589e\u7684\u6574\u6570\u503c\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2. <strong>\u63d2\u5165\u6570\u636e\u65f6\u4f7f\u7528 <code>AUTO_INCREMENT<\/code><\/strong><\/h3>\n\n\n\n<p>\u5f53\u4f60\u63d2\u5165\u6570\u636e\u65f6\uff0c\u53ef\u4ee5\u7701\u7565 <code>AUTO_INCREMENT<\/code> \u5217\u7684\u503c\uff0cMySQL \u4f1a\u81ea\u52a8\u4e3a\u5b83\u5206\u914d\u4e00\u4e2a\u503c\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b\uff1a\u63d2\u5165\u6570\u636e<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>INSERT INTO employees (emp_name, emp_salary) \nVALUES ('John Doe', 50000.00);<\/code><\/pre>\n\n\n\n<p>\u8fd9\u91cc\uff0c\u6211\u4eec\u53ea\u63d2\u5165\u4e86 <code>emp_name<\/code> \u548c <code>emp_salary<\/code> \u7684\u503c\uff0c<code>emp_id<\/code> \u4f1a\u81ea\u52a8\u751f\u6210\u4e00\u4e2a\u552f\u4e00\u7684\u9012\u589e\u503c\u3002\u5047\u8bbe\u8fd9\u662f\u8868\u4e2d\u7684\u7b2c\u4e00\u884c\u6570\u636e\uff0c<code>emp_id<\/code> \u4f1a\u88ab\u8d4b\u503c\u4e3a <code>1<\/code>\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u63d2\u5165\u591a\u884c\u6570\u636e<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>INSERT INTO employees (emp_name, emp_salary) \nVALUES \n('Jane Smith', 55000.00),\n('James Brown', 60000.00),\n('Emily White', 62000.00);<\/code><\/pre>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u63d2\u5165\u64cd\u4f5c\u4e2d\uff0c<code>emp_id<\/code> \u4f1a\u4f9d\u6b21\u88ab\u81ea\u52a8\u8d4b\u503c\u4e3a 2\u30013 \u548c 4\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3. <strong>\u83b7\u53d6 <code>AUTO_INCREMENT<\/code> \u7684\u5f53\u524d\u503c<\/strong><\/h3>\n\n\n\n<p>\u53ef\u4ee5\u4f7f\u7528 <code>LAST_INSERT_ID()<\/code> \u51fd\u6570\u6765\u83b7\u53d6\u6700\u540e\u63d2\u5165\u7684 <code>AUTO_INCREMENT<\/code> \u503c\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b\uff1a\u83b7\u53d6\u6700\u540e\u63d2\u5165\u7684 ID<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT LAST_INSERT_ID();<\/code><\/pre>\n\n\n\n<p>\u8fd9\u4e2a\u51fd\u6570\u4f1a\u8fd4\u56de\u6700\u8fd1\u4e00\u6b21\u63d2\u5165\u64cd\u4f5c\u4e2d\u81ea\u52a8\u751f\u6210\u7684 <code>AUTO_INCREMENT<\/code> \u503c\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4. <strong>\u91cd\u7f6e <code>AUTO_INCREMENT<\/code> \u503c<\/strong><\/h3>\n\n\n\n<p>\u5982\u679c\u9700\u8981\u4fee\u6539 <code>AUTO_INCREMENT<\/code> \u7684\u8d77\u59cb\u503c\u6216\u91cd\u7f6e\u5b83\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>ALTER TABLE<\/code> \u6765\u8bbe\u7f6e\u65b0\u7684\u8d77\u59cb\u503c\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b\uff1a\u4fee\u6539 <code>AUTO_INCREMENT<\/code> \u7684\u8d77\u59cb\u503c<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>ALTER TABLE employees AUTO_INCREMENT = 100;<\/code><\/pre>\n\n\n\n<p>\u8fd9\u5c06\u4f7f\u5f97\u4e0b\u6b21\u63d2\u5165\u6570\u636e\u65f6\uff0c<code>emp_id<\/code> \u4ece 100 \u5f00\u59cb\uff0c\u800c\u4e0d\u662f\u9ed8\u8ba4\u7684 1\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b\uff1a\u91cd\u7f6e <code>AUTO_INCREMENT<\/code> \u7684\u503c\u4e3a\u6700\u5c0f\u503c<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>ALTER TABLE employees AUTO_INCREMENT = 1;<\/code><\/pre>\n\n\n\n<p>\u8fd9\u5c06\u91cd\u7f6e <code>AUTO_INCREMENT<\/code> \u4e3a 1\uff0c\u4f46\u9700\u8981\u6ce8\u610f\uff0c\u53ea\u6709\u5728\u8868\u4e2d\u6ca1\u6709\u6570\u636e\u6216\u6570\u636e\u5df2\u88ab\u5220\u9664\u65f6\uff0c\u624d\u80fd\u91cd\u7f6e\u4e3a 1\u3002\u5982\u679c\u8868\u4e2d\u5df2\u6709\u6570\u636e\uff0c<code>AUTO_INCREMENT<\/code> \u7684\u8d77\u59cb\u503c\u5c06\u59cb\u7ec8\u5927\u4e8e\u5f53\u524d\u8868\u4e2d\u7684\u6700\u5927\u503c\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">5. <strong>\u4f7f\u7528 <code>AUTO_INCREMENT<\/code> \u7684\u6ce8\u610f\u4e8b\u9879<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u6bcf\u4e2a\u8868\u53ea\u80fd\u6709\u4e00\u4e2a <code>AUTO_INCREMENT<\/code> \u5217<\/strong>\uff0c\u800c\u4e14\u8be5\u5217\u5fc5\u987b\u662f\u7d22\u5f15\u7684\u4e00\u90e8\u5206\uff0c\u901a\u5e38\u662f\u4e3b\u952e\u3002<\/li>\n\n\n\n<li><code>AUTO_INCREMENT<\/code> \u503c\u662f\u5168\u5c40\u9012\u589e\u7684\uff0c\u610f\u5473\u7740\u5982\u679c\u5728\u8868\u4e2d\u5220\u9664\u4e86\u4e00\u4e9b\u884c\uff0c<code>AUTO_INCREMENT<\/code> \u7684\u503c\u4e0d\u4f1a\u56de\u9000\u3002\u4f8b\u5982\uff0c\u63d2\u5165 5 \u884c\u540e\u5220\u9664\u7b2c 3 \u884c\uff0c\u65b0\u7684 <code>AUTO_INCREMENT<\/code> \u503c\u4f9d\u7136\u4f1a\u6bd4 5 \u5927\u3002<\/li>\n\n\n\n<li><code>AUTO_INCREMENT<\/code> \u7684\u503c\u4f1a\u81ea\u52a8\u9012\u589e\uff0c\u4f46\u4f60\u4e5f\u53ef\u4ee5\u901a\u8fc7\u663e\u5f0f\u5730\u4e3a <code>AUTO_INCREMENT<\/code> \u5217\u8d4b\u503c\u6765\u8df3\u8fc7\u67d0\u4e9b\u503c\u3002<\/li>\n\n\n\n<li>\u5982\u679c\u4f60\u4e3a <code>AUTO_INCREMENT<\/code> \u5217\u63d2\u5165\u4e00\u4e2a\u660e\u786e\u7684\u503c\uff0cMySQL \u4f1a\u7ee7\u7eed\u9012\u589e\u3002\u4f8b\u5982\uff0c\u5982\u679c\u5f53\u524d <code>AUTO_INCREMENT<\/code> \u503c\u4e3a 5\uff0c\u800c\u4f60\u624b\u52a8\u63d2\u5165\u4e00\u4e2a\u503c\u4e3a 10 \u7684\u8bb0\u5f55\uff0c\u4e0b\u4e00\u6b21\u63d2\u5165\u65f6\uff0c<code>AUTO_INCREMENT<\/code> \u4f1a\u7ee7\u7eed\u4ece 11 \u5f00\u59cb\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">6. <strong>\u5b9e\u4f8b\uff1a\u63d2\u5165\u5e76\u67e5\u8be2\u6570\u636e<\/strong><\/h3>\n\n\n\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5b8c\u6574\u7684\u4f8b\u5b50\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528 <code>AUTO_INCREMENT<\/code> \u5217\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>-- \u521b\u5efa employees \u8868\nCREATE TABLE employees (\n    emp_id INT AUTO_INCREMENT,\n    emp_name VARCHAR(100),\n    emp_salary DECIMAL(10, 2),\n    PRIMARY KEY (emp_id)\n);\n\n-- \u63d2\u5165\u6570\u636e\nINSERT INTO employees (emp_name, emp_salary) \nVALUES ('John Doe', 50000.00), \n       ('Jane Smith', 55000.00), \n       ('James Brown', 60000.00);\n\n-- \u67e5\u8be2\u6240\u6709\u5458\u5de5\u6570\u636e\nSELECT * FROM employees;<\/code><\/pre>\n\n\n\n<p>\u67e5\u8be2\u7ed3\u679c\u5c06\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>emp_id<\/th><th>emp_name<\/th><th>emp_salary<\/th><\/tr><\/thead><tbody><tr><td>1<\/td><td>John Doe<\/td><td>50000.00<\/td><\/tr><tr><td>2<\/td><td>Jane Smith<\/td><td>55000.00<\/td><\/tr><tr><td>3<\/td><td>James Brown<\/td><td>60000.00<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">7. <strong>\u603b\u7ed3<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>AUTO_INCREMENT<\/code> \u662f MySQL \u4e2d\u7528\u4e8e\u81ea\u52a8\u751f\u6210\u552f\u4e00\u7684\u9012\u589e\u503c\u7684\u4e00\u79cd\u673a\u5236\uff0c\u901a\u5e38\u7528\u4e8e\u4e3b\u952e\u5217\u3002<\/li>\n\n\n\n<li>\u63d2\u5165\u6570\u636e\u65f6\uff0c<code>AUTO_INCREMENT<\/code> \u5217\u4e0d\u9700\u8981\u663e\u5f0f\u6307\u5b9a\u503c\uff0cMySQL \u4f1a\u81ea\u52a8\u4e3a\u5176\u5206\u914d\u9012\u589e\u503c\u3002<\/li>\n\n\n\n<li>\u53ef\u4ee5\u901a\u8fc7 <code>ALTER TABLE<\/code> \u8bed\u53e5\u91cd\u7f6e\u6216\u66f4\u6539 <code>AUTO_INCREMENT<\/code> \u7684\u8d77\u59cb\u503c\u3002<\/li>\n\n\n\n<li>\u901a\u8fc7 <code>LAST_INSERT_ID()<\/code> \u83b7\u53d6\u6700\u8fd1\u63d2\u5165\u7684 <code>AUTO_INCREMENT<\/code> \u503c\u3002<\/li>\n<\/ul>\n\n\n\n<p><code>AUTO_INCREMENT<\/code> \u662f\u6570\u636e\u5e93\u4e2d\u975e\u5e38\u5b9e\u7528\u7684\u529f\u80fd\uff0c\u80fd\u591f\u7b80\u5316\u4e3b\u952e\u7684\u7ba1\u7406\uff0c\u786e\u4fdd\u6570\u636e\u884c\u7684\u552f\u4e00\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 MySQL \u4e2d\uff0cAUTO_INCREMENT \u662f\u4e00\u79cd\u7528\u4e8e\u81ea\u52a8\u751f\u6210\u552f\u4e00\u503c\u7684\u673a\u5236\uff0c\u901a\u5e38\u7528\u4e8e\u4e3b\u952e\u5b57\u6bb5\u3002\u5b83\u662f\u4e00\u79cd\u5e38 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[35],"tags":[],"class_list":["post-1800","post","type-post","status-publish","format-standard","hentry","category-mysql"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1800","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=1800"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1800\/revisions"}],"predecessor-version":[{"id":1801,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1800\/revisions\/1801"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=1800"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=1800"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=1800"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}