INSERT ... ON DUPLICATE KEY UPDATE Syntax
2016-05-20 18:35
441 查看
If you specify
(The effects are not identical for an
The
With
If column
If
You can use the
That statement is identical to the following two statements:
If a table contains an
The
Because the results of
In MySQL 5.7, an
转载
ON DUPLICATE KEY UPDATE, and a row is inserted that would cause a duplicate value in a
UNIQUEindex or
PRIMARY KEY, MySQL performs an
UPDATEof the old row. For example, if column
ais declared as
UNIQUEand 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
InnoDBtable where
ais an auto-increment column. With an auto-increment column, an
INSERTstatement increases the auto-increment value but
UPDATEdoes not.)
The
ON DUPLICATE KEY UPDATEclause 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_ROWSflag 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
bis also unique, the
INSERTis equivalent to this
UPDATEstatement instead:
UPDATE table SET c=c+1 WHERE a=1 OR b=2 LIMIT 1;
If
a=1 OR b=2matches several rows, only one row is updated. In general, you should try to avoid using an
ON DUPLICATE KEY UPDATEclause on tables with multiple unique indexes.
You can use the
VALUES(function in thecol_name)
UPDATEclause to refer to column values from the
INSERTportion of the
INSERT ... ON DUPLICATE KEY UPDATEstatement. In other words,
VALUES(in thecol_name)
ON DUPLICATE KEY UPDATEclause refers to the value of
col_namethat would be inserted, had no duplicate-key conflict occurred. This function is especially useful in multiple-row inserts. The
VALUES()function is meaningful only in
INSERT ... UPDATEstatements and returns
NULLotherwise. 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_INCREMENTcolumn and
INSERT ... ON DUPLICATE KEY UPDATEinserts or updates a row, the
LAST_INSERT_ID()function returns the
AUTO_INCREMENTvalue.
The
DELAYEDoption is ignored when you use
ON DUPLICATE KEY UPDATE.
Because the results of
INSERT ... SELECTstatements depend on the ordering of rows from the
SELECTand this order cannot always be guaranteed, it is possible when logging
INSERT ... SELECT ON DUPLICATE KEY UPDATEstatements for the master and the slave to diverge. Thus,
INSERT ... SELECT ON DUPLICATE KEY UPDATEstatements 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
MIXEDmode. In addition, an
INSERT ... ON DUPLICATE KEY UPDATEstatement 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 UPDATEon a partitioned table using a storage engine such as
MyISAMthat 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
InnoDBthat employ row-level locking.) See Section 20.6.4, “Partitioning and Locking”, for more information.
转载
INSERT ... ON DUPLICATE KEY UPDATE Syntax
相关文章推荐
- Java NIO3:通道和文件通道
- Java NIO2:缓冲区
- hibernate两个实体类映射数据库中的同一张表,保错
- [C#] 操作数据库时检验性别、年龄、邮箱、手机号的输入
- 自定义ViewProgress
- Java NIO1:I/O模型概述
- iOS开发之网络检测
- 参考1
- Java IO8:IO简单总结
- Java IO7:管道流、对象流
- 【PS4开发】unity3d ps4手柄键位
- java 数据库连接操作类
- Javascript面向对象编程 及 doT的使用
- 服务器nginx 用shell 统计并发
- 关于阿里云服务器安装 wordpress(linux环境)的教程
- 测试
- hdu 2572 水题
- 浮点数的二进制表示
- 【Python自动化运维之路Day2】
- Java IO6:字符流进阶及BufferedWriter、BufferedReader