Insertintoxxxonduplicatekeyupdate in Mysql _ MySQL

Source: Internet
Author: User
In Mysql, the Insertintoxxxonduplicatekeyupdate problem bitsCN.com. for example, if Column a is defined as unique and the value is 1, the following statements have the same effect, that is to say, if a = 1 exists in the incoming and outgoing records, c = c + 1 is directly updated without the c = 3 operation.

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

It is also worth mentioning that this statement is in mysql, but not in standard SQL statements.

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

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.