Computer >> Computer tutorials >  >> Programming >> MySQL

How to reset auto-incrementing column in MySQL?


To reset auto-incrementing column, use TRUNCATE TABLE command. After that, on insertion, it will reset the column.

Let us first create a table −

mysql> create table DemoTable1888
   (
   Id int NOT NULL AUTO_INCREMENT,
   PRIMARY KEY(Id)
   );
Query OK, 0 rows affected (0.00 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable1888 values();
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1888 values();
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1888 values();
Query OK, 1 row affected (0.00 sec)

Display some records in the table using insert command −

mysql> select * from DemoTable1888;

This will produce the following output −

+----+
| Id |
+----+
|  1 |
|  2 |
|  3 |
+----+
3 rows in set (0.00 sec)

Here is the query to reset auto-incrementing column −

mysql> truncate table DemoTable1888;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from DemoTable1888;
Empty set (0.00 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable1888 values();
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1888 values();
Query OK, 1 row affected (0.00 sec)

Display some records in the table using insert command −

mysql> select * from DemoTable1888;

This will produce the following output −

+----+
| Id |
+----+
|  1 |
|  2 |
+----+
2 rows in set (0.00 sec)