在MySQL中重置AUTO_INCREMENT
截断表以重置AUTO_INCREMENT-
truncate table yourTableName;
让我们首先创建一个表-
mysql> create table DemoTable -> ( -> Id int NOT NULL AUTO_INCREMENT, -> PRIMARY KEY(Id) -> ); Query OK, 0 rows affected (0.52 sec)
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable values(); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values(); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(); Query OK, 1 row affected (0.09 sec)
使用select语句显示表中的所有记录-
mysql> select *from DemoTable;
输出结果
+----+ | Id | +----+ | 1 | | 2 | | 3 | | 4 | +----+ 4 rows in set (0.00 sec)
以下是重置AUTO_INCREMENT的查询-
mysql> truncate table DemoTable; Query OK, 0 rows affected (0.80 sec) Let us insert some records once again. It will by default begin from 1 for AUTO_INCREMENT: mysql> insert into DemoTable values(); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values(); Query OK, 1 row affected (0.09 sec)
显示表中的所有记录-
mysql> select *from DemoTable;
输出结果
+----+ | Id | +----+ | 1 | | 2 | +----+ 2 rows in set (0.00 sec)