避免MySQL替换逻辑SQL的坑爹操作

发表于:2018-1-15 09:59

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:贺磊    来源:51CTO博客

#
SQL
分享:
  replace into和insert into on duplicate key 区别
  replace的用法
  · 当不冲突时相当于insert,其余列默认值
  · 当key冲突时,自增列更新,replace冲突列,其余列默认值
  · Com_replace会加1
  · Innodb_rows_updated会加1
  Insert into …on duplicate key的用法
  · 不冲突时相当于insert,其余列默认值
  · 当与key冲突时,只update相应字段值。
  · Com_insert会加1
  · Innodb_rows_inserted会增加1
  实验展示
  表结构
create table helei1(
id int(10) unsigned NOT NULL AUTO_INCREMENT,
name varchar(20) NOT NULL DEFAULT '',
age tinyint(3) unsigned NOT NULL default 0,
PRIMARY KEY(id),
UNIQUE KEY uk_name (name)
)
ENGINE=innodb AUTO_INCREMENT=1
DEFAULT CHARSET=utf8;
</br>
  表数据
root@127.0.0.1 (helei)> select * from helei1;
+----+-----------+-----+
| id | name | age |
+----+-----------+-----+
| 1 | 贺磊 | 26 |
| 2 | 小明 | 28 |
| 3 | 小红 | 26 |
+----+-----------+-----+
3 rows in set (0.00 sec)
replace into用法
root@127.0.0.1 (helei)> replace into helei1 (name) values('贺磊');
Query OK, 2 rows affected (0.00 sec)
root@127.0.0.1 (helei)> select * from helei1;
+----+-----------+-----+
| id | name | age |
+----+-----------+-----+
| 2 | 小明 | 28 |
| 3 | 小红 | 26 |
| 4 | 贺磊 | 0 |
+----+-----------+-----+
3 rows in set (0.00 sec)
root@127.0.0.1 (helei)> replace into helei1 (name) values('爱璇');
Query OK, 1 row affected (0.00 sec)
root@127.0.0.1 (helei)> select * from helei1;
+----+-----------+-----+
| id | name | age |
+----+-----------+-----+
| 2 | 小明 | 28 |
| 3 | 小红 | 26 |
| 4 | 贺磊 | 0 |
| 5 | 爱璇 | 0 |
+----+-----------+-----+
4 rows in set (0.00 sec)
  replace的用法
  当没有key冲突时,replace into 相当于insert,其余列默认值
  当key冲突时,自增列更新,replace冲突列,其余列默认值
  Insert into …on duplicate key:
root@127.0.0.1 (helei)> select * from helei1;
+----+-----------+-----+
| id | name | age |
+----+-----------+-----+
| 2 | 小明 | 28 |
| 3 | 小红 | 26 |
| 4 | 贺磊 | 0 |
| 5 | 爱璇 | 0 |
+----+-----------+-----+
4 rows in set (0.00 sec)
root@127.0.0.1 (helei)> insert into helei1 (name,age) values('贺磊',0) on duplicate key update age=100;
Query OK, 2 rows affected (0.00 sec)
root@127.0.0.1 (helei)> select * from helei1;
+----+-----------+-----+
| id | name | age |
+----+-----------+-----+
| 2 | 小明 | 28 |
| 3 | 小红 | 26 |
| 4 | 贺磊 | 100 |
| 5 | 爱璇 | 0 |
+----+-----------+-----+
4 rows in set (0.00 sec)
root@127.0.0.1 (helei)> select * from helei1;
+----+-----------+-----+
| id | name | age |
+----+-----------+-----+
| 2 | 小明 | 28 |
| 3 | 小红 | 26 |
| 4 | 贺磊 | 100 |
| 5 | 爱璇 | 0 |
+----+-----------+-----+
4 rows in set (0.00 sec)
root@127.0.0.1 (helei)> insert into helei1 (name) values('爱璇') on duplicate key update age=120;
Query OK, 2 rows affected (0.01 sec)
root@127.0.0.1 (helei)> select * from helei1;
+----+-----------+-----+
| id | name | age |
+----+-----------+-----+
| 2 | 小明 | 28 |
| 3 | 小红 | 26 |
| 4 | 贺磊 | 100 |
| 5 | 爱璇 | 120 |
+----+-----------+-----+
4 rows in set (0.00 sec)
root@127.0.0.1 (helei)> insert into helei1 (name) values('不存在') on duplicate key update age=80;
Query OK, 1 row affected (0.00 sec)
root@127.0.0.1 (helei)> select * from helei1;
+----+-----------+-----+
| id | name | age |
+----+-----------+-----+
| 2 | 小明 | 28 |
| 3 | 小红 | 26 |
| 4 | 贺磊 | 100 |
| 5 | 爱璇 | 120 |
| 8 | 不存在 | 0 |
+----+-----------+-----+
5 rows in set (0.00 sec)
  总结
  replace into这种用法,相当于如果发现冲突键,先做一个delete操作,再做一个insert 操作,未指定的列使用默认值,这种情况会导致自增主键产生变化,如果表中存在外键或者业务逻辑上依赖主键,那么会出现异常。因此建议使用Insert into …on duplicate key。由于编写时间也很仓促,文中难免会出现一些错误或者不准确的地方,不妥之处恳请读者批评指正。

上文内容不用于商业目的,如涉及知识产权问题,请权利人联系博为峰小编(021-64471599-8017),我们将立即处理。  
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号