当前位置:网站首页>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 .
边栏推荐
- Source code analysis | activity setcontentview I don't flash
- Pyqt5 installation and use
- Markdown - enter a score (typora, latex)
- Add other view components to the audio and video components of the applet
- Salesforce file (II) custom development fileUpload
- PHP Base64 image processing Encyclopedia
- Practice and exploration of vivo live broadcast application technology
- How to gracefully solve the problem of platform font adaptation
- Vulnhub DC-5
- Construction and exploration of vivo database and storage platform
猜你喜欢

6. template for integer and real number dichotomy

C language series - Section 4 - arrays

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

8. greed

Spark broadcast variables and accumulators (cases attached)
What is sitelock? What is the function?

5. concept of ruler method

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

Vulnhub DC-5

Soft exam information system project manager_ Contract Law_ Copyright_ Implementation Regulations - Senior Information System Project Manager of soft exam 030
随机推荐
Pytest common summary
C language series - Section 4 - arrays
[target tracking] open source | polytrack: use boundary polygons to quickly track and segment multiple targets, instead of bounding box and mask tracking
Automatically update site statistics with actions
The metauniverse is just a cloak for future technological evolution
How to generate IATA barcode in batch through TXT file
Golang string comparison
Reading redis source code (II) underlying data structure
What is a smart farm?
How to use fortress on mobile devices
Wechat applet camera compressed image is Base64
How to make keyword targeted layout based on search sources?
January 31, 2022: Maze III. There is a ball in the maze of open spaces and walls. ball
Salesforce fileUpload (I) how to configure the file upload function
Optimization method of live weak network
DNS Service Setup
862. triple sorting
Biological psychiatry: defining individualized functional neuroanatomy for precision psychiatry
How to set jewelry label paper
Simple implementation of promise basic method