当前位置:网站首页>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 .
原网站

版权声明
本文为[It workers]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/01/202201261219134581.html