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 you insert a new row into a table if the row causes a duplicate in UNIQUE index or PRIMARY KEY , MySQL will issue an error.

However, if you specify the ON DUPLICATE KEY UPDATE option in the INSERT statement, MySQL will update the existing row with the new values instead.

The syntax of INSERT ON DUPLICATE KEY UPDATE statement is as follows:

INSERT INTO table (column_list)
VALUES (value_list)
ON DUPLICATE KEY UPDATE
   c1 = v1,
   c2 = v2,
   ...;

The only addition to the INSERT statement is the ON DUPLICATE KEY UPDATE clause where you specify a list of column-value-pair assignments in case of duplicate.

Basically, the statement first tries to insert a new row into the table. If a duplicate error occurs, it will update the existing row with the value specified in the ON DUPLICATE KEY UPDATE clause.

MySQL returns the number of affected-rows based on the action it performs:

  • If the new row is inserted, the number of affected-rows is 1.
  • If the existing row is updated, the number of affected-rows is 2.
  • If the existing row is updated using its current values, the number of affected-rows is 0.

To use the values from the INSERT clause in the DUPLICATE KEY UPDATE clause, you use the VALUES()function as follows:

INSERT INTO table_name(c1)
VALUES(c1)
ON DUPLICATE KEY UPDATE c1 = VALUES(c1) + 1;

The statement above sets the value of the c1 to its current value specified by the expression VALUES(c1) plus 1 if there is a duplicate in UNIQUE index or PRIMARY KEY.

MySQL INSERT ON DUPLICATE KEY UPDATE example

Let’s take a look at an example of using the INSERT ON DUPLICATE KEY UPDATE to understand how it works.

First, create a table named devices to store the network devices.

CREATE TABLE devices (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(100)
);

Next, insert rows into the devices table.

INSERT INTO devices(name)
VALUES('Router F1'),('Switch 1'),('Switch 2');

Then, query the data from the devices table to verify the insert:

SELECT
    id,
    name
FROM
    devices;

Now, we have three rows in the devices table.

After that, insert one more row into the devices table.

INSERT INTO
   devices(name)
VALUES
   ('Printer')
ON DUPLICATE KEY UPDATE name = 'Printer';

Because there is no duplicate, MySQL inserts a new row into the devices table. The statement above has the same effect as the following statement:

INSERT INTO devices(name)
VALUES ('Printer');

Finally, insert a row with a duplicate value in the id column.

INSERT INTO devices(id,name)
VALUES
   (4,'Printer')
ON DUPLICATE KEY UPDATE name = 'Central Printer';

MySQL issues the following message:

2 row(s) affected

Because a row with id 4 already exists in the devices table, the statement updates the name from Printer to Central Printer.

In this tutorial, you have learned how to insert or update data in a table using the ON DUPLICATE KEY UPDATE option of the INSERT statement.

原文地址:https://www.cnblogs.com/john123/p/12232329.html

时间: 2024-08-29 15:51:49

MySQL INSERT ON DUPLICATE KEY UPDATE的相关文章

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使用

今天做推断插入用到了MySQL中ON DUPLICATE KEY UPDATE,如今Mark下面! 假设你想做到数据库中没有数据的话插入数据.有数据的话更新数据,那么你能够选择ON DUPLICATE KEY UPDATE. ON DUPLICATE KEY UPDATE可以在UNIQUE索引或PRIMARY KEY存在的情况下对旧行运行UPDATE操作. 比如:假设列a被定义为UNIQUE,而且包括值1,则下面两个语句具有同样的效果: INSERT INTO table (a,b,c) VAL

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 f

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单个增加更新及批量增加更新的sql

应用场景: 在实际应用中,经常碰到导入数据的功能,当导入的数据不存在时则进行添加,有修改时则进行更新, 在刚碰到的时候,第一反应是将其实现分为两块,分别是判断增加,判断更新,后来发现在mysql中有ON DUPLICATE KEY UPDATE一步就可以完成,感觉实在是太方便了, 该语句是基于唯一索引或主键使用,比如一个字段a被加上了unique index,并且表中已经存在了一条记录值为1. 比如: 下面两个语句会有相同的效果: INSERT INTO table (a,b,c) VALUES

MySQL使用on duplicate key update时导致主键不连续自增

使用on duplicate key update语法有时是很方便,但是会有一个影响:默认情况下,每次更新都会更新该表的自增主键ID,如果更新频率很快,会导致主键ID自增的很快,过段时间就超过数字类型的的范围了解决这个问题,有两种方式:(实际目前的方式就是把自增主键ID设置为bigint,也有一部分操作先查询再选择插入OR更新)方法一:拆分成两个动作,先查询,再更新方法二:修改innodb_autoinc_lock_mode参数(不推荐)  innodb_autoinc_lock_mode中有3

mysql:on duplicate key update与replace into

在往表里面插入数据的时候,经常需要:a.先判断数据是否存在于库里面:b.不存在则插入:c.存在则更新 一.replace into 前提:数据库里面必须有主键或唯一索引,不然replace into 会直接插入新数据,导致数据表里面有重复数据 执行时先尝试插入数据: a.当数据表里面存在(通过主键或唯一索引来判断)该数据,则先将表里的数据删除,再插入新的数据 b.如果数据表里面不存在该数据,则直接插入数据 replace into是insert into的增强版,语法跟insert iton差不

【MySQL】ON DUPLICATE KEY UPDATE

之前没用过这个操作,甚至没见过--最近接触到,而且还挺有用. 作用:若 KEY 不重复,则插入记录:否则更新记录. 单条操作: INSERT INTO table(a, b, c) VALUES (1,2,3) ON DUPLICATE KEY UPDATE c=c+1; 批量操作: INSERT INTO database.table (id, key, field) VALUES (v1, v2, v3), (v1, v2, v3), (v1, v2, v3) ON DUPLICATE KE

MySql避免重复插入记录方法(ignore,Replace,on duplicate key update,not exist)

在MySQL中进行条件插入数据时,可能会用到以下语句,现小结一下.我们先建一个简单的表来作为测试: CREATE TABLE `books` ( `id` INT(11) NOT NULL AUTO_INCREMENT, `name` VARCHAR(200) NOT NULL, PRIMARY KEY (`id`), UNIQUE KEY `NewIndex1` (`name`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; 1.insert ignore int