博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
INSERT ... ON DUPLICATE KEY UPDATE Syntax
阅读量:4641 次
发布时间:2019-06-09

本文共 3208 字,大约阅读时间需要 10 分钟。

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  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  when connecting to , 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  is equivalent to this  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  function in the  clause to refer to column values from the  portion of the  statement. In other words,  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  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  inserts or updates a row, the  function returns the AUTO_INCREMENT value.

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

Because the results of  statements depend on the ordering of rows from the  and this order cannot always be guaranteed, it is possible when logging  statements for the master and the slave to diverge. Thus,  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  statement against a table having more than one unique or primary key is also marked as unsafe. (Bug #11765650, Bug #58637) See also .

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

转载 

转载于:https://www.cnblogs.com/exceptioneye/p/5513153.html

你可能感兴趣的文章
1 为什么搭建.Net core下的云开发框架
查看>>
Java安全防御学习笔记V1.0
查看>>
Python时间和日期
查看>>
oracle 11g ocp 笔记(26)--全球化
查看>>
bzoj3712: [PA2014]Fiolki
查看>>
asp.net 淘宝api获取商品信息
查看>>
宏_CRTIMP分析
查看>>
ubuntu创建、删除文件及文件夹,强制清空回收站方法
查看>>
jmeter参数化之CSV Data Set Config
查看>>
Cobbler--自动化部署
查看>>
diff
查看>>
linux源码Makefile详解
查看>>
css3圈圈进度条
查看>>
【linux配置】在VMware中为Redhat HAT配置本地yum源
查看>>
Python 函数动态参数
查看>>
javascript之非构造函数的继承
查看>>
python爬虫--爬取某网站电影下载地址
查看>>
C#实现 单点登录(SSO)
查看>>
高精度计算(2015.8.1)
查看>>
cocos2d-x tile map瓦片地图的黑线及地图抖动解决方案
查看>>