[Switch] if the primary key conflict occurs, update it. Otherwise, insert (on duplicate key update)

Source: Internet
Author: User

MySQL "on duplicate key update" Syntax
If the on duplicate key update is specified at the end of the insert statement and the duplicate value appears in a unique index or primary key after the row is inserted, update is executed on the row with the duplicate value; if the unique value column is not duplicate, a new row is inserted.
For example, if column A is a primary key or has a unique index with a value of 1, the following two statements have the same effect:


Insert into table (a, c) values (1, 3) on duplicate key update C = C + 1;
Update table set C = C + 1 where a = 1;


If a row is inserted as a new record, the value of the affected row is 1. If the original record is updated, the value of the affected row is 2.
This syntax can also be used as follows:
If you insert multiple rows of records (assuming a is the primary key or a is a unique index column ):


Insert into table (a, c) values (1, 3), (1, 7) on duplicate key update C = C + 1;


After execution, the value of C is changed to 4 (the second repeat with the first one, and C is on the original value + 1 ).


Insert into table (a, c) values (1, 3), (1, 7) on duplicate key update c = values (C );


After the execution, the value of C will change to 7 (the second repeat with the first repeat, and C will take the repeat value 7 directly ).
Note: On duplicate key update is only a special MySQL syntax, not a standard SQL syntax!
This syntax is applicable to scenarios where you need to determine whether a record exists, and if no record exists, insert the statement and update the statement.

Insert into .. on duplicate key to update multiple rows
If on duplicate key update is specified at the end of the insert statement, and duplicate values appear in a unique index or primary key after the row is inserted, the old row update is executed; if the unique value column is not duplicate, a new row is inserted. For example, if column A is defined as unique and contains a value of 1, the following two statements have the same effect:


Insert into table (A, B, C)
Values (1, 2, 3) on duplicate key update C = C + 1;
Update table set C = C + 1 where a = 1;


If a row is inserted as a new record, the value of the affected row is 1. If the original record is updated, the value of the affected row is 2.
For more information about the insert into... on duplicate key function, see MySQL reference documentation: 13.2.4. Insert syntax.

Now the question is, how can I specify the value of the field after on duplicate key update if multiple rows are inserted? You must know that only one on duplicate key update exists in an insert statement. It will update a row of records or all rows to be updated. This problem has plagued me for a long time. In fact, all problems are solved by using the values () function.

For example, field a is defined as unique, and records (, 9) and (, 1) exist in the table of the original database ), if the value of the inserted record is the same as the original record, the original record is updated; otherwise, a new row is inserted:


Insert into table (a, B, c) Values
(1, 2, 3 ),
(2, 5, 7 ),
(3, 3, 6 ),
(4, 8, 2)
On duplicate key update B = values (B );


When the preceding SQL statement is executed, if a in (, 7) conflicts with the unique value of the original record (, 9), The on duplicate key update statement is executed to update the original record, 9) Update (, 9), update (, 1) to (, 3, 1), insert new records (, 3), and (, 2)

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.