MySQL中replace into与replace区别是什么

44次阅读
没有评论

共计 3513 个字符,预计需要花费 9 分钟才能阅读完成。

本篇内容介绍了“MySQL 中 replace into 与 replace 区别是什么”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让丸趣 TV 小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

0. 故事的背景

【表格结构】

CREATE TABLE `xtp_algo_white_list` (
 `strategy_type` int DEFAULT NULL,
 `user_name` varchar(64) COLLATE utf8_bin DEFAULT NULL,
 `status` int DEFAULT NULL,
 `destroy_at` datetime DEFAULT NULL,
 `created_at` datetime DEFAULT CURRENT_TIMESTAMP,
 `updated_at` datetime DEFAULT CURRENT_TIMESTAMP,
 UNIQUE KEY `xtp_algo_white_list_UN` (`strategy_type`,`user_name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin

# `strategy_type`,`user_name`  这两个是联合唯一索引,多关注后续需要用到!!!

【需求:】

根据表格里面,209133002266 账户的数据,重新插入一个用户 20220302001,使得新生成的数据中 strategy_type status destroy_at 字段与 209133002266 用户的一致。

使用 update 一条一条更新也行,但是比较慢。

使用 replace into 效果会高很多,但是深入研究发现也有一些坑的地方

1.replace into 的使用方法

replace into xtp_algo_white_list (`strategy_type`, `user_name`, `status`, `destroy_at`)
select strategy_type ,20220302001, status, destroy_at from xtp_algo_white_list xawl where xawl.user_name = 209133002266;
# replace into  后面跟表格 + 需要插入的所有字段名(自动递增字段不用写)# select  后面选择的字段,如果根据查询结果取值,则写字段名;如果是写死的,则直接写具体值即可
#  可以理解为,第一部分是插入表格的结构,第二部分是你查询的数据结果 

2. 有唯一索引时—replace into 与 replace 效果

step1:第一次执行 sql 情况

replace into xtp_algo_white_list (`strategy_type`, `user_name`, `status`, `destroy_at`)
select strategy_type ,20220302001, status, destroy_at from xtp_algo_white_list xawl where xawl.user_name = 209133002266;

【执行完之后,查询结果如下:】

step2:第二次执行 sql 情况

为什么第二次执行的时候,显示 update 12 行的数据且 created at 数据更新了,而第一次会显示 update 6 行???

1. 因为在执行 sql 的时候,replace into 其实分了两个步骤执行。第一步是将查询到数据转化为新的数据。第二步,新的数据如果表中已经有相同的内容,则删除掉。如果没有相同的内容,则直接插入新的数据。

2. 因如上第一次执行的时候,已经生成一次新数据了,第二次会先删除,再把最新的数据插入进去,最终才显示 update 12 行

step3:第三次执行 sql 情况

#  此时执行的是 replace 
replace xtp_algo_white_list (`strategy_type`, `user_name`, `status`, `destroy_at`)
select strategy_type ,20220302001, status, destroy_at from xtp_algo_white_list xawl where xawl.user_name = 209133002266;

最终查看到的情况与第二次执行的 sql 一样。

当新数据已经存在的时候,replace into 与 replace 是一样的

后续删除所有 20220302001,执行 1 次,2 次 sql,发现 replace into 与 replace 效果都是一样的

【总结:】当有唯一索引限制的时候,如果新增的数据会受限于唯一索引,则数据只会插入一次,如果已经存在则会先删除再插入。此时 replace into 与 replace 效果一样。

3. 没有唯一索引时—replace into 与 replace

我们将 strategy_type user_name 联合唯一索引删除,且删除 20220302001 用户所有数据。最终表格结构如下:

CREATE TABLE `xtp_algo_white_list` (
 `strategy_type` int DEFAULT NULL,
 `user_name` varchar(64) COLLATE utf8_bin DEFAULT NULL,
 `status` int DEFAULT NULL,
 `destroy_at` datetime DEFAULT NULL,
 `created_at` datetime DEFAULT CURRENT_TIMESTAMP,
 `updated_at` datetime DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin

1).replace 函数的具体情况

step1:执行如下 replace 对应 sql:

replace xtp_algo_white_list (`strategy_type`, `user_name`, `status`, `destroy_at`)
select strategy_type ,20220302001, status, destroy_at from xtp_algo_white_list xawl where xawl.user_name = 209133002266;

step2:再次执行 replace 对应 sql:

MySQL 中 replace into 与 replace 区别是什么

MySQL 中 replace into 与 replace 区别是什么

第二次执行 replace 对应 sql,因为没有唯一索引限制,结果原始数据居然没变动。又重新生成了新的 6 条数据。

如果后续还执行如上的 sql,则数据还会继续增加

2).replace into 函数的具体情况

执行之前,先清理数据,将所有 20220302001 的数据都删除掉

step1:执行如下 replace into 对应 sql:

replace into xtp_algo_white_list (`strategy_type`, `user_name`, `status`, `destroy_at`)
select strategy_type ,20220302001, status, destroy_at from xtp_algo_white_list xawl where xawl.user_name = 209133002266;

MySQL 中 replace into 与 replace 区别是什么

MySQL 中 replace into 与 replace 区别是什么

step2:再次执行 replace into 对应 sql:

MySQL 中 replace into 与 replace 区别是什么

MySQL 中 replace into 与 replace 区别是什么

最终发现,没有唯一索引的时候,replace into 与 replace 居然一摸一样的效果,都是继续增加数据。

4.replace 的用法

单独 replace 的作用是替换字段中某数值的显示效果。可以数值中的部分替换、也可以全部替换。

如下表格,将 user_name 的字段,20220302 改为 A_20220303 显示,并且新字段叫做 new_name 显示

MySQL 中 replace into 与 replace 区别是什么

select *, replace(user_name,20220302, A_20220303) as  new_name  from xtp_algo_white_list where user_name = 20220302001;

MySQL 中 replace into 与 replace 区别是什么

“MySQL 中 replace into 与 replace 区别是什么”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注丸趣 TV 网站,丸趣 TV 小编将为大家输出更多高质量的实用文章!

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-07-13发表,共计3513字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)