MySQL query to set my auto increment column ( id ) to zero or reset the value of auto increment field?

MySQLMySQLi Database

Use ALTER table to set the auto_increment column to 0 or reset with another value

ALTER TABLE yourTableName AUTO_INCREMENT=0;

The above syntax will begin from 1.

Let us first create a table −

mysql> create table DemoTable698 (
   ClientId int NOT NULL AUTO_INCREMENT PRIMARY KEY
) auto_increment=109;
Query OK, 0 rows affected (0.88 sec)

Here is the query to reset the auto_increment value to 0 −

mysql> alter table DemoTable698 AUTO_INCREMENT=0;
Query OK, 0 rows affected (0.21 sec)
Records: 0 Duplicates: 0 Warnings: 0

Insert some records in the table using insert command. Here, we haven’t inserted anything since we want to display auto increment value after setting it from beginning again −

mysql> insert into DemoTable698 values();
Query OK, 1 row affected (0.18 sec)
mysql> insert into DemoTable698 values();
Query OK, 1 row affected (0.12 sec)
mysql> insert into DemoTable698 values();
Query OK, 1 row affected (0.12 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable698;

This will produce the following output -

+----------+
| ClientId |
+----------+
| 1        |
| 2        |
| 3        |
+----------+
3 rows in set (0.00 sec)
raja
Published on 21-Aug-2019 11:48:17
Advertisements