INSERT ... ON DUPLICATE KEY UPDATE Syntax

If you specify ON DUPLICATE KEY UPDATE, and a row is inserted that would cause a duplicate value in a UNIQUE index or PRIMARY KEY, MySQL performs an UPDATE of the old row. For example, if column a is declared as UNIQUE and contains the value 1, the following two statements have similar 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;

(The effects are not identical for an InnoDB table where a is an auto-increment column. With an auto-increment column, an INSERT statement increases the auto-increment value but UPDATE does not.)

The ON DUPLICATE KEY UPDATE clause can contain multiple column assignments, separated by commas.

With ON 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 the CLIENT_FOUND_ROWS flag to mysql_real_connect() when connecting to mysqld, the affected-rows value is 1 (not 0) if an existing row is set to its current values.

If column b is also unique, the INSERT is equivalent to this UPDATE statement instead:

UPDATE table SET c=c+1 WHERE a=1 OR b=2 LIMIT 1;

If a=1 OR b=2 matches several rows, only one row is updated. In general, you should try to avoid using an ON DUPLICATE KEY UPDATE clause on tables with multiple unique indexes.

You can use the VALUES(col_name) function in the UPDATE clause to refer to column values from the INSERT portion of the INSERT ... ON DUPLICATE KEY UPDATE statement. In other words, VALUES(col_name) in the ON DUPLICATE KEY UPDATE clause refers to the value of col_name that would be inserted, had no duplicate-key conflict occurred. This function is especially useful in multiple-row inserts. The VALUES() function is meaningful only inINSERT ... UPDATE statements and returns NULL otherwise. Example:

INSERT INTO table (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 table (a,b,c) VALUES (1,2,3)
  ON DUPLICATE KEY UPDATE c=3;
INSERT INTO table (a,b,c) VALUES (4,5,6)
  ON DUPLICATE KEY UPDATE c=9;

If a table contains an AUTO_INCREMENT column and INSERT ... ON DUPLICATE KEY UPDATE inserts or updates a row, the LAST_INSERT_ID() function returns the AUTO_INCREMENT value.

The DELAYED option is ignored when you use ON DUPLICATE KEY UPDATE.

Because the results of INSERT ... SELECT statements depend on the ordering of rows from the SELECT and this order cannot always be guaranteed, it is possible when logging INSERT ... SELECT ON DUPLICATE KEY UPDATE statements for the master and the slave to diverge. Thus, INSERT ... SELECT ON DUPLICATE KEY UPDATE statements are flagged as unsafe for statement-based replication. With this change, such statements produce a warning in the log when using statement-based mode and are logged using the row-based format when using MIXED mode. In addition, an INSERT ... 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 also Section 18.2.1.1, “Advantages and Disadvantages of Statement-Based and Row-Based Replication”.

In MySQL 5.7, an INSERT ... ON DUPLICATE KEY UPDATE on a partitioned table using a storage engine such as MyISAM 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 as InnoDB that employ row-level locking.) See Section 20.6.4, “Partitioning and Locking”, for more information.

转载

INSERT ... ON DUPLICATE KEY UPDATE Syntax

时间: 2024-10-13 18:41:44

INSERT ... ON DUPLICATE KEY UPDATE Syntax的相关文章

MySQL INSERT ON DUPLICATE KEY UPDATE

来源:https://www.mysqltutorial.org/mysql-insert-or-update-on-duplicate-key-update/ Introduction to the MySQL INSERT ON DUPLICATE KEY UPDATE statement The INSERT ON DUPLICATE KEY UPDATE is a MySQL's extension to the SQL standard's INSERT statement. When

MYSQL之REPLACE INTO和INSERT … ON DUPLICATE KEY UPDATE用法

