mysql sql_safe_updates 分析
生活随笔
收集整理的這篇文章主要介紹了
mysql sql_safe_updates 分析
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
排名前5的SQL悲劇中肯定有:
delete from table t /* where true */; update t set col='new_value' /* where true */由于漏掉where條件或者拼接SQL后的where條件部分為true,這時整個表都被刪除/更新了...
在使用mysql的應用中,避免此類低級錯誤的方法: 1、應用仔細檢查(小心才能使得萬年船啊~) 2、開啟sql_safe_updates? mysql> set sql_safe_updates=1; Query OK, 0 rows affected (0.00 sec) mysql> delete from t; ERROR 1175 (HY000): You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column mysql> delete from t where 1=1; ERROR 1175 (HY000): You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column 本文根據mysql源碼介紹一下sql_safe_updates的邏輯
sql_safe_update開啟后,mysql server層在調用mysql_update/mysql_delete時對where和limit進行判斷后決定是否執行,代碼調用關系: dispatch_command |->mysql_parse|->mysql_execute_command->mysql_update/mysql_delete mysql_update:(sql/sql_update.cc)
當where條件中column沒有索引可用且無limit限制時會拒絕更新
當where條件為常量或者為空,或者where條件中column沒有索引可用且無limit限制時拒絕刪除
不過開啟sql_safe_updates后,update和delete在修改數據時,如果不帶limit,需要where條件可以走索引,否則會報錯.
轉載于:https://www.cnblogs.com/duanxz/p/3891540.html
總結
以上是生活随笔為你收集整理的mysql sql_safe_updates 分析的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: iOS多线程开发(三)---Run Lo
- 下一篇: HipChat上传文件报未知错误解决方案