Go deep into mysql & quot; ONDUPLICATEKEYUPDATE & quot; syntax _ MySQL
Source: Internet
Author: User
Go deep into mysqlamp; quot; ONDUPLICATEKEYUPDATEamp; quot; syntax analysis bitsCN. commysql "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.
If you want to know
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.