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

Nginx rtmp module compilation arm version problem

Table of contents 1. Preparation: 2. Source code ...

Ubuntu16.04 builds php5.6 web server environment

Ubuntu 16.04 installs the PHP7.0 environment by d...

JavaScript to achieve elastic navigation effect

This article shares the specific code for JavaScr...

Summary of WEBAPP development skills (notes for mobile website development)

1. To develop web responsively, the page must ada...

Solution to the problem of eight hours difference in MySQL insertion time

Solve the problem of eight hours time difference ...

React-Native environment setup and basic introduction

Environment Preparation 1. Environment Constructi...

Implement 24+ array methods in JavaScript by hand

Table of contents 1. Traversal Class 1. forEach 2...

CSS performance optimization - detailed explanation of will-change usage

will-change tells the browser what changes will h...

Detailed explanation of MySQL multi-table join query

Table of contents Multi-table join query Inner Jo...

Detailed explanation of Vue lazyload picture lazy loading example

Documentation: https://github.com/hilongjw/vue-la...

JavaScript implements front-end countdown effect

This article shares the specific code of JavaScri...

Zen Coding Easy and fast HTML writing

Zen Coding It is a text editor plugin. In a text ...

Detailed explanation of the pitfalls of mixing npm and cnpm

Table of contents cause reason Introduction to NP...