Parsing auto_increment in mysql

Source: Internet
Author: User

When I visited the Forum today, I found a seemingly simple problem, but it attracted wide attention:
This is a very early interview question:
A table contains an ID auto-incrementing primary key. After 17 records are inserted, records 15, 16, and 17 are deleted, Mysql is restarted, and another record is inserted, the ID of this record is 18 or 15.
Answer:

If the table type is MyISAM, it is 18.
Because the MyISAM table records the maximum ID of the auto-incrementing primary key to the data file, the maximum ID of the restart MySQL auto-incrementing primary key will not be lost.
If the table type is InnoDB, it is 15.
The InnoDB table only records the maximum ID of the auto-increment primary key to the memory. Therefore, restarting the database or performing the OPTIMIZE operation on the table will lead to the loss of the maximum ID.

I also did an experiment and the results confirmed the above statement. I am ashamed to say that I have made a mistake in the case of a simple problem.
Copy codeThe Code is as follows:
Mysql> select * from test1;
+ ---- + ----------- +
| Id | name |
+ ---- + ----------- +
| 1 | Chen binhui |
| 2 | chen |
| 3 | chen |
| 4 | chen |
| 5 | chen |
| 6 | chen |
| 7 | chen |
| 8 | chen |
| 9 | chen |
| 10 | chen |
| 11 | chen |
+ ---- + ----------- +
11 rows in set (0.00 sec)

Mysql> delete from test1 where id in (10, 11, 9 );
Query OK, 3 rows affected (0.03 sec)

Mysql> show create table test1;
Create table 'test1 '(
'Id' int (11) not null auto_increment,
'Name' varchar (10) default NULL,
Primary key ('id ')
) ENGINE = InnoDB AUTO_INCREMENT = <SPAN style = "COLOR: # ff0000"> 12 </SPAN> default charset = utf8 |
Mysql> exit;
Bye
[Root @ fsailing1 ~] # Service mysqld restart
Stop MySQL: [OK]
Start MySQL: [OK]
[Root @ fsailing1 ~] # Mysql-uroot-p
Enter password:
Welcome to the MySQL monitor. Commands end with; or \ g.
Your MySQL connection id is 2
Server version: 5.0.95 Source distribution
Copyright (c) 2000,201 1, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
Affiliates. Other names may be trademarks of their respective
Owners.

Type 'help; 'or' \ H' for help. type' \ C' to clear the current input statement.

Mysql> use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with-

Database changed
Mysql> show create table test1;
| Create table 'test1 '(
'Id' int (11) not null auto_increment,
'Name' varchar (10) default NULL,
Primary key ('id ')
) ENGINE = InnoDB AUTO_INCREMENT = <SPAN style = "COLOR: # ff0000"> 9 </SPAN> default charset = utf8 |

2. You can also obtain the number of auto-increment fields in the current database table.
Copy codeThe Code is as follows:
Mysql> select last_insert_id ();
+ ------------------ +
| Last_insert_id () |
+ ------------------ +
| 0 |
+ ------------------ +
1 row in set (0.00 sec)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.