Introduction: When using MySQL to create a table, we usually create an auto-increment field (AUTO_INCREMENT) and use this field as the primary key. This article will tell you everything about auto-increment ID in the form of questions and answers. Note: This article is all based on the Innodb storage engine. Let’s not talk too much, let’s take a look at the detailed introduction together. 1.Why does MySQL recommend setting the auto-increment column id as the primary key?
In summary: when we use the auto-increment column as the primary key, the access efficiency is the highest. 2. Are the auto-increment column IDs necessarily continuous? The increment of the auto-increment ID is not necessarily continuous. Let's first look at MySQL's storage strategy for auto-increment values:
The situations that cause the auto-increment ID to be discontinuous may be: 1. Unique key conflict 2. Transaction rollback 3. insert ... select statement to batch apply for auto-increment id 3.Is there an upper limit for the auto-increment ID? The auto-increment id is an integer field. We often use the int type to define the growth id, and the int type has an upper limit, which means the growth id also has an upper limit.
It can be seen from the above table that when the auto-increment field uses the int signed type, the maximum value can reach 2147483647, which is more than 2.1 billion; when using the int unsigned type, the maximum value can reach 4294967295, which is more than 4.2 billion. Of course, bigint can represent a wider range. Next, we test what happens when the auto-increment id reaches the maximum and inserts data again: create table t(id int unsigned auto_increment primary key) auto_increment=4294967295; insert into t values(null); // Successfully inserted a row of 4294967295 show create table t; /* CREATE TABLE `t` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, PRIMARY KEY (`id`) )ENGINE=InnoDB AUTO_INCREMENT=4294967295; */ insert into t values(null); //Duplicate entry '4294967295' for key 'PRIMARY' From the experiment, we can see that when the auto-increment ID reaches the maximum, it cannot be expanded. After the first insert statement successfully inserts the data, the AUTO_INCREMENT of this table does not change (it is still 4294967295), which causes the second insert statement to get the same auto-increment ID value. When trying to execute the insert statement again, a primary key conflict error is reported. 4. How should we maintain the auto-increment column? The following two suggestions are provided for maintenance: 1. Field type selection: It is recommended to use the int unsigned type. If it is predicted that the amount of data in the table will be very large, the bigint unsigned type can be used instead. 2. Pay more attention to the auto-increment value of large tables to prevent primary key overflow. Summarize The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. Thank you for your support of 123WORDPRESS.COM. You may also be interested in:
|
<<: $nextTick explanation that you can understand at a glance
>>: Nginx one domain name to access multiple projects method example
Install Enter the following command to install it...
When using Navicat to connect to a remote Linux M...
Table of contents 4 isolation levels of MySQL Cre...
Table of contents 1. Example: this can directly g...
What should I do if MySQL fails to connect to the...
To put it simply, website construction is about &q...
There are two types of dead link formats defined b...
Today is still a case of Watch app design. I love...
This article shares the specific code of the jQue...
Table of contents 1. Resource files 2. Installati...
To automatically load kernel modules in CentOS, y...
In the vue scaffolding, we can see that in the ne...
Solution Add position:relative to the parent elem...
This article shares the specific code for Vue to ...
Preface Currently, the front-end solves cross-dom...