当前位置:网站首页>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 .
边栏推荐
- 2022 opening H5 mobile page special effects
- 2022-02-05: the k-th decimal number of dictionary order. Given integers n and K, find 1
- How to generate IATA barcode in batch through TXT file
- Some people are crazy, others are running away, and open source software is both hot and cold
- How audio and video technology provides a completely true and stable new experience for the meta universe
- How to set jewelry label paper
- Exploit format string vulnerability in CDE
- Windows system poisoning, SQL Server database file recovery rescue and OA program file recovery
- Implementing StdevP function of Excel with PHP
- Wechat applet camera compressed image is Base64
猜你喜欢

Spark broadcast variables and accumulators (cases attached)

5. concept of ruler method

Vulnhub DC-5

8. greed
What is sitelock? What is the function?

Soft exam information system project manager_ Information system comprehensive testing and management - Senior Information System Project Manager of soft test 027

C language series - Section 4 - arrays

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

How to store, manage and view family photos in an orderly manner?
随机推荐
How to make keyword targeted layout based on search sources?
Add other view components to the audio and video components of the applet
Pond sampling
How audio and video technology provides a completely true and stable new experience for the meta universe
Extract NTDs with volume shadow copy service dit
Learning notes of recommendation system (1) - Collaborative Filtering - Theory
Detailed explanation of online reputation management
Xiamen's hidden gaopuge smart park has finally been uncovered
Win11 client remote ECS black screen
Wechat applet camera compressed image is Base64
Reinforcement learning series (IV) -policygradient example
Docker builds redis3 master-slave cluster and expands the capacity
Solution to the problem of easycvr switching MySQL database traffic statistics cannot be displayed
Spark broadcast variables and accumulators (cases attached)
Windows system poisoning, SQL Server database file recovery rescue and OA program file recovery
SAP WM cannot automatically obtain the special movement mark in the material master data when receiving Po goods?
Aikuai multi dialing + load balancing overlay bandwidth
Troubleshooting and solution of 4K video cannot be played on easydss live video on demand platform
The metauniverse is just a cloak for future technological evolution
Goframe framework (RK boot): Based on cloud native environment, distinguish configuration files (config)