mysqldude

MySQL reset of auto increment values

Version compatibility

StepMySQLMariaDB
5.65.78.08.4910.610.1111.411.8
Insert explicit id 15, next automatic id continues at 16 (original)
original post
✓✓✓✓✓✓✓✓✓
Counter at INT max (2147483647): next insert fails (post shows 1062)
original post
✓ 1062✓ 1062✓ 1062✓ 1062✓ 1062✗ 167✗ 167✗ 167✗ 167
DELETE the max row, ALTER TABLE ... AUTO_INCREMENT = 30, insert (original)
original post
✓✓✓✓✓✓✓✓✓
Delete the highest id, restart the server, insert: is the counter kept?
version check
≠≠✓✓✓✓✓✓✓
Insert explicit id 15, next automatic id continues at 16 (original) original post
MySQL 5.6✓5.7✓8.0✓8.4✓9✓
MariaDB 10.6✓10.11✓11.4✓11.8✓
Counter at INT max (2147483647): next insert fails (post shows 1062) original post
MySQL 5.6✓ 10625.7✓ 10628.0✓ 10628.4✓ 10629✓ 1062
MariaDB 10.6✗ 16710.11✗ 16711.4✗ 16711.8✗ 167
DELETE the max row, ALTER TABLE ... AUTO_INCREMENT = 30, insert (original) original post
MySQL 5.6✓5.7✓8.0✓8.4✓9✓
MariaDB 10.6✓10.11✓11.4✓11.8✓
Delete the highest id, restart the server, insert: is the counter kept? version check
MySQL 5.6≠5.7≠8.0✓8.4✓9✓
MariaDB 10.6✓10.11✓11.4✓11.8✓
✓works ✓ 1062fails with the error the step demonstrates ⚠works with a warning ≠runs, result differs ✗fails Tested 2026-11-23 on 5.6.51, 5.7.44, 8.0.46, 8.4.13, 9.7.4, 10.6.28, 10.11.19, 11.4.13, 11.8.9
What the warnings and failures say (2)
  • Counter at INT max (2147483647): next insert fails (post shows 1062) (MariaDB 10.6, MariaDB 10.11, MariaDB 11.4, MariaDB 11.8): error 167: Out of range value for column 'id' at row 1
  • Delete the highest id, restart the server, insert: is the counter kept? (MySQL 5.6, MySQL 5.7): rows differ

Related errors: 1062, 1467

The original article

First published 2015-01-13. The text below is the original; the table above shows how it behaves on current versions.

There are occasions when it is necessary to reset the auto_increment value of a table in MySQL. For example, you are adding content but want to identify a new application process by changing from the current value of let’s say 1538 and start the next value on 10000. That way you can easily identify entries which were inserted with the new process.

Firstly, some background into how auto_increments works. It is possible to assign a value to an auto_increment field or have a value automatically assigned by leaving the auto_increment field as a null.

mysql> insert into persons (dob, name) values (str_to_date('01/01/2015 00:00', '%d/%m/%Y %T'), 'Tom'),
    -> (str_to_date('01/01/2015 01:00', '%d/%m/%Y %T'), 'Tim'),
    -> (str_to_date('02/01/2015 15:23', '%d/%m/%Y %T'), 'Ron'),
    -> (str_to_date('07/01/2015 10:19', '%d/%m/%Y %T'), 'Jerry'),
    -> (str_to_date('07/01/2015 21:59', '%d/%m/%Y %T'), 'Abe'),
    -> (str_to_date('11/01/2015 16:00', '%d/%m/%Y %T'), 'Jake'),
    -> (str_to_date('12/01/2015 13:32', '%d/%m/%Y %T'), 'John'),
    -> (str_to_date('13/01/2015 21:37', '%d/%m/%Y %T'), 'Jack');
Query OK, 8 rows affected (0.01 sec)
Records: 8  Duplicates: 0  Warnings: 0

mysql> insert into persons (id, dob, name) values (null, str_to_date('13/02/2015 13:09', '%d/%m/%Y %T'), 'Jane');
Query OK, 1 row affected (0.01 sec)

mysql> select * from persons order by id;
+----+---------------------+-------+
| id | dob                 | name  |
+----+---------------------+-------+
|  1 | 2015-01-01 00:00:00 | Tom   |
|  2 | 2015-01-01 01:00:00 | Tim   |
|  3 | 2015-01-02 15:23:00 | Ron   |
|  4 | 2015-01-07 10:19:00 | Jerry |
|  5 | 2015-01-07 21:59:00 | Abe   |
|  6 | 2015-01-11 16:00:00 | Jake  |
|  7 | 2015-01-12 13:32:00 | John  |
|  8 | 2015-01-13 21:37:00 | Jack  |
|  9 | 2015-02-13 13:09:00 | Jane  |
+----+---------------------+-------+
9 rows in set (0.00 sec)

mysql> insert into persons (id, dob, name) values (15, str_to_date('11/02/2015 01:17', '%d/%m/%Y %T'), 'Jade');
Query OK, 1 row affected (0.01 sec)

mysql> select * from persons order by id;
+----+---------------------+-------+
| id | dob                 | name  |
+----+---------------------+-------+
|  1 | 2015-01-01 00:00:00 | Tom   |
|  2 | 2015-01-01 01:00:00 | Tim   |
|  3 | 2015-01-02 15:23:00 | Ron   |
|  4 | 2015-01-07 10:19:00 | Jerry |
|  5 | 2015-01-07 21:59:00 | Abe   |
|  6 | 2015-01-11 16:00:00 | Jake  |
|  7 | 2015-01-12 13:32:00 | John  |
|  8 | 2015-01-13 21:37:00 | Jack  |
|  9 | 2015-02-13 13:09:00 | Jane  |
| 15 | 2015-02-11 01:17:00 | Jade  |
+----+---------------------+-------+
10 rows in set (0.00 sec)

