The promissory note article is reprinted, just record sql for myself.
MySQL bulk insert encounters a unique index avoidance method (avoid importing duplicate data) and does
not avoid importing duplicate data. It is recommended to establish a unique index to
prevent bulk inserts. When encountering a unique index, you can use the following 3 methods to avoid
(1) Import difference data, ignore duplicate data, use IGNORE INTO
(2) Import and overwrite duplicate data, use REPLACE INTO
(3) Import unspecified fields of duplicate data, use INSERT INTO ON DUPLICATE KEY UPDATE
Table test1
id user_id user_name user_type
1 101 101 1
2 102 102 2
3 103 103 3
Table test2
id user_id user_name user_type
1 201 201 1
2 202 202 2
3 203 203 3
4 101 204 4
(1) Use the first method to import, the use of IGNORE INTO :
INSERT IGNORE INTO test1(user_id,user_name,user_type)
SELECT user_id,user_name,user_type FROM test2;
The results of table test1 are as follows
id user_id user_name user_type
1 101 101 1
2 102 102 2
3 103 103 3
4 201 201 1
5 202 202 2
6 203 203 3
The records with id=4 in Table 2 are ignored and not imported.
(Ii) The second way is introduced, the REPLACE the INTO using
the REPLACE the INTO test1 (user_id, USER_NAME, user_type)
the SELECT user_id, USER_NAME, user_type the FROM test2;
The results of table test1 are as follows
id user_id user_name user_type
2 102 102 2
3 103 103 3
4 201 201 1
5 202 202 2
6 203 203 3
7 101 204 4
From the above figure, we can find that the data of user_id = 101 has changed. If duplicates are found during import, delete and insert them.
If no column is specified when importing, the data of the unspecified column will be replaced with (Null)
REPLACE INTO test1(user_id,user_name)
SELECT user_id,user_name FROM test2;
the results of table test1 are as follows
id user_id user_name user_type
2 102 102 2
3 103 103 3
4 201 201 (Null)
5 202 202 (Null)
6 203 203 (Null)
7 101 204 (Null)
(C) introducing the use of a third embodiment, the INSERT the DUPLICATE the INTO the ON KEY the UPDATE using
the INSERT the INTO test1 (user_id, USER_NAME, user_type)
the SELECT user_id, USER_NAME, the FROM test2 user_type
the ON KEY the DUPLICATE the UPDATE
test1.user_name = test2.user_name;
The results of table test1 are as follows
id user_id user_name user_type
1 101 204 1
2 102 102 2
3 103 103 3
4 201 201 1
5 202 202 2
6 203 203 3
As shown above, only the user_name with user_id=101 has changed, but the user_type field of the test1 table is retained. If you update multiple fields, just append them later.
INSERT INTO test1(user_id,user_name,user_type)
SELECT user_id,user_name,user_type FROM test2
ON DUPLICATE KEY UPDATE
test1.user_name = test2.user_name,
test1.user_type = test2.user_type;
The promissory note article is reprinted, just record sql for myself.
MySQL bulk insert encounters a unique index avoidance method (avoid importing duplicate data) and does
not avoid importing duplicate data. It is recommended to establish a unique index to
prevent bulk inserts. When encountering a unique index, you can use the following 3 methods to avoid
(1) Import difference data, ignore duplicate data, use IGNORE INTO
(2) Import and overwrite duplicate data, use REPLACE INTO
(3) Import unspecified fields of duplicate data, use INSERT INTO ON DUPLICATE KEY UPDATE
Table test1
id user_id user_name user_type
1 101 101 1
2 102 102 2
3 103 103 3
Table test2
id user_id user_name user_type
1 201 201 1
2 202 202 2
3 203 203 3
4 101 204 4
(1) Use the first method to import, the use of IGNORE INTO :
INSERT IGNORE INTO test1(user_id,user_name,user_type)
SELECT user_id,user_name,user_type FROM test2;
The results of table test1 are as follows
id user_id user_name user_type
1 101 101 1
2 102 102 2
3 103 103 3
4 201 201 1
5 202 202 2
6 203 203 3
The records with id=4 in Table 2 are ignored and not imported.
(Ii) The second way is introduced, the REPLACE the INTO using
the REPLACE the INTO test1 (user_id, USER_NAME, user_type)
the SELECT user_id, USER_NAME, user_type the FROM test2;
The results of table test1 are as follows
id user_id user_name user_type
2 102 102 2
3 103 103 3
4 201 201 1
5 202 202 2
6 203 203 3
7 101 204 4
From the above figure, we can find that the data of user_id = 101 has changed. If duplicates are found during import, delete and insert them.
If no column is specified when importing, the data of the unspecified column will be replaced with (Null)
REPLACE INTO test1(user_id,user_name)
SELECT user_id,user_name FROM test2;
the results of table test1 are as follows
id user_id user_name user_type
2 102 102 2
3 103 103 3
4 201 201 (Null)
5 202 202 (Null)
6 203 203 (Null)
7 101 204 (Null)
(C) introducing the use of a third embodiment, the INSERT the DUPLICATE the INTO the ON KEY the UPDATE using
the INSERT the INTO test1 (user_id, USER_NAME, user_type)
the SELECT user_id, USER_NAME, the FROM test2 user_type
the ON KEY the DUPLICATE the UPDATE
test1.user_name = test2.user_name;
The results of table test1 are as follows
id user_id user_name user_type
1 101 204 1
2 102 102 2
3 103 103 3
4 201 201 1
5 202 202 2
6 203 203 3
As shown above, only the user_name with user_id=101 has changed, but the user_type field of the test1 table is retained. If you update multiple fields, just append them later.
INSERT INTO test1(user_id,user_name,user_type)
SELECT user_id,user_name,user_type FROM test2
ON DUPLICATE KEY UPDATE
test1.user_name = test2.user_name,
test1.user_type = test2.user_type;