我在練習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)不能沒有索引!