The solution of duplicate entry ‘for key’ primary ‘when inserting data in MySQL

The problem is the key duplication encountered in inserting data table.
solution:
1. Using ignore, if there are duplicate values in the inserted record, the record row with duplicate values will be ignored, and the insertion of other rows will not be affected.

INSERT IGNORE INTO Table_name(…..) VALUES(1,1),(2,2),(3,3);

2. Use replace to delete the duplicate record row in the table before inserting when the inserted record encounters primary key or unique duplicate

REPLACE INTO Table_name() VALUES(1,1),(2,2),(3,3)

3. Using values after on duplicate key update refers to the value of the inserted record, while not using values refers to the value of the table itself. The record of the subsequent update is the ID of the duplicate primary key or unique key where.

NSERT TO Table_name() VALUES(1,1),(1,2) ON DUPLICATE KEY UPDATE NAME1=NAME1+1;

Download: https://blog.csdn.net/zhangyr_student/article/details/80119238


Read More: