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

VMware kali virtual machine environment configuration method

1|0 Compile the kernel (1) Run the uname -r comma...

linux No space left on device 500 error caused by inode fullness

What is an inode? To understand inode, we must st...

Detailed installation tutorial of zabbix 4.04 (based on CentOS 7.6)

1. Preparation before installation: 1.1 Install J...

A brief discussion on VUE uni-app custom components

1. Parent components can pass data to child compo...

Example code for configuring monitoring items and aggregated graphics in Zabbix

1. Install Zabbix Agent to monitor the local mach...

The basic use of html includes links, style sheets, span and div, etc.

1. Links Hypertext links are very important in HTM...

MySQL column to row conversion, method of merging fields (must read)

Data Sheet: Column to row: using max(case when th...

How to configure the pdflatex environment in docker

Technical Background Latex is an indispensable to...

Combining XML and CSS styles

student.xml <?xml version="1.0" enco...

How to implement horizontal bar chart with percentage in echarts

Table of contents Example Code Rendering Code Ana...

Window.name solves the problem of cross-domain data transmission

<br />Original text: http://research.microso...

mysql splits a row of data into multiple rows based on commas

Table of contents Separation effect Command line ...