您当前的位置:首页 > 文章 > MySQL 报错 Error Code:

MySQL 报错 Error Code: 1175. You are using safe update mode ...

作者:凡尘dba 时间:2023-11-27 阅读数:171 人阅读

一、执行 update 报错


Error Code: 1175. You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column.

解决办法:

执行 update 的时候如果报这个错,执行前设置 set sql_safe_updates=0 即可。


原因如下:


-U, --safe-updates Only allow UPDATE and DELETE that uses keys.

mysql 中 safe-updates 模式可以限制不加条件对表的更新或删除,这样对数据安全有一定的好处,可以有效的防止误操作,但更新删除也有一定的限制,以下情况会被限制:


1、safe-updates 模式下,不加条件对表 update,报错 ERROR 1175 (HY000)。


mysql> update test set name='aaa';

ERROR 1175 (HY000): You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column.


2、safe-updates 模式下,即使加了条件,没有用 limit 限制,也报错 ERROR 1175 (HY000)。

mysql> update test set name='abc' where name='aaa';

ERROR 1175 (HY000): You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column.


3、safe-updates 模式下,同样的条件,加了limit限制,可以更新。

mysql> update test set name='abc' where name='aaa' limit 1;

Query OK, 1 row affected (0.00 sec)

Rows matched: 1 Changed: 1 Warnings: 0


4、safe-updates 模式下,给where条件的列加个索引后,也可以更新。

mysql> ALTER TABLE test ADD INDEX idx_name (name);

Query OK, 0 rows affected (0.01 sec)

Records: 0 Duplicates: 0 Warnings: 0


mysql> update test set name='abc' where name='c';

Query OK, 1 row affected (0.00 sec)

Rows matched: 1 Changed: 1 Warnings: 0

本站大部分文章、数据、图片均来自互联网,一切版权均归源网站或源作者所有。

如果侵犯了您的权益请来信告知我们删除。邮箱:1451803763@qq.com