1. 程式人生 > >更新資料庫中資料時出現: 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 m

更新資料庫中資料時出現: 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 m

在資料庫中更新資料時報錯: 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(錯誤及操作見截圖)

1.sql:   update tableName set employee_detailed_status='xx', employee_status_code='xx' where employee_detailed_status='xx'

 

2.通過錯誤提示可以看出:  當前資料庫使用的是 safe update mode(安全更新模式),並且在update時where 中沒有把主鍵當做條件。因為在該模式會導致非主鍵條件下無法執行update或者delete命令,因此會報錯。

3.如何解決這個問題呢?

第一種方法(簡單粗暴):

執行命令SET SQL_SAFE_UPDATES = 0 來修改下資料庫模式,這樣再去執行update語句或者delete語句即可成功。

第二種方法(投機取巧法,這種方法不一定適用於所有情況,如果這種方法不行則採用方法1):

既然在這種模式下需要將主鍵作為條件,那我們就將主鍵作為條件加到sql中,如何加呢?

那我們就分析我們的sql語句,更改後的sql如下: 

update tableName set employee_detailed_status='xx', employee_status_code='xx' where employee_detailed_status='xx' and id is not null;