MYSQL insertion processing method is repeated several key-values

When a unique column on insertion of a key record UNIQUE duplicate values, the default insert 1062 when the error will be reported, MYSQL Based three different treatment methods, we introduced below.

First establish two test table, create a unique constraint on the id column.
MySQL> Create Table test1 (int ID, name VARCHAR (. 5), int type, Primary Key (ID));
Query the 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
find duplicate delete and then insert, if there is more than one field record, when inserted, if any field is not assigned, then the newly inserted record these fields are empty.
MySQL> Replace INTO test1 (ID, name) (SELECT ID, name from test2);
Query the OK,. 5 rows affected (0.04 sec)
Records: Duplicates. 4: Warnings. 1: 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 inserted table if the column is not specified, will be represented by NULL, rather than the original contents of the table. If you insert the contents of the column and inserted as table columns, NULL will not appear. For example
MySQL> Replace INTO test1 (ID, name, type) (SELECT ID, name, type from test2);
Query the OK,. 8 rows affected (0.04 sec)
Records: Duplicates. 4: Warnings. 4: 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)

If INSERT when the need to retain is inserted into the table columns, just update the specified column, then you can use the second method.

2, INSERT INTO ON DUPLICATE KEY UPDATE
Duplicate update operation. On the basis of the original record, update the contents of the specified field, the contents of other fields reserved. For example, I just want to insert test2 table id, name field, but to retain the type field test1 table:
MySQL> INSERT INTO test1 (id, name, type) (the SELECT id, name, type from test2) ON DUPLICATE KEY UPDATE test1. = test2.name name;
Query the OK,. 5 rows affected (0.04 sec)
Records: Duplicates. 4: Warnings. 1: 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 when inserting data just does not have the original table, you can use the third method.

3, IGNORE INTO
determine whether there is, there is not inserted, or inserted. It is easy to understand, when inserted, the only constraint violation, MySQL will not attempt to execute this statement. For example:
MySQL> INSERT INTO test1 the ignore (ID, name, type) (SELECT ID, name, type from test2);
Query the OK,. 3 rows affected (0.01 sec)
Records: Duplicates. 4: Warnings. 1: 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)

Guess you like

Origin blog.51cto.com/14439341/2418562