Problems encountered when updating the auto-increment primary key id in Mysql

Problems encountered when updating the auto-increment primary key id in Mysql

This is a problem I know about, but I almost fell into the trap (I almost forgot it, but fortunately I remembered it when I sorted out the online points before going online), so I specially recorded it here

Why update the auto-increment id?

I had to update a batch of IDs because of historical business problems, and in order to avoid conflicts, I needed to expand the IDs by several times for update. Because the amount of data in my table is not high, it is a high-read and low-write situation, so I simply expanded it by 1000.

question

In MySQL, if we update the auto-increment primary key to a larger value (for example, the maximum value of the auto-increment id is 1000, and you update the record with id=49 to id=1049), MySQL will not modify the auto-increment value of the table to the updated value. In some cases, such as after DDL, restart, etc., the business starts to report errors. At this time, if you don’t know the current operation, you may mistakenly think that it is a problem with the current business operation. In fact, it is because of the pit buried by the update id (primary key conflict)
As shown below:

Figure 1: Original data before update

Raw data

Execute update statement

update test set id = 10 where id = 2;

Figure 2: Updated data

Updated data

Execute a new insert statement

insert test (name) values ​​('dddd')

Figure 3: New data inserted

After inserting new data

I believe everyone has seen the problem at this time. There may be no problem at the beginning after the update, but when the self-incrementing id catches up with the maximum value you updated, id conflict is inevitable. . .

How to solve it

1. If it is a personal test library, it is not important, you can restart the database
2. Of course, the online database cannot be set up in this way, unless you are very willful (and you need a DBA to accompany you in being willful). At this time, you can try to specify an id to insert a piece of data that is meaningless in the business, such as soft deleted data. (My case list does not have a soft delete mark, you can understand it.)

insert test (id,name) values ​​(20,'eeee');

After operation, as shown in the figure:

Insert by specifying id

Execute the following SQL statement and compare the results

insert test (name) values ​​('ffff');

Comparison results

At this point, the auto-increment id has started to increase from the maximum value.

After searching for information, I found that this BUG was raised in 2005, but it was not fixed due to performance and few scenarios; this problem behaves normally in MySQL 8.0.11.

This is the end of this article about the problems encountered in Mysql updating the auto-increment primary key id. For more relevant content about Mysql updating the auto-increment primary key id, please search for previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • How to generate mysql primary key id (self-increment, unique and irregular)
  • Mysql auto-increment primary key id is not processed in this way

<<:  HTML table_Powernode Java Academy

>>:  CSS Transition expands and collapses elements by changing the Height

Recommend

Full analysis of Vue diff algorithm

Table of contents Preface Vue update view patch s...

Example of how to reference environment variables in Docker Compose

In a project, you often need to use environment v...

What should I do if I can't view the source file of a web page?

Q: Whether using Outlook or IE, when you right-cl...

How to write the style of CSS3 Tianzi grid list

In many projects, it is necessary to implement th...

VUE render function usage and detailed explanation

Table of contents Preface The role of render Rend...

Vue realizes the palace grid rotation lottery

Vue implements the palace grid rotation lottery (...

How to use Vuex's auxiliary functions

Table of contents mapState mapGetters mapMutation...

Vue sample code for easily implementing virtual scrolling

Table of contents Preface Rolling principle accom...

CSS pseudo-element::marker detailed explanation

This article will introduce an interesting pseudo...

Simple principles for web page layout design

This article summarizes some simple principles of...

Summarize the User-Agent of popular browsers

1. Basic knowledge: Http Header User-Agent User A...

Mybatis implements SQL query interception and modification details

Preface One of the functions of an interceptor is...

How to implement Hover drop-down menu with CSS

As usual, today I will talk about a very practica...