MySQL Interview: There are three pieces of data in an auto-increment table. After deleting the last two pieces of data, restart the database and add another piece of data. What is the ID of this piece of data?

I discovered such a problem when I was studying the database interview questions today, so I tried it and came to the following conclusions:

1. If the engine type of the table is  MyISAM , then the ID of this record is 4. Because the  MyISAM table will record the maximum ID of the auto-incrementing primary key in the data file , after restarting MySQL, the maximum ID of the auto-incrementing primary key will not be lost.

2. If the engine type of the table is  InnoDB , then the ID of this record is 2. Because the  InnoDB table records the maximum ID of the auto-incrementing primary key in memory , the maximum ID (in fact, the value of the AUTO_INCREMENT counter) will be lost after restarting the database; once the database is re-run, the database will automatically calculate the maximum ID of the auto-incrementing primary key ( in fact That is, add 1 to the last record ID and assign it to AUTO_INCREMENT ) again into the memory.

[Note] This is only the previous version of MySQL 8, that is, MySQL 5.7 and previous versions . Because in MySQL 8.0, the behavior of InnoDB has changed . Each time it is changed, the current maximum auto-increment counter value (AUTO_INCREMENT) will be written to the redo log and saved to the engine-specific system table of each checkpoint. These changes make the current maximum auto-increment counter value remain unchanged after the server restarts, the same as  MyISAM  . See the official document for details: InnoDB AUTO_INCREMENT counter initialization

[Note] If the deleted record is not the last record, the auto-incremented maximum ID is all the same, because the auto-increment counter value (AUTO_INCREMENT) will not change.

The maximum ID of an auto-increment primary key is controlled by AUTO_INCREMENT (starting number of auto-increment), which can be specified by the statement  SHOW CREATE TABLE table name;  view detailed information of the table. The following information will be entered:

+---------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table   | Create Table                                                                                                                                                              |
+---------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| testNum | CREATE TABLE `testNum` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(2) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 |
+---------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
 

Guess you like

Origin blog.csdn.net/peng2hui1314/article/details/104145979