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
--When connecting to the database, the matching r...
MySQL official website download address: https://...
Find the problem I recently encountered a problem...
touch Command It has two functions: one is to upd...
This article example shares the specific code for...
Table of contents Installation-free version of My...
1. Apache server installation and configuration y...
1. What is it? MySQL is the most popular relation...
A few days ago, I wrote an article about using CS...
This article shares with you how to use Vue to ch...
Table of contents 1. Overview 2. gdb debugging 2....
This article describes how to install mysql5.6 us...
Install Ubuntu 20.04 Install NVIDIA drivers Confi...
Generate a certificate chain Use the script to ge...
IP masquerading and port forwarding Firewalld sup...