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)不能没有索引!

原文地址:https://www.cnblogs.com/drake-guo/p/6101531.html