When solving MYSQL batch insert, UUID_SHORT appeared 1062 -Duplicate entry 'xxx' for key 'PRIMARY'

1. Problem description

When MYSQL batch inserts, 1062 -Duplicate entry 'xxx' for key 'PRIMARY' appears when using UUID_SHORT
insert image description here

2. Scene description:

It can be inserted normally in the test environment, but it fails when it goes to production. How to deal with this need?

2.1 Method 1:

Answer: If the data inserted in batches can be obtained, you can try to generate the data in the test environment first, convert it into SQL statements, and take it to production for execution, so that the problem can be solved. As shown in the figure below: the data generated in the test environment is imported into SQL through the tool, and then executed in production.
insert image description here
insert image description here

2.2 Method 2:

Answer: Most of the time, it is impossible for us to have access to the data in the production environment, but [duplicate primary key] will cause us to be unable to complete the current task, so we can consider using the following method, which is also recommended instead of 2.1 .

(1) The SQL is as follows, mainly customizing a variable named 'number', before each insert, the primary key id value is +1

set @number = 1;
INSERT INTO '你的表名'
SELECT @number := @number+1 AS id,CONCAT('解决MySQL UUID_SHORT批量生成主键重复问题:',u.name) 
FROM '你的表名' AS `u`

(2) Original table data:
insert image description here
(3) Screenshot of successful execution:

insert image description here
insert image description here

Guess you like

Origin blog.csdn.net/qq_40600379/article/details/128999174