開發(fā)懵逼了!誤用一個(gè)雙引號(hào),生產(chǎn)數(shù)據(jù)全變0!
最近經(jīng)常碰到開發(fā)誤刪除誤更新數(shù)據(jù),這不,他們又給我找了個(gè)麻煩,我們來看下整個(gè)過程,把我坑得夠慘。
圖片來自 Pexels
過程
由于開發(fā)需要在生產(chǎn)環(huán)節(jié)中修復(fù)數(shù)據(jù),需要執(zhí)行 120 條 SQL 語句,需要將數(shù)據(jù)進(jìn)行更新。
于是開發(fā)連上了生產(chǎn)數(shù)據(jù)庫,首先執(zhí)行了第一條 SQL:
- update tablename set source_name = "bj1062-北京市朝陽區(qū)常營北辰福第"
- where source_name = "-北京市朝陽區(qū)常營北辰福第"
我們仔細(xì)看了下,這個(gè) SQL,的確沒有什么問題,where 條件也是正常的,大意就是將這個(gè)地址的前面加字符串 bj1062,是真的沒有錯(cuò)誤么?
是的,沒有錯(cuò)誤。開發(fā)執(zhí)行完成后,結(jié)果的確是符合預(yù)期。
然后開發(fā)執(zhí)行了剩下的 SQL,都是和上面的 SQL 一樣,將地址進(jìn)行更新。
執(zhí)行完成后,開發(fā)懵逼了,發(fā)現(xiàn) source_name 都變成了 0,開發(fā)趕緊給我打電話說:
Harvey,我執(zhí)行了 update,where 條件都是對(duì)的,set 的值也是對(duì)的,但是 set 后的字段全部都變成了 0,你趕緊幫我看看,看看能不能恢復(fù)數(shù)據(jù)。
我趕緊登上服務(wù)器,查看了這段時(shí)間的 binlog,發(fā)現(xiàn)了大量的 update tablename set source_name=0 的語句,利用 binlog2sql 進(jìn)行了解析。
項(xiàng)目地址:
- binlog2sql https://github.com/danfengcao/binlog2sql
趕緊和開發(fā)確定了操作的時(shí)間點(diǎn),生成 flashback 的 SQL,進(jìn)行了數(shù)據(jù)恢復(fù),同時(shí)保留現(xiàn)場(chǎng)證據(jù)。
然后對(duì)開發(fā)執(zhí)行的 SQL 進(jìn)行了 check,發(fā)現(xiàn)了幾條很詭異的 SQL:
這幾條 SQL 的引號(hào)位置跑到了 where 字段名字后面,簡化后的 SQL 變成了:
- update tbl_name set str_col="xxx" = "yyy"
那么這個(gè) SQL 在 MySQL 他是如何進(jìn)行語義轉(zhuǎn)化的呢?
可能是下面這樣的么?
- update tbl_name set (str_col="xxx" )= "yyy"
這樣就語法錯(cuò)誤了,那么只會(huì)是下面這樣的形式:
- update tbl_name set str_col=("xxx" = "yyy")
而
- select "xxx" = "yyy"
的值是 0,所以:
- update tbl_name set str_col="xxx" = "yyy"
等價(jià)于:
- update tbl_name set str_col=0
所以就導(dǎo)致了 source_name 字段全部更新成了 0。
我們?cè)傺芯肯?select 形式這種語句會(huì)怎么樣。
- mysql [localhost] {msandbox} (test) > select id,str_col from tbl_name where str_col="xxx" = "yyy";
- +----+---------+
- | id | str_col |
- +----+---------+
- | 1 | aaa |
- | 2 | aaa |
- | 3 | aaa |
- | 4 | aaa |
- +----+---------+
我們發(fā)現(xiàn),這個(gè) SQL 將 str_col='aaa' 的記錄也查找出來了,為什么呢?
- mysql [localhost] {msandbox} (test) > warnings
- Show warnings enabled.
- mysql [localhost] {msandbox} (test) > explain extended select id,str_col from tbl_name where str_col="xxx" = "yyy"\G
- *************************** 1. row ***************************
- id: 1
- select_type: SIMPLE
- table: tbl_name
- type: index
- possible_keys: NULL
- key: idx_str
- key_len: 33
- ref: NULL
- rows: 4
- filtered: 100.00
- Extra: Using where; Using index
- 1 row in set, 1 warning (0.00 sec)
- Note (Code 1003): /* select#1 */ select `test`.`tbl_name`.`id` AS `id`,`test`.`tbl_name`.`str_col` AS `str_col` from `test`.`tbl_name` where ((`test`.`tbl_name`.`str_col` = 'xxx') = 'yyy')
這里他把 where 條件轉(zhuǎn)化成了:
- ((`test`.`tbl_name`.`str_col` = 'xxx') = 'yyy')
這個(gè)條件的首先判斷 str_col 和 'xxx' 是否相等,如果相等,那么里面括號(hào)的值為 1,如果不相等,就是 0。
然后 0 或者 1 再和和 'yyy' 進(jìn)行判斷,由于等號(hào)一邊是 int,另外一邊是字符串,兩邊都轉(zhuǎn)化為 float 進(jìn)行比較。
可以看我之前的一篇文章 MySQL 中隱式轉(zhuǎn)換導(dǎo)致的查詢結(jié)果錯(cuò)誤案例分析:
- http://www.fordba.com/mysql-type-convert-analysis.html
'yyy' 轉(zhuǎn)化為浮點(diǎn)型為 0,0 和 0 比較恒等于 1。
- mysql [localhost] {msandbox} (test) > select 'yyy'+0.0;
- +-----------+
- | 'yyy'+0.0 |
- +-----------+
- | 0 |
- +-----------+
- 1 row in set, 1 warning (0.00 sec)
- mysql [localhost] {msandbox} (test) > select 0=0;
- +-----+
- | 0=0 |
- +-----+
- | 1 |
- +-----+
- 1 row in set (0.00 sec)
這樣導(dǎo)致結(jié)果恒成立,也就是 select 語句等價(jià)于以下 SQL:
- select id,str_col from tbl_name where 1=1;
將查詢出所有的記錄。
小結(jié)
在寫 SQL 的過程中,一定要小心引號(hào)的位置是否正確,有時(shí)候引號(hào)位置錯(cuò)誤,SQL 依然是正常的,但是卻會(huì)導(dǎo)致執(zhí)行結(jié)果全部錯(cuò)誤。在執(zhí)行前必須在測(cè)試環(huán)境執(zhí)行測(cè)試,結(jié)合 IDE 的語法高亮發(fā)現(xiàn)相應(yīng)的問題。
作者:Harvey
編輯:陶家龍
出處:www.fordba.com/mysql-double-quotation-marks-accident.html