操作mysql資料庫,删除表中的某一行資料提示如下錯誤:ERROR 1175 (HY000): You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column
錯誤提示:正在使用安全更新模式,嘗試更新表沒有使用鍵列的where條件;
原因是:mysql有個叫SQL_SAFE_UPDATES的變量,為了資料庫更新操作的安全性,此值預設為1,是以才會出現更新失敗的情況。
舉例如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
<code>mysql> </code><code>select</code> <code>* from </code><code>test</code><code>;</code>
<code>+----+--------+</code>
<code>| </code><code>id</code> <code>| name |</code>
<code>| 1 | anglea |</code>
<code>| 2 | baby |</code>
<code>| 3 | jerry |</code>
<code>| 4 | tom |</code>
<code>| 5 | yong |</code>
<code>mysql> delete from </code><code>test</code> <code>where name=</code><code>'yong'</code><code>; </code>
<code>ERROR 1175 (HY000): You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column</code>
檢視設定:
<code>mysql> show variables like </code><code>'sql_safe%'</code><code>;</code>
<code>+------------------+-------+</code>
<code>| Variable_name | Value |</code>
<code>| sql_safe_updates | ON |</code>
下面是SQL_SAFE_UPDATES變量為0和1時的取值說明:
SQL_SAFE_UPDATES有兩個取值0和1, 或ON 和OFF;
SQL_SAFE_UPDATES = 1,ON時,不帶where和limit條件的update和delete操作語句是無法執行的,即使是有where和limit條件但不帶key column的update和delete也不能執行。
SQL_SAFE_UPDATES =0,OFF時,update和delete操作将會順利執行。那麼很顯然,此變量的預設值是1。
是以,出現1175錯誤的時候,可以先設定SQL_SAFE_UPDATES的值為0 OFF,然後再執行更新;
以下2條指令都可以;
mysql> set sql_safe_updates=0;
mysql> set sql_safe_updates=off;
<code>| sql_safe_updates | OFF |</code>
<code>mysql> delete from </code><code>test</code> <code>where name=</code><code>'yong'</code><code>;</code>
<code>Query OK, 1 row affected (0.00 sec)</code>
更改隻在目前生效,退出mysql,再次登入後恢複為預設。
本文轉自 模範生 51CTO部落格,原文連結:http://blog.51cto.com/mofansheng/1694516,如需轉載請自行聯系原作者