Adding another entry at this point will scan the largest value in the id column and add one. So the next value will not be 10 but will be 16.

mysql> insert into persons (dob, name) values (str_to_date('15/02/2015 03:29', '%d/%m/%Y %T'), 'Jasmin');
Query OK, 1 row affected (0.01 sec)

mysql> select * from persons order by id;
+----+---------------------+--------+
| id | dob                 | name   |
+----+---------------------+--------+
|  1 | 2015-01-01 00:00:00 | Tom    |
|  2 | 2015-01-01 01:00:00 | Tim    |
|  3 | 2015-01-02 15:23:00 | Ron    |
|  4 | 2015-01-07 10:19:00 | Jerry  |
|  5 | 2015-01-07 21:59:00 | Abe    |
|  6 | 2015-01-11 16:00:00 | Jake   |
|  7 | 2015-01-12 13:32:00 | John   |
|  8 | 2015-01-13 21:37:00 | Jack   |
|  9 | 2015-02-13 13:09:00 | Jane   |
| 15 | 2015-02-11 01:17:00 | Jade   |
| 16 | 2015-02-15 03:29:00 | Jasmin |
+----+---------------------+--------+
11 rows in set (0.00 sec)

Truncating a table will automatically reset the auto_increment value back to 1 on a table. However, this implies removing all of the data.

Now let us have a look at what happens if we reach the maximum value for the auto_increment column and we attempt to add another row. An INT has a max SIGNED value of 2147483647. So we will add in a record with this value and then attempt to add a new value with auto_increment automatically assigning the next largest value i.e. over the limit of the column.

mysql> insert into persons (id, dob, name) values (2147483647, str_to_date('17/02/2015 15:13', '%d/%m/%Y %T'), 'Tod');
Query OK, 1 row affected (0.01 sec)

mysql> select * from persons;
+------------+---------------------+--------+
| id         | dob                 | name   |
+------------+---------------------+--------+
|          1 | 2015-01-01 00:00:00 | Tom    |
|          2 | 2015-01-01 01:00:00 | Tim    |
|          3 | 2015-01-02 15:23:00 | Ron    |
|          4 | 2015-01-07 10:19:00 | Jerry  |
|          5 | 2015-01-07 21:59:00 | Abe    |
|          6 | 2015-01-11 16:00:00 | Jake   |
|          7 | 2015-01-12 13:32:00 | John   |
|          8 | 2015-01-13 21:37:00 | Jack   |
|          9 | 2015-02-13 13:09:00 | Jane   |
|         15 | 2015-02-11 01:17:00 | Jade   |
|         16 | 2015-02-15 03:29:00 | Jasmin |
| 2147483647 | 2015-02-17 15:13:00 | Tod    |
+------------+---------------------+--------+
12 rows in set (0.00 sec)

mysql> insert into persons (dob, name) values (str_to_date('17/02/2015 19:32', '%d/%m/%Y %T'), 'Buddy');
ERROR 1062 (23000): Duplicate entry '2147483647' for key 'PRIMARY'

In this case we have reached the maximum value we now need to change the auto_increment value. However, we need to remove the highest value auto_increment value on the table otherwise it will no longer work as the new value will always be the highest auto_increment value plus 1. In the example below we will set the auto_increment value to 30 and start adding entries using this value.

mysql> delete from persons where id = 2147483647;
Query OK, 1 row affected (0.00 sec)

mysql> select * from persons;
+----+---------------------+--------+
| id | dob                 | name   |
+----+---------------------+--------+
|  1 | 2015-01-01 00:00:00 | Tom    |
|  2 | 2015-01-01 01:00:00 | Tim    |
|  3 | 2015-01-02 15:23:00 | Ron    |
|  4 | 2015-01-07 10:19:00 | Jerry  |
|  5 | 2015-01-07 21:59:00 | Abe    |
|  6 | 2015-01-11 16:00:00 | Jake   |
|  7 | 2015-01-12 13:32:00 | John   |
|  8 | 2015-01-13 21:37:00 | Jack   |
|  9 | 2015-02-13 13:09:00 | Jane   |
| 15 | 2015-02-11 01:17:00 | Jade   |
| 16 | 2015-02-15 03:29:00 | Jasmin |
+----+---------------------+--------+
11 rows in set (0.00 sec)

mysql> alter table persons auto_increment = 30;
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> insert into persons (dob, name) values (str_to_date('17/02/2015 19:32', '%d/%m/%Y %T'), 'Buddy');
Query OK, 1 row affected (0.01 sec)

mysql> select * from persons;
+----+---------------------+--------+
| id | dob                 | name   |
+----+---------------------+--------+
|  1 | 2015-01-01 00:00:00 | Tom    |
|  2 | 2015-01-01 01:00:00 | Tim    |
|  3 | 2015-01-02 15:23:00 | Ron    |
|  4 | 2015-01-07 10:19:00 | Jerry  |
|  5 | 2015-01-07 21:59:00 | Abe    |
|  6 | 2015-01-11 16:00:00 | Jake   |
|  7 | 2015-01-12 13:32:00 | John   |
|  8 | 2015-01-13 21:37:00 | Jack   |
|  9 | 2015-02-13 13:09:00 | Jane   |
| 15 | 2015-02-11 01:17:00 | Jade   |
| 16 | 2015-02-15 03:29:00 | Jasmin |
| 30 | 2015-02-17 19:32:00 | Buddy  |
+----+---------------------+--------+
12 rows in set (0.00 sec)