REPLACE INTO的用法与INSERT很相似,最终在表中的目的是插入一行新的数据.不同的是,当插入时出现主键或者唯一索引冲突的时候,会删除原有记录,重新插入新的记录.因此,除非表具有主键或者唯一索引,否则使用REPLACE INTO无任何意义. 以下新建了一个表来进行测试,并添加触发检视REPLACE INTO是如何工作的: CREATE TABLE `replace_into` ( `id` int(11) NOT NULL AUTO_INCREMENT, `uid` int(11) N

解决MySQL复合主键下ON DUPLICATE KEY UPDATE语句失效问题

先描述一下这个问题的起因,假设有一张表,里面保存了交易订单,每张订单有唯一的ID,有最后更新时间,还有数据,详情如下: +-------+----------+------+-----+---------------------+-------+ | Field | Type     | Null | Key | Default             | Extra | +-------+----------+------+-----+---------------------+-------

mysql ON DUPLICATE KEY UPDATE、REPLACE INTO

INSERT INTO ON DUPLICATE KEY UPDATE 与 REPLACE INTO,两个命令可以处理重复键值问题,在实际上它之间有什么区别呢?前提条件是这个表必须有一个唯一索引或主键. 1.REPLACE发现重复的先删除再插入,如果记录有多个字段,在插入的时候如果有的字段没有赋值,那么新插入的记录这些字段为空.2.INSERT发现重复的是更新操作.在原有记录基础上,更新指定字段内容,其它字段内容保留. 这样REPLACE的操作成本要大于 insert  ON DUPLICATE

MySql之on duplicate key update详解

在我们的日常开发中,你是否遇到过这种情景:查看某条记录是否存在,不存在的话创建一条新记录,存在的话更新某些字段.你的处理方式是不是就是按照下面这样? $result = mysql_query('select * from xxx where id = 1'); $row = mysql_fetch_assoc($result); if($row){ mysql_query('update ...'); }else{ mysql_query('insert ...'); } 这样的写法可能有如下

MySQL ON DUPLICATE KEY UPDATE 语法

一.前言 在日常业务开发中经常有这样一个场景,首先创建一条记录,然后插入到数据库,如果数据库已经存在同一主键的记录,执行update操作:否则,执行insert操作.这个操作可以在业务层做,也可以在数据库层面做.业务层一般做法是先查询,如果不存在就插入,如果存在就更新:但是查询和插入不是原子性操作,在并发量比较高的时候,可能两个线程都查询不到某个记录,所以会执行两次插入,其中一条必然会因为唯一性约束冲突而失败.数据库层mysql中INSERT ... ON DUPLICATE KEY UPDAT

INSERT 中ON DUPLICATE KEY UPDATE的使用

使用场景,在做全国各省ip访问统计时要将sparkStreaming的数据存在mysql中,按照一般设计,id,province,counts,time,这样就需要每次清空表,但是如果多分区的话就存在删除表的时候回出现后一个分区可能把前一个分区的数据删除掉,当然最好的办法是每次都只更新而不删除,但是如果通过代码实现比较复杂. 此时我们需要的就是如果存在则更新,如果不存在则新增. 用redis的kv就可以很容易的实现.在MySQL中也有这样的功能.INSERT 中ON DUPLICATE KEY

【转】MySQL的Replace into 与Insert into on duplicate key update真正的不同之处

原文链接:http://www.jb51.net/article/47090.htm 今天听同事介绍oracle到mysql的数据migration,他用了Insert into ..... on duplicate key update ...,我当时就想怎么不用Replace呢,于是回来就仔细查了下,它们果然还是有区别的 看下面的例子吧: 1 Replace into ... 1.1 录入原始数据 mysql> use test; Database changed mysql>  mysq

INSERT into ... on duplicate key update ...

INSERT  into exception_report_total ( waitFillOrders ,waitSendOrders ,changeOrders ,changeTotal ,waitProduceOrders ,wmsStockOrders ,erpStockOrders ,waitReserveOrders ,waitDispatchOrders ,addDispatchOrders ,waitCancel ,sendExceptions ,reserveOrders ,c