How to reset auto-increment values in MySQL
MySQL provides you with a useful function called auto increment. You can assign the AUTO_INCREMENT attribute to the table column to create a unique identifier for the new row. Generally, you use the AUTO_INCREMENT attribute for the primary table key column.
Whenever you insert a new row into a table, MySQL uses the AUTO_INCREMENT attribute to automatically assign an ordinal number to the column.
For example, if there are eight rows in a table and you insert a new row without specifying a value for the autoincrement column, MySQL will automatically insert a new id row with a value of 9.
Sometimes you may need to reset the autoincrement column value so that the ID of the first record you insert into the table starts with a certain number, for example, 1.
In MySQL, you can reset the auto-increment values in various ways.
EXAMPLES OF RESETTING THE VALUE OF AUTOMATIC INCREMENTS IN MYSQL
First, create a table with the name tmp and assign the attribute AUTO_INCREMENT to the id column of the primary key.
CREATE TABLE tmp (
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(45) DEFAULT NULL,
PRIMARY KEY (id)
Second, insert a sample data in the tmp table:
INSERT INTO tmp(name)
Thirdly, a query to the tmp table to check the insertion operation:
We have three rows with column ID values: 1, 2, and 3. Perfect! It’s time to practice resetting the auto-increment value of the ID column.
USING ALTER TABLE INSTRUCTION
You can reset the auto-increment value with the ALTER TABLE operator. The syntax of the ALTER TABLE operator to reset the autoincrement is as follows:
ALTER TABLE table_name AUTO_INCREMENT = value;
You specify the name of the table after ALTER TABLE and the value you want to reset in the expression AUTO_INCREMENT=value.
Note that the value must be greater than or equal to the current maximum value of the auto-increment column.
Let’s delete the last entry in the tmp table with id value 3:
DELETE FROM tmp
ID = 3;
If you insert a new row, MySQL will assign 4 columns id of the new row. However, you can reset the number generated by MySQL to 3 using the following ALTER TABLE instruction:
ALTER TABLE tmp AUTO_INCREMENT = 3;
Now let’s try to insert a new line in the tmp table and request data from it to see the effect:
INSERT INTO tmp(name)
VALUES ('MySQL example 3');
We have three lines with the last autoincrement value of 3 instead of 4, which is what we expected.
USING TRUNCATE TABLE OPERATOR
The TRUNCATE TABLE operator deletes all data from the table and resets the auto-increment value to zero.
The following illustrates the syntax of the TRUNCATE TABLE operator:
TRUNCATE TABLE table_name;
Using the TRUNCATE TABLE operator, you remove all data from the table forever and reset the auto-increment value to zero.
USING DROP TABLE AND CREATE TABLE OPERATORS
You can use a pair of operators: DROP TABLE and CREATE TABLE to reset the auto-increment column. Note that this method removes all data from the table forever.
Like the TRUNCATE TABLE operator, these operators delete the table and recreate it, so the autoincrement value is reset to zero.
DROP TABLE table_name;
CREATE TABLE table_name(...);
In this article, you have learned how to reset the autoincrement value in MySQL in various ways. The first method is preferable because it is the simplest and has no side effects.