Mysql auto-increment primary key id is not processed in this way

Mysql auto-increment primary key id is not processed in this way

Mysql auto-increment primary key id does not increase step by step

1. Introduction

When adding data to the MySQL database and using ON DUPLICATE KEY UPDATE to update the data, the ID may not increase step by step, but increase intermittently.

For example, if the id is 10, the next time it is added, it may be 15 or another number. The interval between the two numbers is the number of times ON DUPLICATE KEY UPDATE is executed, that is, ON DUPLICATE KEY UPDATE increments the primary key of the table by one when executing the update.

As shown in the figure

2. Problem Introduction

When adding and modifying the same table, I used two mapper interface methods, that is, I used two ON DUPLICATE KEY UPDATE operations, which will cause the primary key of the table to increment when you update it. The next time you insert, it will not increase step by step.

3. Solution

Split the insert and update of the database.

Advantages and disadvantages of auto-increment primary key

Auto-increment primary key

This method uses the auto-increment numeric field provided by the database as the auto-increment primary key. Its advantages are:

(1) The database is automatically numbered, fast, and incrementally increases, and is stored in order, which is very beneficial for retrieval;

(2) Digital type, which takes up little space, is easy to sort, and is convenient to pass in the program;

(3) If you add records through a non-system, you do not need to specify this field and do not need to worry about primary key duplication.

In fact, its disadvantages come from its advantages, and the disadvantages are as follows:

(1) Because of the automatic growth, it is troublesome to manually insert a record with a specified ID. Especially when the system is integrated with other systems and data import is required, it is difficult to ensure that the ID of the original system does not conflict with the primary key (assuming that the old system is also digital). Especially when a new system is launched, the old and new systems exist in parallel, and the databases are heterogeneous and require two-way synchronization, the auto-increment primary key will be your nightmare;

(2) During system integration or cutover, if the primary keys of the new and old systems are different and are numeric, the primary key data type will be modified, which will also lead to modifications to other tables associated with foreign keys, with equally serious consequences.

(3) If the system is also digital, in order to distinguish between new and old data during import, you may want to add a character identifier (such as "o", old) before the primary key of the old data to indicate that it is old data. Then the automatically increasing digital type faces another challenge.

The above is my personal experience. I hope it can give you a reference. I also hope that you will support 123WORDPRESS.COM.

You may also be interested in:
  • Tutorial on primary key in MySQL and setting its auto-increment
  • Example of changing the auto-increment primary key type from int to char in mysql
  • Solution to running out of MySQL's auto-increment ID (primary key)
  • What to do if the auto-increment primary key in MySQL is used up
  • MySQL 8 new features: detailed explanation of persistence of auto-increment primary key
  • Example analysis of mysql non-primary key self-increment usage
  • Detailed explanation of the implementation of MySQL auto-increment primary key
  • Why is the MySQL auto-increment primary key not continuous?

<<:  Implementation of master-slave replication in docker compose deployment

>>:  Solution to invalid margin-top of elements in div tags

Recommend

Docker installation Nginx tutorial implementation illustration

Let’s install Nginx and try it out. Please note t...

Detailed tutorial on how to install MySQL 5.7.18 in Linux (CentOS 7) using YUM

The project needs to use MySQL. Since I had alway...

Complete steps to quickly build a vue3.0 project

Table of contents 1. We must ensure that the vue/...

Solve the conflict between docker and vmware

1. Docker startup problem: Problem Solved: You ne...

Detailed explanation of various usages of proxy_pass in nginx

Table of contents Proxy forwarding rules The firs...

WeChat applet implements a simple calculator

WeChat applet's simple calculator is for your...

How to debug loader plugin in webpack project

Recently, when I was learning how to use webpack,...

5 super useful open source Docker tools highly recommended

Introduction The Docker community has created man...

Detailed steps for setting up and configuring nis domain services on Centos8

Table of contents Introduction to NIS Network env...

Explain MySQL's binlog log and how to use binlog log to recover data

As we all know, binlog logs are very important fo...

MySQL service and database management

Table of contents 1. Start and stop service instr...

JavaScript to implement voice queuing system

Table of contents introduce Key Features Effect d...