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

MySQL 5.6 root password modification tutorial

1. After installing MySQL 5.6, it cannot be enabl...

The difference between div and span in HTML (commonalities and differences)

Common points: The DIV tag and SPAN tag treat som...

Native JS implementation of loading progress bar

This article shares a dynamic loading progress ba...

W3C Tutorial (13): W3C WSDL Activities

Web Services are concerned with application-to-ap...

How to reasonably use the redundant fields of the database

privot is the intermediate table of many-to-many ...

Echarts Basic Introduction: General Configuration of Bar Chart and Line Chart

1Basic steps of echarts Four Steps 1 Find the DOM...

HTML implements read-only text box and cannot modify the content

Without further ado, I will post the code for you...

Nginx reverse proxy forwards port 80 requests to 8080

Let's first understand a wave of concepts, wh...

Summary of 4 ways to add users to groups in Linux

Preface Linux groups are organizational units use...

Docker stop stops/remove deletes all containers

This article mainly introduces Docker stop/remove...

MySQL slow query log configuration and usage tutorial

Preface MySQL slow query log is a function that w...

jQuery realizes dynamic particle effect

This article shares the specific code of jQuery t...