Bug #76206 | AUTO_INCREMENT behaviour not fully documented where values given explicitly | ||
---|---|---|---|
Submitted: | 7 Mar 2015 9:41 | Modified: | 17 Mar 2015 17:02 |
Reporter: | Alan Egerton | Email Updates: | |
Status: | Closed | Impact on me: | |
Category: | MySQL Server: Documentation | Severity: | S3 (Non-critical) |
Version: | All | OS: | Any |
Assigned to: | Paul DuBois | CPU Architecture: | Any |
Tags: | auto_increment |
[7 Mar 2015 9:41]
Alan Egerton
[7 Mar 2015 23:55]
MySQL Verification Team
Thank you for the bug report.
[17 Mar 2015 17:02]
Paul DuBois
Thank you for your bug report. This issue has been addressed in the documentation. The updated documentation will appear on our website shortly.
[27 Jun 2018 16:12]
Daniel Price
Posted by developer: The documentation update for the bug added the following text: "When you insert any other value into an AUTO_INCREMENT column, the column is set to that value and the sequence is reset so that the next automatically generated value follows sequentially from the largest column value." This is only true in MySQL 8.0. For previous versions of MySQL, the sequence is not reset as demonstrated by this example on MySQL 5.7: CREATE TABLE animals ( id MEDIUMINT AUTO_INCREMENT, name CHAR(30), PRIMARY KEY (id) ); INSERT INTO animals (name) VALUES ('dog'),('cat'),('penguin'), ('lax'),('whale'),('ostrich'); SELECT * FROM animals; +------+---------+ | id | name | +------+---------+ | 1 | dog | | 2 | cat | | 3 | penguin | | 4 | lax | | 5 | whale | | 6 | ostrich | +------+---------+ 6 rows in set (0.00 sec) UPDATE animals SET id=1000 WHERE name='ostrich'; INSERT INTO animals (name) VALUES ('groundhog'), ('squirrel'); mysql> SELECT * FROM animals; +------+-----------+ | id | name | +------+-----------+ | 1 | dog | | 2 | cat | | 3 | penguin | | 4 | lax | | 5 | whale | | 7 | groundhog | | 8 | squirrel | | 1000 | ostrich | +------+-----------+
[13 Jul 2018 13:52]
Daniel Price
Posted by developer: Correction: Inserting an explicit value that is greater than the internal auto-increment counter, does reset the sequence. Updating an existing auto-increment column value, as demonstrated above, does not (prior to MySQL 8.0). Test script: DROP TABLE IF EXISTS t1; CREATE TABLE t1 (i INT NOT NULL AUTO_INCREMENT PRIMARY KEY); INSERT INTO t1 () VALUES(); INSERT INTO t1 () VALUES(); INSERT INTO t1 () VALUES(); SELECT * FROM t1; INSERT INTO t1 () VALUES(47); INSERT INTO t1 () VALUES(); SELECT * FROM t1; Result: mysql> DROP TABLE IF EXISTS t1; Query OK, 0 rows affected (0.12 sec) mysql> CREATE TABLE t1 (i INT NOT NULL AUTO_INCREMENT PRIMARY KEY); Query OK, 0 rows affected (0.03 sec) mysql> INSERT INTO t1 () VALUES(); Query OK, 1 row affected (0.01 sec) mysql> INSERT INTO t1 () VALUES(); Query OK, 1 row affected (0.00 sec) mysql> INSERT INTO t1 () VALUES(); Query OK, 1 row affected (0.00 sec) mysql> SELECT * FROM t1; +---+ | i | +---+ | 1 | | 2 | | 3 | +---+ 3 rows in set (0.00 sec) mysql> INSERT INTO t1 () VALUES(47); Query OK, 1 row affected (0.01 sec) mysql> INSERT INTO t1 () VALUES(); Query OK, 1 row affected (0.00 sec) mysql> SELECT * FROM t1; +----+ | i | +----+ | 1 | | 2 | | 3 | | 47 | | 48 | +----+ 5 rows in set (0.00 sec)
[16 Jul 2018 11:56]
Daniel Price
Posted by developer: The following behavior applies in MySQL 5.5, 5.6, and 5.7: "When you insert any other value into an AUTO_INCREMENT column, the column is set to that value and the sequence is reset so that the next automatically generated value follows sequentially from the largest column value. Updating an existing AUTO_INCREMENT column value in an InnoDB table does not reset the AUTO_INCREMENT sequence as it does for MyISAM and NDB tables." In MySQL 8.0, updating an existing AUTO_INCREMENT column value in an InnoDB table also resets the AUTO_INCREMENT sequence. https://dev.mysql.com/doc/mysql-tutorial-excerpt/5.7/en/example-auto-increment.html The revised documentation should appear online soon.