PDF (A4) - 35.2Mb
Man Pages (TGZ) - 256.4Kb
Man Pages (Zip) - 361.2Kb
Info (Gzip) - 3.4Mb
Info (Zip) - 3.4Mb
MySQL Globalization
MySQL Information Schema
MySQL Installation Guide
MySQL and Linux/Unix
MySQL and macOS
MySQL Partitioning
MySQL Performance Schema
MySQL Replication
Using the MySQL Yum Repository
MySQL Restrictions and Limitations
Security in MySQL
MySQL and Solaris
Building MySQL from Source
Starting and Stopping MySQL
MySQL Tutorial
MySQL and Windows
MySQL NDB Cluster 7.5
If you specify anON DUPLICATE KEY UPDATE clause and a row to be inserted would cause a duplicate value in aUNIQUE index orPRIMARY KEY, anUPDATE of the old row occurs. For example, if columna is declared asUNIQUE and contains the value1, the following two statements have similar effect:
INSERT INTO t1 (a,b,c) VALUES (1,2,3) ON DUPLICATE KEY UPDATE c=c+1;UPDATE t1 SET c=c+1 WHERE a=1; The effects are not quite identical: For anInnoDB table wherea is an auto-increment column, theINSERT statement increases the auto-increment value but theUPDATE does not.
If columnb is also unique, theINSERT is equivalent to thisUPDATE statement instead:
UPDATE t1 SET c=c+1 WHERE a=1 OR b=2 LIMIT 1; Ifa=1 OR b=2 matches several rows, onlyone row is updated. In general, you should try to avoid using anON DUPLICATE KEY UPDATE clause on tables with multiple unique indexes.
WithON DUPLICATE KEY UPDATE, the affected-rows value per row is 1 if the row is inserted as a new row, 2 if an existing row is updated, and 0 if an existing row is set to its current values. If you specify theCLIENT_FOUND_ROWS flag to themysql_real_connect() C API function when connecting tomysqld, the affected-rows value is 1 (not 0) if an existing row is set to its current values.
If a table contains anAUTO_INCREMENT column andINSERT ... ON DUPLICATE KEY UPDATE inserts or updates a row, theLAST_INSERT_ID() function returns theAUTO_INCREMENT value.
TheON DUPLICATE KEY UPDATE clause can contain multiple column assignments, separated by commas.
It is possible to useIGNORE withON DUPLICATE KEY UPDATE in anINSERT statement, but this may not behave as you expect when inserting multiple rows into a table that has multiple unique keys. This becomes apparent when an updated value is itself a duplicate key value. Consider the tablet, created and populated by the statements shown here:
mysql> CREATE TABLE t (a SERIAL, b BIGINT NOT NULL, UNIQUE KEY (b));;Query OK, 0 rows affected (0.03 sec)mysql> INSERT INTO t VALUES (1,1), (2,2);Query OK, 2 rows affected (0.01 sec)Records: 2 Duplicates: 0 Warnings: 0mysql> SELECT * FROM t;+---+---+| a | b |+---+---+| 1 | 1 || 2 | 2 |+---+---+2 rows in set (0.00 sec) Now we attempt to insert two rows, one of which contains a duplicate key value, usingON DUPLICATE KEY UPDATE, where theUPDATE clause itself results in a duplicate key value:
mysql> INSERT INTO t VALUES (2,3), (3,3) ON DUPLICATE KEY UPDATE a=a+1, b=b-1;ERROR 1062 (23000): Duplicate entry '1' for key 't.b'mysql> SELECT * FROM t;+---+---+| a | b |+---+---+| 1 | 1 || 2 | 2 |+---+---+2 rows in set (0.00 sec) The first row contains a duplicate value for one of the table's unique keys (columna), butb=b+1 in theUPDATE clause results in a unique key violation for columnb; the statement is immediately rejected with an error, and no rows are updated. Let us repeat the statement, this time adding theIGNORE keyword, like this:
mysql> INSERT IGNORE INTO t VALUES (2,3), (3,3) -> ON DUPLICATE KEY UPDATE a=a+1, b=b-1;Query OK, 1 row affected, 1 warning (0.00 sec)Records: 2 Duplicates: 1 Warnings: 1This time, the previous error is demoted to a warning, as shown here:
mysql> SHOW WARNINGS;+---------+------+-----------------------------------+| Level | Code | Message |+---------+------+-----------------------------------+| Warning | 1062 | Duplicate entry '1' for key 't.b' |+---------+------+-----------------------------------+1 row in set (0.00 sec) Because the statement was not rejected, execution continues. This means that the second row is inserted intot, as we can see here:
mysql> SELECT * FROM t;+---+---+| a | b |+---+---+| 1 | 1 || 2 | 2 || 3 | 3 |+---+---+3 rows in set (0.00 sec) In assignment value expressions in theON DUPLICATE KEY UPDATE clause, you can use theVALUES( function to refer to column values from thecol_name)INSERT portion of theINSERT ... ON DUPLICATE KEY UPDATE statement. In other words,VALUES( in thecol_name)ON DUPLICATE KEY UPDATE clause refers to the value ofcol_name that would be inserted, had no duplicate-key conflict occurred. This function is especially useful in multiple-row inserts. TheVALUES() function is meaningful only as an introducer forINSERT statement value lists, or in theON DUPLICATE KEY UPDATE clause of anINSERT statement, and returnsNULL otherwise. For example:
INSERT INTO t1 (a,b,c) VALUES (1,2,3),(4,5,6) ON DUPLICATE KEY UPDATE c=VALUES(a)+VALUES(b);That statement is identical to the following two statements:
INSERT INTO t1 (a,b,c) VALUES (1,2,3) ON DUPLICATE KEY UPDATE c=3;INSERT INTO t1 (a,b,c) VALUES (4,5,6) ON DUPLICATE KEY UPDATE c=9; ForINSERT ... SELECT statements, these rules apply regarding acceptable forms ofSELECT query expressions that you can refer to in anON DUPLICATE KEY UPDATE clause:
References to columns from queries on a single table, which may be a derived table.
References to columns from queries on a join over multiple tables.
References to columns from
DISTINCTqueries.References to columns in other tables, as long as the
SELECTdoes not useGROUP BY. One side effect is that you must qualify references to nonunique column names.
References to columns from aUNION do not work reliably. To work around this restriction, rewrite theUNION as a derived table so that its rows can be treated as a single-table result set. For example, this statement can produce incorrect results:
INSERT INTO t1 (a, b) SELECT c, d FROM t2 UNION SELECT e, f FROM t3ON DUPLICATE KEY UPDATE b = b + c; Instead, use an equivalent statement that rewrites theUNION as a derived table:
INSERT INTO t1 (a, b)SELECT * FROM (SELECT c, d FROM t2 UNION SELECT e, f FROM t3) AS dtON DUPLICATE KEY UPDATE b = b + c; The technique of rewriting a query as a derived table also enables references to columns fromGROUP BY queries.
Because the results ofINSERT ... SELECT statements depend on the ordering of rows from theSELECT and this order cannot always be guaranteed, it is possible when loggingINSERT ... SELECT ON DUPLICATE KEY UPDATE statements for the source and the replica to diverge. Thus,INSERT ... SELECT ON DUPLICATE KEY UPDATE statements are flagged as unsafe for statement-based replication. Such statements produce a warning in the error log when using statement-based mode and are written to the binary log using the row-based format when usingMIXED mode. AnINSERT ... ON DUPLICATE KEY UPDATE statement against a table having more than one unique or primary key is also marked as unsafe. (Bug #11765650, Bug #58637)
See alsoSection 16.2.1.1, “Advantages and Disadvantages of Statement-Based and Row-Based Replication”.
AnINSERT ... ON DUPLICATE KEY UPDATE on a partitioned table using a storage engine such asMyISAM that employs table-level locks locks any partitions of the table in which a partitioning key column is updated. (This does not occur with tables using storage engines such asInnoDB that employ row-level locking.) For more information, seeSection 22.6.4, “Partitioning and Locking”.
PDF (A4) - 35.2Mb
Man Pages (TGZ) - 256.4Kb
Man Pages (Zip) - 361.2Kb
Info (Gzip) - 3.4Mb
Info (Zip) - 3.4Mb
MySQL Globalization
MySQL Information Schema
MySQL Installation Guide
MySQL and Linux/Unix
MySQL and macOS
MySQL Partitioning
MySQL Performance Schema
MySQL Replication
Using the MySQL Yum Repository
MySQL Restrictions and Limitations
Security in MySQL
MySQL and Solaris
Building MySQL from Source
Starting and Stopping MySQL
MySQL Tutorial
MySQL and Windows
MySQL NDB Cluster 7.5