当前位置:网站首页>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 .
边栏推荐
- 6. template for integer and real number dichotomy
- Daily shift series: memory problem of primary online service
- Salesforce file (II) custom development fileUpload
- Redis source code reading (I) general overview
- Scanning technology (getting started with web security 06)
- Docker builds MySQL master-slave
- Markdown - enter a score (typora, latex)
- SAP WM cannot automatically obtain the special movement mark in the material master data when receiving Po goods?
- How to set up an H5 demo of easyplayer locally to play h265 video streams?
- February 3, 2022: a group of people (two or more) want to meet at the same place
猜你喜欢

C language series - Section 4 - arrays
What is sitelock? What is the function?

Vulnhub DC-5

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

Spark broadcast variables and accumulators (cases attached)

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

5. concept of ruler method

6. template for integer and real number dichotomy

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

8. greed
随机推荐
Transformation solution of digital intelligent supply chain platform for project management in engineering industry
Reading redis source code (IV) command request processing flow
January 31, 2022: Maze III. There is a ball in the maze of open spaces and walls. ball
Troubleshooting and optimization of easynvr version 5.0 Video Square snapshot not displayed
Use Sakura FRP intranet penetration service to build your own website / game server
2D visual empowerment smart water green intensive development
Goframe framework (RK boot): rapid configuration of server CORS
Web components series (I) - Overview
Markdown - enter a score (typora, latex)
How to generate DataMatrix code in batch through TXT file
[SaaS examination certification] apaas_ Tencent Qianfan magic pen
February 2, 2022: the closest binary search tree value II. Given a non empty two
Pond sampling
Use of apicloud AVM framework list component list view and flex layout tutorial
How to make keyword targeted layout based on search sources?
How to locate memory leaks
Detailed explanation of label smoothing and implementation of pytorch tenorflow
How to set up an H5 demo of easyplayer locally to play h265 video streams?
Deep analysis of time complexity
Handlebars dynamic adjustment