sql_safe_updates

1.官方文档

首先我们来看一下官方文档(5.6)对该参数的解释:

sql_safe_updates     Yes Both Yes

##因为sql_safe_updates是SYSTEM VAR,所以我们无法直接在mysql启动命令行和控制文件中添加该参数,只能在等能mysql实例后执行 set global/session sql_safe_updates=1;来设置。

但是我们可以通过init_connect参数来实现在控制文件中设置sql_safe_updates的目的,我们可以在控制文件中添加如下记录(注意init_connect对于有super权限的用户是无效的)

init_connect='SET SQL_SAFE_UPDATES=1'

sql_safe_updates

If set to 1, MySQL aborts UPDATE or DELETE statements that do not use a key in the WHERE clause or a LIMIT clause. (Specifically, UPDATE statements must have a WHERE clause that uses a key or a LIMIT clause, or both. DELETE statements must have both.) This makes it possible to catch UPDATE or DELETEstatements where keys are not used properly and that would probably change or delete a large number of rows. The default value is 0.

##官方文档上的解释和我实际测试的结果有些出入,还是以实际测试结果为准

2.测试

2.1 测试库版本

mysql> select version();
+------------+
| version()  |
+------------+
| 5.6.26-log |
+------------+

2.2 update及delete测试

2.2.1 测试表结构如下

