MySQL's auto_increment problem record

Outline

In database applications, often used in auto-increment unique number to identify the record. In MySQL, can be generated automatically by auto_increment attribute data column. Available "auto_increment = n" option may be to build a table to specify the initial value of the increment . Available "alter table table_name auto_increment = n" command to reset the increment starting value, of course, will take MySQL data table auto_increment columns provided when the maximum value of n + 1 and the larger value as a new auto_increment .

 

MySQL's auto_increment attribute has the following characteristics:

  • Auto_increment attribute data column should have a positive sequence number, if the data is declared as UNSIGNED column, so that the number of sequence numbers can be doubled. For example the maximum number of data tinyint column is 127, if coupled UNSIGNED, then the maximum number becomes 255
  • auto_increment data columns must have a unique index number to avoid repetition; NOT NULL must have properties

    Practice found out all the data in a table Zhang innoDB delete and restart Mysql will lead auto_increment columns of the table becomes one. Test multiple variations Laid case auto_increment column and reported below.

in conclusion

1) innoDB engine tables, after performing the delete operation to empty, table auto_increment values ​​will not be affected; once restarted Mysql database, auto_increment value will become 1

2) MyISAM engine table after the delete operation, AUTO_INCREMENT value table will not be affected; Mysql database restart, the value will not be affected AUTO_INCREMENT

3) When you create innoDB table, specify whether or not to specify auto_increment, delete Clear + to restart the database will cause the table auto_increment value becomes 1

4) delete innoDB table, with or without added WHERE 1, after a restart of the database will be reset to 1 auto_increment

5) Mysql database after a reboot, innoDB table auto_increment value will be set to the maximum value of a column in the table auto_increment + 1

the reason

Mysql auto_increment value of the database is stored in memory, auto_increment innoDB engine tables in the database service stops and will not do lasting operation, Mysql database will restart the next time, the equivalent of executing a statement:

select max(id) maxId from table;
alter table auto_increment = maxId + 1;

Auto_increment set value table of the table.

 

Strictly speaking this is a bug Mysql's. This bug will be fixed in version 8.0. About where the content of version 8.0, see: MySQL 8.0.0 release, the highlights of which are in!  And  MySQL 8.0 release, it's time to say goodbye with MyISAM

Published 378 original articles · won praise 19 · views 160 000 +

Guess you like

Origin blog.csdn.net/qq_32252917/article/details/104113609