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: