MySQL的Replace into 与Insert into ..... on duplicate key update
来源:互联网 发布:mac flamingo 珊瑚 编辑:程序博客网 时间:2024/04/29 09:58
背景:某天,我老婆蛋,发了数据库的博文给我看,因为之前一直没有仔细研究过replace into 与 on duplicate key update区别,此次燃起了我的好奇心,故作了验证了。
以下是在mysql 5.1.50验证的
1. 建表:
mysql> CREATE TABLE `check_t1` (
`a` int(11) NOT NULL AUTO_INCREMENT,
`b` varchar(10) DEFAULT NULL,
`c` varchar(10) NOT NULL DEFAULT '',
PRIMARY KEY (`a`)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
2. 插入数据:
insert into check_t1(b,c) values('b1','c1'),('b2','c2'),('b3','c3');
mysql> show create table check_t1;
+----------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+----------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| check_t1 | CREATE TABLE `check_t1` (
`a` int(11) NOT NULL AUTO_INCREMENT,
`b` varchar(10) DEFAULT NULL,
`c` varchar(10) NOT NULL DEFAULT '',
PRIMARY KEY (`a`)
) ENGINE=MyISAM AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 |
+----------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
3. replace into:
mysql> replace into check_t1(a,b) value(1,'a11');
Query OK, 2 rows affected (0.00 sec)
mysql> select * from check_t1;
+---+------+----+
| a | b | c |
+---+------+----+
| 1 | a11 | |
| 2 | b2 | c2 |
| 3 | b3 | c3 |
+---+------+----+
mysql> show create table check_t1;
+----------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+----------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| check_t1 | CREATE TABLE `check_t1` (
`a` int(11) NOT NULL AUTO_INCREMENT,
`b` varchar(10) DEFAULT NULL,
`c` varchar(10) NOT NULL DEFAULT '',
PRIMARY KEY (`a`)
) ENGINE=MyISAM AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 |
mysql> insert into check_t1(b,c) value('b4','c4');
Query OK, 1 row affected (0.00 sec)
mysql> select * from check_t1;
+---+------+----+
| a | b | c |
+---+------+----+
| 1 | a11 | |
| 2 | b2 | c2 |
| 3 | b3 | c3 |
| 4 | b4 | c4 |
+---+------+----+
所以当与主键冲突时,replace覆盖相关字段,其它字段填充默认值,可以理解为删除重复key的记录,新插入一条记录,一个delete原有记录再insert的操作,但自增长值不变。
4.insert .. on deplicate update
mysql> insert into check_t1(a,b) value(3,'b33') on duplicate key update b='b33';
Query OK, 2 rows affected (0.00 sec)
mysql> show create table check_t1;
+----------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+----------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| check_t1 | CREATE TABLE `check_t1` (
`a` int(11) NOT NULL AUTO_INCREMENT,
`b` varchar(10) DEFAULT NULL,
`c` varchar(10) NOT NULL DEFAULT '',
PRIMARY KEY (`a`)
) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=utf8 |
mysql> select * from check_t1;
+---+------+----+
| a | b | c |
+---+------+----+
| 1 | a11 | |
| 2 | b2 | c2 |
| 3 | b33 | c3 |
| 4 | b4 | c4 |
+---+------+----+
由此可看当主键有时,只执行后面的udate操作语句,可以理解为删除重复key的记录,新插入一条记录,一个delete原有记录再insert的操作,但自增长值不变。
5.总结:
insert .. on deplicate udpate保留了所有字段的旧值,再覆盖然后一起insert进去,而replace没有保留旧值,直接删除再insert新值。从底层执行效率上来讲,replace要比insert .. on deplicate update效率要高,但是在写replace的时候,字段要写全,防止老的字段数据被删除。
- MySQL的Replace into 与Insert into ..... on duplicate key update ...真正的不同之处
- MySQL的Replace into 与Insert into ..... on duplicate key update ...真正的不同之处
- MySQL的Replace into 与Insert into ..... on duplicate key update ...真正的不同之处
- MySQL的Replace into 与Insert into ..... on duplicate key update ...真正的不同之处
- MySQL的Replace into 与Insert into ..... on duplicate key update ...真正的不同之处
- MySQL的Replace into 与Insert into ..... on duplicate key update ...真正的不同之处
- MySQL的Replace into 与Insert into on duplicate key update真正的不同之处
- MySQL的Replace into 与Insert into on duplicate key update真正的不同之处
- MySQL的Replace into 与Insert into ..... on duplicate key update ...真正的不同之处
- MySQL的Replace into 与Insert into on duplicate key update真正的不同之处
- MySQL的Replace into 与 Insert into on duplicate key update的不同之处
- MySQL的Replace into 与Insert into on duplicate key update真正的不同之处
- Mysql replace into 与 insert into on duplicate key update 的区别
- MySQL的Replace into 与Insert into ..... on duplicate key update
- Mysql replace into 与 insert into on duplicate key update 死锁和性能测试
- MySql避免"重复插入记录"的方法(INSERT ignore into,Replace into,ON DUPLICATE KEY UPDATE)
- mysql中insert into on duplicate key update 和replace into的区别
- REPLACE INTO 与 INSERT ... ON DUPLICATE KEY UPDATE
- HDU 3371 Connect the Cities 最小生成树
- android apk 反编译
- 2.5.5改变拖放图标.html
- DataGrid的滚动条不显示问题
- 客户细分总结
- MySQL的Replace into 与Insert into ..... on duplicate key update
- tomca在win下启动隐藏窗口
- Android组件之间的信使——Intent
- HttpClient和HttpURLConnection的使用和区别(下)
- 批量替换页面中的路径 相对路径转换为绝对路径
- 项目引入Solr时应该考虑的一些问题
- Oracle WorkFlow 工作流 上篇
- UVa-10763-Foreign Exchange
- PHP下载CSS文件中的图片