如何理解MySQL表的自动新增AUTO_INCREMENT的值和步长 本页介绍如何理解MySQL表的自动新增AUTOINCREMENT的值和步长。 表的新增AUTOINCREMENT的值和步长 背景描述: AUTOINCREMENT的初值与步长由"autoincrementincrement" 和"autoincrementoffset"两个参数决定。 1.autoincrementoffset:AUTOINCREMENT值的初值。 2.autoincrementincrement:AUTOINCREMENT值每次增长的步长。 3.当 autoincrementoffset > autoincrementincrement 时,实际使用时初值会变为为autoincrementincrement。 4.当 autoincrementoffset show variables like 'autoinc%'; ++ Variablename Value ++ autoincrementincrement 1 autoincrementoffset 1 ++ 2 rows in set (0.00 sec) mysql> create table tiayiyuntest(uid int NOT NULL AUTOINCREMENT, PRIMARY KEY (uid)); Query OK, 0 rows affected (0.01 sec) mysql> show create table tiayiyuntest; ++ Table Create Table ++ tiayiyuntest CREATE TABLE tiayiyuntest ( uid int(11) NOT NULL AUTOINCREMENT, PRIMARY KEY (uid) ) ENGINEInnoDB DEFAULT CHARSETutf8mb4 COLLATEutf8mb4bin ++ 1 row in set (0.00 sec) mysql> insert into tiayiyuntest values(0), (0), (0); Query OK, 3 rows affected (0.00 sec) Records: 3 Duplicates: 0 Warnings: 0 mysql> select from tiayiyuntest; + uid + 1 2 3 + 3 rows in set (0.00 sec) mysql> show create table tiayiyuntest; ++ Table Create Table ++ tiayiyuntest CREATE TABLE tiayiyuntest ( uid int(11) NOT NULL AUTOINCREMENT, PRIMARY KEY (uid) ) ENGINEInnoDB AUTOINCREMENT4 DEFAULT CHARSETutf8mb4 COLLATEutf8mb4bin ++ 1 row in set (0.00 sec) 2.修改autoincrementincrement2,步长变为2。 mysql> set session autoincrementoffset2; Query OK, 0 rows affected (0.00 sec) mysql> show variables like 'autoinc%'; ++ Variablename Value ++ autoincrementincrement 1 autoincrementoffset 2 ++ 2 rows in set (0.00 sec) mysql> insert into tiayiyuntest values(0), (0), (0); Query OK, 3 rows affected (0.01 sec) Records: 3 Duplicates: 0 Warnings: 0 mysql> select from tiayiyuntest; + uid + 1 2 3 4 5 6 + 6 rows in set (0.00 sec) mysql> show create table tiayiyuntest; ++ Table Create Table ++ tiayiyuntest CREATE TABLE tiayiyuntest ( uid int(11) NOT NULL AUTOINCREMENT, PRIMARY KEY (uid) ) ENGINEInnoDB AUTOINCREMENT7 DEFAULT CHARSETutf8mb4 COLLATEutf8mb4bin ++ 1 row in set (0.00 sec) 3.autoincrementoffset10,autoincrementincrement2,初值为2(因为autoincrementoffset > autoincrementincrement),步长为2。 mysql> set session autoincrementoffset10; Query OK, 0 rows affected (0.00 sec) mysql> set session autoincrementincrement2; Query OK, 0 rows affected (0.00 sec) mysql> show variables like 'autoinc%'; ++ Variablename Value ++ autoincrementincrement 2 autoincrementoffset 10 ++ 2 rows in set (0.00 sec) mysql> create table tianyiyuntest(uid int NOT NULL AUTOINCREMENT, PRIMARY KEY (uid)); Query OK, 0 rows affected (0.01 sec) mysql> show create table tianyiyuntest; ++ Table Create Table ++ tianyiyuntest CREATE TABLE tianyiyuntest ( uid int(11) NOT NULL AUTOINCREMENT, PRIMARY KEY (uid) ) ENGINEInnoDB DEFAULT CHARSETutf8mb4 COLLATEutf8mb4bin ++ 1 row in set (0.00 sec) mysql> insert into tianyiyuntest values(0), (0), (0); Query OK, 3 rows affected (0.00 sec) Records: 3 Duplicates: 0 Warnings: 0 mysql> select from tianyiyuntest; + uid + 2 4 6 + 3 rows in set (0.00 sec) mysql> show create table tianyiyuntest; ++ Table Create Table ++ tianyiyuntest CREATE TABLE tianyiyuntest ( uid int(11) NOT NULL AUTOINCREMENT, PRIMARY KEY (uid) ) ENGINEInnoDB AUTOINCREMENT8 DEFAULT CHARSETutf8mb4 COLLATEutf8mb4bin ++ 1 row in set (0.00 sec) 4.autoincrementoffset5,autoincrementincrement10,初值为5,步长为10。 mysql> set session autoincrementoffset5; Query OK, 0 rows affected (0.00 sec) mysql> set session autoincrementincrement10; Query OK, 0 rows affected (0.00 sec) mysql> show variables like 'autoinc%'; ++ Variablename Value ++ autoincrementincrement 10 autoincrementoffset 5 ++ 2 rows in set (0.00 sec) mysql> create table tianyiy(uid int NOT NULL AUTOINCREMENT, PRIMARY KEY (uid)); Query OK, 0 rows affected (0.01 sec) mysql> insert into tianyiy values(0), (0), (0); Query OK, 3 rows affected (0.00 sec) Records: 3 Duplicates: 0 Warnings: 0 mysql> select from tianyiy; + uid + 5 15 25 + 3 rows in set (0.00 sec) mysql> show create table tianyiy; ++ Table Create Table ++ tianyiy CREATE TABLE tianyiy ( uid int(11) NOT NULL AUTOINCREMENT, PRIMARY KEY (uid) ) ENGINEInnoDB AUTOINCREMENT35 DEFAULT CHARSETutf8mb4 COLLATEutf8mb4bin ++ 1 row in set (0.00 sec)