公司数据库数据被我更新成 null 了,救命!

2022-07-11 16:30:58 +08:00
 kaiger

数据库有些数据值为: "保密"

前面我在用 regex_replace 函数更新一些不规范数据

我顺手就用 update lawyers set working_years=regexp_replace(working_years, "保密", null); 把 "保密" 更新为 null

结果 working_years 字段全部被更新为 null !!!

有办法恢复吗,大佬救命!!!

10944 次点击
所在节点    MySQL
79 条回复
hs444
2022-07-11 16:47:32 +08:00
之前在生产环境也干过这种事,但用的不是 mysql ,操作时事务也是手动提交的,一瞬间汗都要下来了,然后回滚了
ikaros
2022-07-11 16:48:53 +08:00
再把 null 更新成保密啊, 问题不大, 反正应该也没人知道哪些保密 哪些 null
danhahaha
2022-07-11 16:49:23 +08:00
保密不就是 null 的意思吗
kaiger
2022-07-11 16:51:16 +08:00
解决了,想问一下大佬

update lawyers set working_years=regexp_replace(working_years, "保密", null)

这条命令为什么会把我的全部数据替换为 null
'
F12
2022-07-11 16:51:21 +08:00
还能上 v2 上找解决方案,应该问题不大
wzly
2022-07-11 16:52:48 +08:00
先不要操作了,看下 data 目录下的 binlog 日志文件吧,以 utf8 打开,一个一个补数据吧
geekershared
2022-07-11 16:54:13 +08:00
@kaiger 怎么解决的,目测你是没有加 where working_years = "保密"。
hay313955795
2022-07-11 16:54:44 +08:00
@kaiger 楼主是怎么解决的呢?
philchang1995
2022-07-11 16:56:32 +08:00
@kaiger #24 REGEXP_REPLACE() 函数在 str 中使用 replacement 替换和正则表达式 regexp 匹配的内容,并返回替换后的结果。

如果 str, regexp 或者 replacement 为 NULL ,REGEXP_REPLACE() 将返回 NULL
philchang1995
2022-07-11 16:57:08 +08:00
同问 你是咋把数据找回来的
RRRoger
2022-07-11 16:58:07 +08:00
@philchang1995 regexp_replace 适用于 select 不适用于 update
leiuu
2022-07-11 16:58:30 +08:00
https://dev.mysql.com/doc/refman/8.0/en/regexp.html#function_regexp-replace

If expr, pat, or repl is NULL, the return value is NULL.
MarkP
2022-07-11 17:04:01 +08:00
@kaiger #24 把保密更改为 null ,为什么要用 regexp_replace 这个正则函数?

正常思路难道不是这样吗?
update lawyers set working_years=null where regexp_replace='保密';


至于为什么换替换为 null ,根据文档 https://dev.mysql.com/doc/refman/8.0/en/regexp.html

Replaces occurrences in the string expr that match the regular expression specified by the pattern pat with the replacement string repl, and returns the resulting string. If expr, pat, or repl is NULL, the return value is NULL.

大概意思正则匹配到了,则返回替换后的结果字符串,如果给定的三个参数里有 null ,则返回 null 。
而你的第三个参数恰好是 null ,所以全被替换成 null 了。
kaiger
2022-07-11 17:06:12 +08:00
@MarkP #33

我前面用的 regex_replace 函数,我懒得重新写 sql 语句,就直接在上一条语句上修改了
MoYi123
2022-07-11 17:06:53 +08:00
下辈子改数据库前记得先打 begin;
MarkP
2022-07-11 17:08:03 +08:00
@RRRoger update 也适用 update ,之前搬一个老项目的数据库,我常用。
ixinshang
2022-07-11 17:08:40 +08:00
@lambdaq 多谢分享
kaiger
2022-07-11 17:08:52 +08:00
@MoYi123 #35

不用下辈子,今天就打。
Jooooooooo
2022-07-11 17:11:06 +08:00
都是惨痛教训:

1. 不是迫不得已不要手动改库

2. 真的要手动改库一定先 select 一下看看影响的行数

3. 改之前先改一条试试看, 防止写错了

4. 大规模改库要备份
MarkP
2022-07-11 17:13:57 +08:00
@kaiger 那也应该用 replace 而不是 regexp_replace

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/865464

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX