位置: 编程技术 - 正文

mysql利用参数sql_safe_updates限制update/delete范围详解(mysql参数表)

编辑:rootadmin

推荐整理分享mysql利用参数sql_safe_updates限制update/delete范围详解(mysql参数表),希望有所帮助,仅作参考,欢迎阅读内容。

文章相关热门搜索词:mysql设置参数查询sql语句,mysql参数化查询,mysql参数表,mysql常用参数设置,mysql设置参数查询sql语句,mysql设置参数查询sql语句,mysql设置参数查询sql语句,mysql设置参数查询sql语句,内容如对您有帮助,希望把文章链接给更多的朋友!

前言

大家应该都知道,我们在mysql运维中出现过不少因为update/delete条件错误导致数据被误更新或者删除的case,为避免类似问题的发生,可以用sql_safe_updates参数来对update/delete做限制。这个参数设置为on后,可防止因程序bug或者DBA手工误操作导致的整个表被更新或者删除的情况。下面话不多说了,来一起看看详细的介绍吧。

设置这个参数时需要注意一下几点:

a、设置前需要确认程序中所有的update和delete都符合sql_safe_updates的限制规范,不然程序会报错。

b、5.0,5.1都是session级别的,5.6是global&session级别;低版本的数据库只能在程序创建session时设置带上set sql_safe_updates=on;高版本的数据库可以直接set global set sql_safe_updates=on,设置完成后让程序重连后生效。

限制规范:

示例表结构:

1、update

a、报错条件:不带where、带where无索引、where条件为常量

不带where:update delay_monitor set Ftime=now();

带where无索引:update delay_monitor set Ftime=now() where Fgtid='test';

where条件为常量:update delay_monitor set Ftime=now() where 1;

b、执行条件:带where带索引、不带where+带limit、带where无索引+limit、带where有索引+limit、where条件为常量+limit

带where带索引:update delay_monitor set Ftime=now() where id=2;

不带where+带limit: update delay_monitor set Ftime=now() limit 1;

mysql利用参数sql_safe_updates限制update/delete范围详解(mysql参数表)

带where无索引+limit:update delay_monitor set Ftime=now() where Fgtid='test' limit 1;

带where有索引+limit:update delay_monitor set Ftime=now() where id =2 limit1;

where条件为常量+limit:update delay_monitor set Ftime=now() where 1 limit 1;

2、delete

相对于update,delelte的限制会更为严格;where条件为常量或者为空,将不予执行。

a、报错条件:不带where、带where无索引、不带where+带limit、where条件为常量、where条件为常量+limit

不带where:delete delay_monitor set Ftime=now();

带where无索引:delete delay_monitor set Ftime=now() where Fgtid='test';

不带where+带limit: delete delay_monitor set Ftime=now() limit 1;

where条件为常量:delete delay_monitor set Ftime=now() where 1;

where条件为常量+limit:delete delay_monitor set Ftime=now() where 1 limit 1;

b、执行条件:带where带索引、带where无索引+limit、带where有索引+limt

带where带索引:delete delay_monitor set Ftime=now() where id=2;

带where无索引+limit:delete delay_monitor set Ftime=now() where Fgtid='test' limit 1;

带where有索引+limit:delete delay_monitor set Ftime=now() where id =2 limit1;

总结如下表:key表示所有、const表示常量

操作 no where where key where nokey limit where nokey+limit where key+limit where const where const+limit delete NO YES NO NO YES YES NO NO update NO YES NO YES YES YES NO YES

总结

本文链接地址:https://www.jiuchutong.com/biancheng/348371.html 转载请保留说明!

上一篇:解析MySQL中存储时间日期类型的选择问题(mysql存储数据的原理)

下一篇:简析mysql字符集导致恢复数据库报错问题(mysql字符集的作用)

免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

鄂ICP备2023003026号

网站地图: 企业信息 工商信息 财税知识 网络常识 编程技术

友情链接: 武汉网站建设 电脑维修 湖南楚通运网络