在 MySQL 中重置 AUTO_INCREMENT

mysqlmysqli database更新于 2025/9/15 5:00:17

截断表以重置 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)

使用 insert 命令在表中插入一些记录 −

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)

相关文章


有用资源