MySQL sql_safe_updates 分析


我在練習MySQL操作語句時,使用一條完全沒有錯誤的語句:

update students set name='drake' where name='chuan';

 

卻報了如下錯誤:

Error Code: 1175. You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column To disable safe mode, toggle the option in Preferences -> SQL Queries and reconnect.
簡單翻譯一下:
你正在使用 安全更新模式(safe upate mode)並且你在嘗試 update 一個表時 沒有用帶有鍵的列 作為where條件,在首選項中切換選項。

 

初學者在修改一個值時可能會用以下語句:

update t set col='new_value' 

 

而這正是很多人常犯的錯誤。因為他沒有限定條件,會將表中所有的記錄都修改一遍。

為了防止這種錯誤出現,我們可以開啟安全更新模式(safe update mode)

 

set [global] SQL_SAFE_UPDATES = 1;

 

 

update操作中:當where條件中列(column沒有索引可用且無limit限制時會拒絕更新。where條件為常量且無limit限制時會拒絕更新。

delete操作中: 當①where條件為常量,where條件為空,③或where條件中 列(column沒有索引可用且無limit限制時拒絕刪除。

 

需要注意的是:

update操作中,where可以為常量  ,where條件中列(column)可以沒有索引。但是需要有limit限制。

然而delete要嚴格一些:where不能為常量,且where條件中列(column)不能沒有索引!

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM