Two different solutions for MySQL insertion and processing of duplicate key values

Source: Internet
Author: User
Tags mysql insert

We all know that when a unique column needs to insert records containing duplicate values on a UNIQUE key, an error is reported during the default insert operation, mySQL inserts two different processing methods for processing duplicate key values. Next we will introduce them separately.

Create two test tables and create the unique constraint on the id column.

 
 
  1. MySQL> create table test1(id int,name varchar(5),type int,primary key(id));   
  2. Query OK, 0 rows affected (0.01 sec)   
  3. MySQL> create table test2(id int,name varchar(5),type int,primary key(id));   
  4. Query OK, 0 rows affected (0.01 sec)   
  5. MySQL> select * from test1;   
  6. +-----+------+------+   
  7. | id | name | type |   
  8. +-----+------+------+   
  9. | 101 | aaa | 1 |   
  10. | 102 | bbb | 2 |   
  11. | 103 | ccc | 3 |   
  12. +-----+------+------+   
  13. 3 rows in set (0.00 sec)   
  14. MySQL> select * from test2;   
  15. +-----+------+------+   
  16. | id | name | type |   
  17. +-----+------+------+   
  18. | 201 | aaa | 1 |   
  19. | 202 | bbb | 2 |   
  20. | 203 | ccc | 3 |   
  21. | 101 | xxx | 5 |   
  22. +-----+------+------+   
  23. 4 rows in set (0.00 sec)  

Method 1 for duplicate key values during MySQL insertion and processing; REPLACE

If the record contains multiple fields and some fields are not assigned a value during insertion, the newly inserted records are empty.

 
 
  1. MySQL> replace into test1(id,name)(select id,name from test2);   
  2. Query OK, 5 rows affected (0.04 sec)   
  3. Records: 4 Duplicates: 1 Warnings: 0   
  4. MySQL> select * from test1;   
  5. +-----+------+------+   
  6. | id | name | type |   
  7. +-----+------+------+   
  8. | 101 | xxx | NULL |   
  9. | 102 | bbb | 2 |   
  10. | 103 | ccc | 3 |   
  11. | 201 | aaa | NULL |   
  12. | 202 | bbb | NULL |   
  13. | 203 | ccc | NULL |   
  14. +-----+------+------+   
  15. 6 rows in set (0.00 sec)  

Note that when you replace a table, if the inserted Table does not specify a column, it will be expressed as NULL, rather than the original content of the table. If the inserted content column is the same as the inserted Table column, no NULL occurs. For example

 
 
  1. MySQL> replace into test1(id,name,type)(select id,name,type from test2);   
  2. Query OK, 8 rows affected (0.04 sec)   
  3. Records: 4 Duplicates: 4 Warnings: 0   
  4. MySQL> select * from test1;   
  5. +-----+------+------+   
  6. | id | name | type |   
  7. +-----+------+------+   
  8. | 101 | xxx | 5 |   
  9. | 102 | bbb | 2 |   
  10. | 103 | ccc | 3 |   
  11. | 201 | aaa | 1 |   
  12. | 202 | bbb | 2 |   
  13. | 203 | ccc | 3 |   
  14. +-----+------+------+   
  15. 6 rows in set (0.00 sec)  

If the columns in the inserted Table need to be retained during the INSERT operation and only the specified columns are updated, the second method can be used.

Method 2 For MySQL to insert duplicate key values 2. INSERT INTO ON DUPLICATE KEY UPDATE

Duplicate update operations are found. Based on the original record, the specified field content is updated, and other fields are retained. For example, I only want to insert the id and name fields of the test2 table, but keep the type FIELD OF THE test1 table:

 
 
  1. MySQL> replace into test1(id,name,type)(select id,name,type from test2);   
  2. Query OK, 8 rows affected (0.04 sec)   
  3. Records: 4 Duplicates: 4 Warnings: 0   
  4. MySQL> select * from test1;   
  5. +-----+------+------+   
  6. | id | name | type |   
  7. +-----+------+------+   
  8. | 101 | xxx | 5 |   
  9. | 102 | bbb | 2 |   
  10. | 103 | ccc | 3 |   
  11. | 201 | aaa | 1 |   
  12. | 202 | bbb | 2 |   
  13. | 203 | ccc | 3 |   
  14. +-----+------+------+   
  15. 6 rows in set (0.00 sec)  

If you only want to INSERT data that is not in the original table, you can use other MySQL INSERT methods to process duplicate key values.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.