開發懵逼了!誤用一個雙引號,生產數據全變0!
最近經常碰到開發誤刪除誤更新數據,這不,他們又給我找了個麻煩,我們來看下整個過程,把我坑得夠慘。
圖片來自 Pexels
過程
由于開發需要在生產環節中修復數據,需要執行 120 條 SQL 語句,需要將數據進行更新。
于是開發連上了生產數據庫,首先執行了第一條 SQL:
- update tablename set source_name = "bj1062-北京市朝陽區常營北辰福第"
- where source_name = "-北京市朝陽區常營北辰福第"
我們仔細看了下,這個 SQL,的確沒有什么問題,where 條件也是正常的,大意就是將這個地址的前面加字符串 bj1062,是真的沒有錯誤么?
是的,沒有錯誤。開發執行完成后,結果的確是符合預期。
然后開發執行了剩下的 SQL,都是和上面的 SQL 一樣,將地址進行更新。
執行完成后,開發懵逼了,發現 source_name 都變成了 0,開發趕緊給我打電話說:
Harvey,我執行了 update,where 條件都是對的,set 的值也是對的,但是 set 后的字段全部都變成了 0,你趕緊幫我看看,看看能不能恢復數據。
我趕緊登上服務器,查看了這段時間的 binlog,發現了大量的 update tablename set source_name=0 的語句,利用 binlog2sql 進行了解析。
項目地址:
- binlog2sql https://github.com/danfengcao/binlog2sql
趕緊和開發確定了操作的時間點,生成 flashback 的 SQL,進行了數據恢復,同時保留現場證據。
然后對開發執行的 SQL 進行了 check,發現了幾條很詭異的 SQL:
這幾條 SQL 的引號位置跑到了 where 字段名字后面,簡化后的 SQL 變成了:
- update tbl_name set str_col="xxx" = "yyy"
那么這個 SQL 在 MySQL 他是如何進行語義轉化的呢?
可能是下面這樣的么?
- update tbl_name set (str_col="xxx" )= "yyy"
這樣就語法錯誤了,那么只會是下面這樣的形式:
- update tbl_name set str_col=("xxx" = "yyy")
而
- select "xxx" = "yyy"
的值是 0,所以:
- update tbl_name set str_col="xxx" = "yyy"
等價于:
- update tbl_name set str_col=0
所以就導致了 source_name 字段全部更新成了 0。
我們再研究下 select 形式這種語句會怎么樣。
- 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 |
- +----+---------+
我們發現,這個 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 條件轉化成了:
- ((`test`.`tbl_name`.`str_col` = 'xxx') = 'yyy')
這個條件的首先判斷 str_col 和 'xxx' 是否相等,如果相等,那么里面括號的值為 1,如果不相等,就是 0。
然后 0 或者 1 再和和 'yyy' 進行判斷,由于等號一邊是 int,另外一邊是字符串,兩邊都轉化為 float 進行比較。
可以看我之前的一篇文章 MySQL 中隱式轉換導致的查詢結果錯誤案例分析:
- http://www.fordba.com/mysql-type-convert-analysis.html
'yyy' 轉化為浮點型為 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)
這樣導致結果恒成立,也就是 select 語句等價于以下 SQL:
- select id,str_col from tbl_name where 1=1;
將查詢出所有的記錄。
小結
在寫 SQL 的過程中,一定要小心引號的位置是否正確,有時候引號位置錯誤,SQL 依然是正常的,但是卻會導致執行結果全部錯誤。在執行前必須在測試環境執行測試,結合 IDE 的語法高亮發現相應的問題。
作者:Harvey
編輯:陶家龍
出處:www.fordba.com/mysql-double-quotation-marks-accident.html