mysql的replace into和on duplicate key update测试笔记

mysql> create table tbl_insert_tmp(id int(5),addr_number int(10), name varchar(20),primary key (id),unique key udx_addr_number (addr_number));       
Query OK, 0 rows affected (0.05 sec)

mysql> show create table tbl_insert_tmp\G
*************************** 1. row ***************************
       Table: tbl_insert_tmp
Create Table: CREATE TABLE `tbl_insert_tmp` (
  `id` int(5) NOT NULL DEFAULT ‘0‘,
  `addr_number` int(10) DEFAULT NULL,
  `name` varchar(20) DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `udx_addr_number` (`addr_number`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

mysql>

1、测试on duplicate key update(是根据主键或唯一键如果有对应键则更新,没有对应键就插入,所以使用起来不会删除正常数据)

mysql> insert into tbl_insert_tmp values(1,100,‘a‘),(2,200,‘b‘),(3,300,‘c‘);
Query OK, 3 rows affected (0.01 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> select * from tbl_insert_tmp ;
+----+-------------+------+
| id | addr_number | name |
+----+-------------+------+
|  1 |         100 | a    |
|  2 |         200 | b    |
|  3 |         300 | c    |
+----+-------------+------+
3 rows in set (0.00 sec)

mysql>

主键id和addr_number唯一键没有重复,直接插入
mysql> insert into tbl_insert_tmp(id,addr_number,name) values(4,400,‘d‘) on duplicate key update name=‘haha‘;
Query OK, 1 row affected (0.00 sec)

mysql> select * from tbl_insert_tmp ;
+----+-------------+------+
| id | addr_number | name |
+----+-------------+------+
|  1 |         100 | a    |
|  2 |         200 | b    |
|  3 |         300 | c    |
|  4 |         400 | d    |
+----+-------------+------+
4 rows in set (0.00 sec)

mysql>

因为id是主键有重复1直接更新

mysql> insert into tbl_insert_tmp(id,addr_number,name) values(1,500,‘d‘) on duplicate key update name=‘haha‘;     
Query OK, 2 rows affected (0.00 sec)

mysql> select * from tbl_insert_tmp ;
+----+-------------+------+
| id | addr_number | name |
+----+-------------+------+
|  1 |         100 | haha |
|  2 |         200 | b    |
|  3 |         300 | c    |
|  4 |         400 | d    |
+----+-------------+------+
4 rows in set (0.00 sec)

因为addr_number唯一键有重复400直接更新

mysql> insert into tbl_insert_tmp(id,addr_number,name) values(5,400,‘d‘) on duplicate key update name=‘hehe‘;       
Query OK, 2 rows affected (0.03 sec)

mysql> select * from tbl_insert_tmp ;
+----+-------------+------+
| id | addr_number | name |
+----+-------------+------+
|  1 |         100 | haha |
|  2 |         200 | b    |
|  3 |         300 | c    |
|  4 |         400 | hehe |
+----+-------------+------+
4 rows in set (0.00 sec)

mysql>

删除addr_number唯一键就可以直接插入重复值
mysql> alter table tbl_insert_tmp drop key udx_addr_number;
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show create table  tbl_insert_tmp\G
*************************** 1. row ***************************
       Table: tbl_insert_tmp
Create Table: CREATE TABLE `tbl_insert_tmp` (
  `id` int(5) NOT NULL DEFAULT ‘0‘,
  `addr_number` int(10) DEFAULT NULL,
  `name` varchar(20) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

mysql>
mysql> insert into tbl_insert_tmp(id,addr_number,name) values(5,400,‘d‘) on duplicate key update name=‘hehe‘;
Query OK, 1 row affected (0.00 sec)

mysql> select * from tbl_insert_tmp ;                                                                         
+----+-------------+------+
| id | addr_number | name |
+----+-------------+------+
|  1 |         100 | haha |
|  2 |         200 | b    |
|  3 |         300 | c    |
|  4 |         400 | hehe |
|  5 |         400 | d    |
+----+-------------+------+
5 rows in set (0.00 sec)

mysql>

2、测试replace into (是根据主键或者唯一键先删除对应键值再插入,所以会删除正常数据,使用时要特别小心)

mysql> truncate table tbl_insert_tmp;
Query OK, 0 rows affected (0.03 sec)

mysql> show create table tbl_insert_tmp\G
*************************** 1. row ***************************
       Table: tbl_insert_tmp
Create Table: CREATE TABLE `tbl_insert_tmp` (
  `id` int(5) NOT NULL DEFAULT ‘0‘,
  `addr_number` int(10) DEFAULT NULL,
  `name` varchar(20) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

mysql> alter table tbl_insert_tmp add unique key udx_addr_number(addr_number);
Query OK, 0 rows affected (0.08 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show create table tbl_insert_tmp\G                                     
*************************** 1. row ***************************
       Table: tbl_insert_tmp
Create Table: CREATE TABLE `tbl_insert_tmp` (
  `id` int(5) NOT NULL DEFAULT ‘0‘,
  `addr_number` int(10) DEFAULT NULL,
  `name` varchar(20) DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `udx_addr_number` (`addr_number`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

mysql>
mysql>
mysql> insert into tbl_insert_tmp values(1,100,‘a‘),(2,200,‘b‘),(3,300,‘c‘),(4,400,‘d‘);
Query OK, 4 rows affected (0.00 sec)
Records: 4  Duplicates: 0  Warnings: 0

mysql> select * from tbl_insert_tmp ;                                                   
+----+-------------+------+
| id | addr_number | name |
+----+-------------+------+
|  1 |         100 | a    |
|  2 |         200 | b    |
|  3 |         300 | c    |
|  4 |         400 | d    |
+----+-------------+------+
4 rows in set (0.00 sec)

mysql>

mysql> replace into tbl_insert_tmp values(1,500,‘e‘);                                   
Query OK, 2 rows affected (0.00 sec)

mysql> select * from tbl_insert_tmp ;                
+----+-------------+------+
| id | addr_number | name |
+----+-------------+------+
|  1 |         500 | e    |
|  2 |         200 | b    |
|  3 |         300 | c    |
|  4 |         400 | d    |
+----+-------------+------+
4 rows in set (0.00 sec)

mysql>
mysql> replace into tbl_insert_tmp values(2,500,‘f‘);
Query OK, 3 rows affected (0.00 sec)

mysql> select * from tbl_insert_tmp ;                
+----+-------------+------+
| id | addr_number | name |
+----+-------------+------+
|  2 |         500 | f    |
|  3 |         300 | c    |
|  4 |         400 | d    |
+----+-------------+------+
3 rows in set (0.00 sec)

mysql>

mysql> replace into tbl_insert_tmp values(5,500,‘h‘);
Query OK, 2 rows affected (0.00 sec)

mysql> select * from tbl_insert_tmp ;                
+----+-------------+------+
| id | addr_number | name |
+----+-------------+------+
|  3 |         300 | c    |
|  4 |         400 | d    |
|  5 |         500 | h    |
+----+-------------+------+
3 rows in set (0.00 sec)

mysql>

删除唯一键udx_addr_number

mysql> alter table tbl_insert_tmp drop key udx_addr_number;
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql>
mysql> replace into tbl_insert_tmp values(1,500,‘g‘);     
Query OK, 1 row affected (0.00 sec)

mysql> select * from tbl_insert_tmp ;                     
+----+-------------+------+
| id | addr_number | name |
+----+-------------+------+
|  1 |         500 | g    |
|  3 |         300 | c    |
|  4 |         400 | d    |
|  5 |         500 | h    |
+----+-------------+------+
4 rows in set (0.00 sec)

mysql>
mysql> replace into tbl_insert_tmp values(2,300,‘i‘);
Query OK, 1 row affected (0.00 sec)

mysql> select * from tbl_insert_tmp ;                
+----+-------------+------+
| id | addr_number | name |
+----+-------------+------+
|  1 |         500 | g    |
|  2 |         300 | i    |
|  3 |         300 | c    |
|  4 |         400 | d    |
|  5 |         500 | h    |
+----+-------------+------+
5 rows in set (0.00 sec)

mysql> replace into tbl_insert_tmp values(2,200,‘b‘);
Query OK, 2 rows affected (0.00 sec)

mysql> select * from tbl_insert_tmp ;                
+----+-------------+------+
| id | addr_number | name |
+----+-------------+------+
|  1 |         500 | g    |
|  2 |         200 | b    |
|  3 |         300 | c    |
|  4 |         400 | d    |
|  5 |         500 | h    |
+----+-------------+------+
5 rows in set (0.00 sec)

mysql>

时间: 2024-12-13 16:10:45

mysql的replace into和on duplicate key update测试笔记的相关文章

mysql insert语法注意事项(ON DUPLICATE KEY UPDATE )

mysql insert语法注意事项(ON DUPLICATE KEY UPDATE ) INSERT语法 INSERT [LOW_PRIORITY | DELAYED | HIGH_PRIORITY] [IGNORE] [INTO] tbl_name [(col_name,...)] VALUES ({expr | DEFAULT},...),(...),... [ ON DUPLICATE KEY UPDATE col_name=expr, ... ] 或: INSERT [LOW_PRIO

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

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

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的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

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

MySql避免重复插入记录方法(ignore,Replace,ON DUPLICATE KEY UPDATE)

本文章来给大家提供三种在mysql中避免重复插入记录方法,主要是讲到了ignore,Replace,ON DUPLICATE KEY UPDATE三种方法,各位同学可尝试参考. 案一:使用ignore关键字 如果是用主键primary或者唯一索引unique区分了记录的唯一性,避免重复插入记录可以使用:  代码如下 复制代码 1 INSERT IGNORE INTO `table_name` (`email`, `phone`, `user_id`) VALUES ('[email protec

mysql ON DUPLICATE KEY UPDATE、REPLACE INTO

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

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与replace into

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