CREATE TABLE `insure_user_info` (`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键',`order_id` varchar(100) DEFAULT NULL,`insure_name` varchar(50) DEFAULT NULL',`insure_id_type` varchar(20) DEFAULT NULL,`insure_idcard` varchar(50) DEFAULT NULL,`insure_birthdate` date DEFAULT NULL,`insure_sex` varchar(10) DEFAULT,`insure_phone` decimal(20,0) DEFAULT,`insure_mail` varchar(100) DEFAULT NULL,`insure_province` varchar(100) DEFAULT,`insure_city` varchar(100) DEFAULT NULL,`insure_address` varchar(100) DEFAULT NULL,`invitation_code` varchar(100) DEFAULT NULL,`create_time` varchar(100) DEFAULT NULL,`is_Policy_send` varchar(10) DEFAULT '不寄送',PRIMARY KEY (`id`),KEY `idx_orderid` (`order_id`),KEY `ind_test_1` (`insure_idcard`,`insure_mail`)
) ENGINE=InnoDB AUTO_INCREMENT=66720 DEFAULT CHARSET=utf8

2.2.2 设置 sql_safe_updates参数为on

set session sql_safe_updates=1;

2.2.3 update测试

mysql> update insure_user_info set insure_name='fei';
ERROR 1175 (HY000): You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column
##直接update,不加where条件及limit,update语句执行失败mysql> update insure_user_info set insure_name='fei' where id<10;
Query OK, 9 rows affected (0.01 sec)
Rows matched: 9  Changed: 9  Warnings: 0
##update 加上where 条件 并且id列为主键,update执行成功mysql> update insure_user_info set insure_name='fei' limit 10;
Query OK, 10 rows affected (0.00 sec)
Rows matched: 10  Changed: 10  Warnings: 0
##update不加where条件,仅适用limit,update执行成功mysql> update insure_user_info set insure_name='fei' where create_time <'2015-12-11 16:01:54';
ERROR 1175 (HY000): You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column
##update的加where条件,但是create_time列未建索引,update语句执行失败mysql> update insure_user_info set insure_name='fei' where create_time <'2015-12-11 16:01:54' limit 10;
Query OK, 9 rows affected (0.21 sec)
Rows matched: 9  Changed: 9  Warnings: 0
##update加where条件,create_time列为非索引列,同时使用limit,update语句执行成功mysql> update insure_user_info set insure_name='fei' where insure_idcard < '110000197609260652';
Query OK, 0 rows affected (0.00 sec)
Rows matched: 0  Changed: 0  Warnings: 0
##update 加where子句,where子句中列为组合索引的prefix列,update执行成功mysql> update insure_user_info set insure_name='fei' where insure_mail < '22@qq.com';
ERROR 1175 (HY000): You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column
##update 加where子句,where子句中列为组合索引的非prefix列,update执行失败

2.2.4 delete测试

mysql> delete from insure_user_info;
ERROR 1175 (HY000): You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column
##直接delete某张表,不加任何条件,delete语句执行失败mysql> delete from insure_user_info limit 10;
ERROR 1175 (HY000): You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column
##delete 不加where字句,仅使用limit,delete语句失败mysql> delete from insure_user_info where create_time <'2015-12-11 16:01:54';
ERROR 1175 (HY000): You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column
##delete 加where子句,但是where子句中create_time列不是索引列,delete执行失败mysql> delete from insure_user_info where id <10;
Query OK, 9 rows affected (0.01 sec)
##delete 加where子句,where子句中 id 为主键索引,delete执行成功mysql> delete from insure_user_info where create_time <'2015-12-11 16:01:54' limit 10;
Query OK, 9 rows affected (0.18 sec)
##delete 加where子句,但是where子句中create_time不是索引列,同时使用limit,delete执行成功mysql> delete from insure_user_info where id <20 limit 10;
Query OK, 10 rows affected (0.00 sec)
##delete 加where子句,where子句中id列是主键,同时使用limit,delete执行成功mysql> delete from insure_user_info where insure_idcard <'110000197609260652';
Query OK, 10 rows affected (0.00 sec)
##delete 加where子句,where子句中insure_phone列为组合索引的prefix列,delete执行成功mysql> delete from insure_user_info where insure_mail<'435065315@163.com';
ERROR 1175 (HY000): You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column
##delete 加where子句,where子句中insure_mail为组合索引的非prefix列,delete执行成功

3.总结

3. 总结
如果设置了sql_safe_updates=1,那么update语句必须满足如下条件之一才能执行成功
1)使用where子句,并且where子句中列必须为prefix索引列
2)使用limit
3)同时使用where子句和limit(此时where子句中列可以不是索引列)delete语句必须满足如下条件之一才能执行成功
1)使用where子句,并且where子句中列必须为prefix索引列
2)同时使用where子句和limit(此时where子句中列可以不是索引列)

NOTE: 所有第三方程序中的sql不受以上所说限制

sql_safe_updates相关推荐

  1. mysql sql_safe_updates 分析

    排名前5的SQL悲剧中肯定有: delete from table t /* where true */; update t set col='new_value' /* where true */ ...

  2. 史上最实用mysql参数之一-----sql_safe_updates

    mysql数据库是可以开启安全模式,不过默认情况下,安全模式不开启的,下面就来说说什么是mysql的安全模式 不知道小伙伴们是否有过维护的数据库表业务数据被人或者因为程序bug导致全表更新,全表删除的 ...

  3. MySQL sql_safe_updates 安全更新模式

    Error Code: 1175. You are using safe update mode and you tried to update a table without a WHERE tha ...

  4. mysql 参数sql_safe_updates限制update/delete

    鉴于以前出现的数据大表误更新和全表误删除操作.影响服务使用和数据安全. 为了防止线上业务出现以下3种情况影响线上服务的正常使用和不小心全表数据删除: 1:没有加where条件的全表更新操作 ; 2:加 ...

  5. 三十之惑–面霸的八月(第二部分)

    书接上回,今天叙述小米的面试经历. 这里可能有一些技术理解和技术方案,欢迎讨论.另昨天共计收入7笔共95元,够我喝几杯咖啡了,谢谢所有捐钱的朋友. 如果你心疼我码字辛苦,有钱朋友钱场,没钱的请拉朋友来 ...

  6. Error Code: 1175. You are using safe update mode and you tried to ......

    MySQL提示的错误信息: Error Code: 1175. You are using safe update mode and you tried to update a table witho ...

  7. MySQL/MariaDB基础及简单SQL语句

    MySQL是一种开放源代码的关系型数据库管理系统(RDBMS),使用最常用的数据库管理语言--结构化查询语言(SQL)进行数据库管理. 在Linux操作系统内核中提供了MySQL或MariaDB的rp ...

  8. MySQL错误代码:MySQL Workbench中UPDATE期间的1175错误代码

    本文翻译自:MySQL error code: 1175 during UPDATE in MySQL Workbench I'm trying to update the column visite ...

  9. update操作报错

    为什么80%的码农都做不了架构师?>>>    相关操作: update table set column='***'; 执行后提示:ERROR 1175 (HY000): You ...

最新文章

  1. 万字干货|逻辑回归最详尽解释
  2. 编码不一致问题-Illegal mix of collations
  3. 《集体智慧编程》第九章
  4. 负债的阶梯,你在第几层?
  5. [转]Installing Memcached on Windows
  6. eclipse新发现功能之dos和terminal(ssh连接)
  7. 7-4 N皇后 (28 分)(思路+详解)
  8. vue 父传子_Vue.js教程Vue基本指令
  9. Entity Framework 异常档案
  10. VS2012新建项目
  11. 基于pytorch实现线性回归
  12. 【教你Win7下如何激活快速启动栏】
  13. 剑网3:指尖江湖手游脚本哪个好呢? 剑网3:指尖江湖手游自动采集IOS脚本
  14. Unity 3D 如何获取鼠标移动事件
  15. #SVN Skipped ‘xxx‘ -- Node remains in conflict 错误的解决办法#
  16. python一行输入n个数据
  17. DVE 查看覆盖率方法
  18. 2020年团体程序设计天梯赛
  19. 多分类-- ROC曲线和AUC值
  20. Tensorflow2.1入门 第六章:循环神经网络

热门文章

  1. 阿里云企业官网建站标准版、高级版和尊贵版功能区别选择攻略
  2. 【2021最新版】Dubbo面试题总结(40道题含答案解析)
  3. 打开一个英文文本文件,将其中大写字母变成小写,小写字母变成大写。
  4. 有了金刚钻,不惧瓷器活 | 在数据文件上轻松使用SQL
  5. php 身份证号码获取星座和生肖
  6. JSP Cookies 处理
  7. 数据结构-单链表基本操作
  8. 四川大学计算机学院工会电话,工会办公室
  9. OA-SLAM:在视觉SLAM中利用对象进行相机重新定位
  10. sql建表+主键+外键