Several ways MYSQL inserts handle duplicate key values

  • 2020-05-14 05:03:41
  • OfStack

Create the unique constraint on the id column by creating two test tables.
mysql > create table test1(id int,name varchar(5),type int,primary key(id));
Query OK, 0 rows affected (0.01 sec)
mysql > create table test2(id int,name varchar(5),type int,primary key(id));
Query OK, 0 rows affected (0.01 sec)
mysql > select * from test1;
+-----+------+------+
| id | name | type |
+-----+------+------+
| 101 | aaa | 1 |
| 102 | bbb | 2 |
| 103 | ccc | 3 |
+-----+------+------+
3 rows in set (0.00 sec)
mysql > select * from test2;
+-----+------+------+
| id | name | type |
+-----+------+------+
| 201 | aaa | 1 |
| 202 | bbb | 2 |
| 203 | ccc | 3 |
| 101 | xxx | 5 |
+-----+------+------+
4 rows in set (0.00 sec)
1, REPLACE INTO
If a record has multiple fields, the newly inserted record will be empty if some fields are not assigned at the time of insertion.
mysql > replace into test1(id,name)(select id,name from test2);
Query OK, 5 rows affected (0.04 sec)
Records: 4 Duplicates: 1 Warnings: 0
mysql > select * from test1;
+-----+------+------+
| id | name | type |
+-----+------+------+
| 101 | xxx | NULL |
| 102 | bbb | 2 |
| 103 | ccc | 3 |
| 201 | aaa | NULL |
| 202 | bbb | NULL |
| 203 | ccc | NULL |
+-----+------+------+
6 rows in set (0.00 sec)
Note that when you replace, if the inserted table has no columns specified, it will be represented by NULL instead of the original contents of the table. If the inserted content column is like the inserted table column 1, NULL does not appear. For example,
mysql > replace into test1(id,name,type)(select id,name,type from test2);
Query OK, 8 rows affected (0.04 sec)
Records: 4 Duplicates: 4 Warnings: 0
mysql > select * from test1;
+-----+------+------+
| id | name | type |
+-----+------+------+
| 101 | xxx | 5 |
| 102 | bbb | 2 |
| 103 | ccc | 3 |
| 201 | aaa | 1 |
| 202 | bbb | 2 |
| 203 | ccc | 3 |
+-----+------+------+
6 rows in set (0.00 sec)
The second method can be used if, for INSERT, you want to keep the columns that were inserted into the table and update only the specified columns.
2. INSERT INTO ON DUPLICATE KEY UPDATE
An update operation was found to be repeated. Update the contents of the specified fields on the basis of the original records, and keep the contents of other fields. For example, I only want to insert id,name fields of test2 table, but I want to keep type fields of test1 table:
mysql > insert into test1(id,name,type)(select id,name,type from test2) on DUPLICATE KEY UPDATE test1.name=test2.name;
Query OK, 5 rows affected (0.04 sec)
Records: 4 Duplicates: 1 Warnings: 0
mysql > select * from test1;
+-----+------+------+
| id | name | type |
+-----+------+------+
| 101 | xxx | 1 |
| 102 | bbb | 2 |
| 103 | ccc | 3 |
| 203 | ccc | 3 |
| 202 | bbb | 2 |
| 201 | aaa | 1 |
+-----+------+------+
6 rows in set (0.00 sec)
If INSERT only wants to insert data that is not in the original table, then you can use the third method.
3, IGNORE INTO
To determine if there is, there is no insert, otherwise insert. It is easy to understand that MySQL will not attempt to execute this statement when inserting a violation of the exclusives constraint. Such as:
mysql > insert ignore into test1(id,name,type)(select id,name,type from test2);
Query OK, 3 rows affected (0.01 sec)
Records: 4 Duplicates: 1 Warnings: 0
mysql > select * from test1;
+-----+------+------+
| id | name | type |
+-----+------+------+
| 101 | aaa | 1 |
| 102 | bbb | 2 |
| 103 | ccc | 3 |
| 203 | ccc | 3 |
| 202 | bbb | 2 |
| 201 | aaa | 1 |
+-----+------+------+
6 rows in set (0.00 sec)

Related articles: