Change the Auto Increment counter in MySQL?

MySQLMySQLi Database

In MySQL, auto increment counter starts from 0 by default, but if you want the auto increment to start from another number, use the below syntax.

ALTER TABLE yourTable auto_increment=yourIntegerNumber;

To understand the above syntax, let us first create a table. The query to create a table is as follows.

mysql> create table startAutoIncrement
-> (
-> Counter int auto_increment ,
-> primary key(Counter)
-> );
Query OK, 0 rows affected (0.90 sec)

Implement the above syntax to begin auto increment from 20. The query is as follows.

mysql> alter table startAutoIncrement auto_increment=20;
Query OK, 0 rows affected (0.30 sec)
Records: 0 Duplicates: 0 Warnings: 0

Insert some records in the table using insert command. The query is as follows.

mysql> insert into startAutoIncrement values();
Query OK, 1 row affected (0.20 sec)

mysql> insert into startAutoIncrement values();
Query OK, 1 row affected (0.14 sec)

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

Now you can check the table records from where auto increment started. We changed the auto increment to begin from 20 above.

The following is the query to display all records from the table using select statement.

mysql> select *from startAutoIncrement;

The following is the output.

+---------+
| Counter |
+---------+
| 20      |
| 21      |
| 22      |
+---------+
3 rows in set (0.00 sec)
raja
Published on 11-Jan-2019 06:47:36
Advertisements