Insert into .. on duplicate key syntax and example tutorial

Source: Internet
Author: User

If the 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:

If the 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 multiple rows of records are inserted, how does one specify the value of the field after on duplicate key update? You must know that one insert statement can only have one on duplicate key update

 

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 );

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 );

Insert [low_priority | delayed | high_priority] [ignore] [into] tbl_name [(col_name,...)] values ({expr | default },...), (...),... [on duplicate key update col_name = expr,...] or:

Insert [low_priority | delayed | high_priority] [ignore] [into] tbl_name set col_name = {expr | default },... [on duplicate key update col_name = expr,...] or:

Insert [low_priority | high_priority] [ignore] [into] tbl_name [(col_name,...)] select... [on duplicate key update col_name = expr,...] insert is used to insert a new row into an existing table. Insert... values and insert... set statements insert rows according to the specified values. Insert statements in the form of insert... select insert the rows selected from other tables. In section 13.2.4.1, "insert... select Syntax" further discusses insert... select.

Rows should be inserted into the tbl_name table. You can specify columns as follows. This statement provides values for these columns.

· The column name list or set clause explicitly indicates the column.

· If you do not specify the list of columns for insert... values or insert... select, the values of each column in the table must be provided in the values list or provided by select. If you do not know the order of the columns in the table, use describe tbl_name to query.

The column value can be specified in multiple ways:

 

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.