当前位置:网站首页>Methods for MySQL to avoid inserting duplicate records
Methods for MySQL to avoid inserting duplicate records
2022-06-23 02:57:00 【It workers】
mysql In case of primary key conflict or unique key conflict , Depending on the insertion strategy , There are generally three ways to avoid . 1、insert ignore 2、replace into 3、insert on duplicate key update
Be careful , Unless the watch has a PRIMARY KEY or UNIQUE Indexes , otherwise , There is no point in using the above three statements , With the use of simple INSERT INTO identical .
One 、insert ignore
insert ignore Data that already exists in the database will be ignored ( Based on the primary key or unique index ), If there is no data in the database , Just insert new data , If you have data, skip this data .
Case:
The table structure is as follows :
root:test> show create table t3\G
*************************** 1. row ***************************
Table: t3
Create Table: CREATE TABLE `t3` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`c1` int(11) DEFAULT NULL,
`c2` varchar(20) DEFAULT NULL,
`c3` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `uidx_c1` (`c1`)
) ENGINE=InnoDB AUTO_INCREMENT=18 DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
root:test> select * from t3;
+----+------+------+------+
| id | c1 | c2 | c3 |
+----+------+------+------+
| 1 | 1 | a | 1 |
| 2 | 2 | a | 1 |
| 8 | NULL | NULL | 1 |
| 14 | 4 | bb | NULL |
| 17 | 5 | cc | 4 |
+----+------+------+------+
5 rows in set (0.00 sec)Test inserting data with unique key conflicts
root:test> insert ignore into t3 (c1,c2,c3) values(5,'cc',4),(6,'dd',5); Query OK, 1 row affected, 1 warning (0.01 sec) Records: 2 Duplicates: 1 Warnings: 1
as follows , You can see that only (6,'dd',5) This article , At the same time, there is one warning Prompt for duplicate values .
root:test> show warnings; +---------+------+---------------------------------------+ | Level | Code | Message | +---------+------+---------------------------------------+ | Warning | 1062 | Duplicate entry '5' for key 'uidx_c1' | +---------+------+---------------------------------------+ 1 row in set (0.00 sec) root:test> select * from t3; +----+------+------+------+ | id | c1 | c2 | c3 | +----+------+------+------+ | 1 | 1 | a | 1 | | 2 | 2 | a | 1 | | 8 | NULL | NULL | 1 | | 14 | 4 | bb | NULL | | 17 | 5 | cc | 4 | | 18 | 6 | dd | 5 | +----+------+------+------+ 6 rows in set (0.00 sec)
Re query the table structure , It was found that although only one record was added , however AUTO_INCREMENT Still added 2 individual (18 become 20)
root:test> show create table t3\G
*************************** 1. row ***************************
Table: t3
Create Table: CREATE TABLE `t3` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`c1` int(11) DEFAULT NULL,
`c2` varchar(20) DEFAULT NULL,
`c3` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `uidx_c1` (`c1`)
) ENGINE=InnoDB AUTO_INCREMENT=20 DEFAULT CHARSET=utf8
1 row in set (0.00 sec)Two 、replace into
- replace into First try inserting data into the table . If you find that this row of data already exists in the table ( Based on the primary key or unique index ) Then delete this row of data first , Then insert the new data , otherwise , Insert new data directly .
- Use replace into, You must have delete and insert jurisdiction
Case:
root:test> show create table t3\G
*************************** 1. row ***************************
Table: t3
Create Table: CREATE TABLE `t3` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`c1` int(11) DEFAULT NULL,
`c2` varchar(20) DEFAULT NULL,
`c3` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `uidx_c1` (`c1`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
root:test> select * from t3;
+----+------+--------+------+
| id | c1 | c2 | c3 |
+----+------+--------+------+
| 1 | 1 | cc | 4 |
| 2 | 2 | dd | 5 |
| 3 | 3 | qwewqe | 3 |
+----+------+--------+------+
3 rows in set (0.00 sec)Insert a and record id=3 Unique key exists ( Column c1) Conflicting data
root:test> replace into t3 (c1,c2,c3) values(3,'new',8); Query OK, 2 rows affected (0.02 sec) root:test> select * from t3; +----+------+------+------+ | id | c1 | c2 | c3 | +----+------+------+------+ | 1 | 1 | cc | 4 | | 2 | 2 | dd | 5 | | 4 | 3 | new | 8 | +----+------+------+------+ 3 rows in set (0.00 sec)
You can see the original id=3,c1=3 The record of is missing , A new one has been added id=4,c1=3 The record of . replace into A number will be returned after the statement is executed , To indicate the number of rows affected . This number is the sum of the number of rows deleted and inserted , In the example above 2 rows affected .
3、 ... and 、insert on duplicate key update
- If in insert into At the end of the statement is specified on duplicate key update, And inserting a row will result in a UNIQUE Index or PRIMARY KEY Duplicate value in , Then execute... On the line with duplicate values UPDATE; If it doesn't cause duplicate problems , Insert a new line , With ordinary insert into equally .
- Use insert into, You must have insert and update jurisdiction
- If a new record is inserted , The value of the affected row displays 1; If the original record is updated , The value of the affected row displays 2; If the value is the same before and after the record is updated , The value of the number of affected rows is displayed 0
Case:
root:test> show create table t3\G
*************************** 1. row ***************************
Table: t3
Create Table: CREATE TABLE `t3` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`c1` int(11) DEFAULT NULL,
`c2` varchar(20) DEFAULT NULL,
`c3` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `uidx_c1` (`c1`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
root:test> select * from t3;
+----+------+------+------+
| id | c1 | c2 | c3 |
+----+------+------+------+
| 1 | 1 | fds | 4 |
| 2 | 2 | ytu | 3 |
| 3 | 3 | czx | 5 |
+----+------+------+------+
3 rows in set (0.00 sec)Insert a and record id=3 Unique key exists ( Column c1) Conflicting data
root:test> insert into t3(c1,c2,c3) values (3,'new',5) on duplicate key update c1=c1+3; Query OK, 2 rows affected (0.01 sec) root:test> select * from t3; +----+------+------+------+ | id | c1 | c2 | c3 | +----+------+------+------+ | 1 | 1 | fds | 4 | | 2 | 2 | ytu | 3 | | 3 | 6 | czx | 5 | +----+------+------+------+ 3 rows in set (0.00 sec)
You can see ,id=3 The record of has changed ,c1= The original c1+3, The other columns have not changed .
Conclusion :
- All three methods can avoid the problem of insertion failure caused by duplicate primary keys or unique indexes .
- insert ignore Can ignore duplicate data , Insert only non duplicate data .
- replace into and insert ... on duplicate key update, Replace the original duplicate data , The difference lies in replace into After deleting the original line , Insert new line , In case of self increase id, This will cause self increase id Changes ;insert ... on duplicate key update When encountering duplicate lines , The original row will be updated directly , Which fields to update and how to update them , Depending on update The following sentence .
边栏推荐
- Wechat applet camera compressed image is Base64
- PHP Base64 image processing Encyclopedia
- Aikuai multi dialing + load balancing overlay bandwidth
- Section 6: basic configuration I of spingboot
- Summary of easy-to-use MySQL interview questions (Part 1)
- February 1, 2022: painting house II. If there is a row of houses, N in total, each
- Hypervisor Necromancy; Recover kernel protector (1)
- The commercial s2b2b e-commerce platform of aquatic industry improves the competitiveness of enterprises and creates a strong engine for industrial development
- Troubleshooting and optimization of easynvr version 5.0 Video Square snapshot not displayed
- Cve-2021-4034 reappearance
猜你喜欢

C language series - Section 4 - arrays

8. greed

Soft exam information system project manager_ Information system comprehensive testing and management - Senior Information System Project Manager of soft test 027
What is sitelock? What is the function?

5. concept of ruler method

How to store, manage and view family photos in an orderly manner?

Spark broadcast variables and accumulators (cases attached)

Soft exam information system project manager_ Contract Law_ Copyright_ Implementation Regulations - Senior Information System Project Manager of soft exam 030

6. template for integer and real number dichotomy

Vulnhub DC-5
随机推荐
Calling applet demo modifying the default large screen view
Goframe framework (RK boot): Based on cloud native environment, distinguish configuration files (config)
What if the software gets stuck and cannot end the process?
How does the easyplayer streaming video player set up tiling?
Storage resources revitalize the system and redefine "hyper fusion"
How to locate memory leaks
Goframe framework (RK boot): fast implementation of server-side JWT verification
How to batch print serial and repeated barcode data
Markdown - enter a score (typora, latex)
Why do I use index, query or slow?
How to generate DataMatrix code in batch through TXT file
Hypervisor Necromancy; Recover kernel protector (2)
CVE-2021-21973 Vmware Vcenter SSRF POC
Reading redis source code (II) underlying data structure
Quic implementation in rust --- Quinn
How to customize a finished label template
2022-01-30: minimum good base. For a given integer n, if K (k) of n
Blue screen and abnormal downtime DMP file and system log collection
Function recursion and iteration
Detailed explanation of various networking modes of video monitoring platform