首页 » 网站建设 » phpupdatesafemode技巧_详解mysql系统变量sql_safe_updates的用法限制update与delete

phpupdatesafemode技巧_详解mysql系统变量sql_safe_updates的用法限制update与delete

duote123 2024-12-18 0

扫一扫用手机浏览

文章目录 [+]

下面一起来看看这个别系变量的细节问题

01系统变量的查看

系统变量sql_safe_updates分会话级别和全局级别。
可以动态修正。
默认是关闭(值为0)。

phpupdatesafemode技巧_详解mysql系统变量sql_safe_updates的用法限制update与delete

show variables优先显示会话级变量的系统变量值,如果这个别系变量没有会话级别的值,则显示全局级系统变量的值,当然最清晰的办法便是加上GLOBAL或SESSION关键字来差异。

phpupdatesafemode技巧_详解mysql系统变量sql_safe_updates的用法限制update与delete
(图片来自网络侵删)

@@GLOBAL.var_name、@@SESSION.var_name 、@@LOCAL.var_name
如果在变量名前没有级别限定符,将优先显示会话级的值。

1、查看会话级别系统变量sql_safe_updates的值

mysql> show variables like 'sql_safe_updates';mysql> show session variables like 'sql_safe_updates';mysql> select @@session.sql_safe_updates;mysql> select @@sql_safe_updates;

2、查看全局级别系统变量sql_safe_updates的值

mysql> show global variables like 'sql_safe_updates';mysql> select @@global.sql_safe_updates;

02系统变量sql_safe_updates的设置

#设置会话级别的系统变量mysql> set sql_safe_updates=1;#设置全局级别的系统变量mysql> set global sql_safe_updates=1;

如果要设置全局级别的系统变量sql_safe_updates,建议在配置文件my.cnf中设置。

03实验--UPDATE语句测试

1、没有WHERE条件。

mysql> update shop set price=price1.5;ERROR 1175 (HY000): You are using safe update mode and you tried to update a table without a WHERE that uses a KEY columnmysql>

2、 没有WHERE条件,但是有LIMTI限定条件

如下测试所示,没有WHERE条件,但是有LIMIT限定的话,SQL也是可以实行的。

mysql> update shop set price=price1.5 limit 5;Query OK, 5 rows affected (0.05 sec)Rows matched: 5 Changed: 5 Warnings: 0

3、有WHERE条件,但是WHERE条件的列没有索引可用

mysql> show index from shop\G; 1. row Table: shop Non_unique: 0 Key_name: PRIMARY Seq_in_index: 1 Column_name: article Collation: A Cardinality: 931203 Sub_part: NULL Packed: NULL Null: Index_type: BTREE Comment: Index_comment: 2. row Table: shop Non_unique: 0 Key_name: PRIMARY Seq_in_index: 2 Column_name: dealer Collation: A Cardinality: 931203 Sub_part: NULL Packed: NULL Null: Index_type: BTREE Comment: Index_comment: 3. row Table: shop Non_unique: 1 Key_name: shop__price Seq_in_index: 1 Column_name: price Collation: A Cardinality: 1877 Sub_part: NULL Packed: NULL Null: Index_type: BTREE Comment: Index_comment: 3 rows in set (0.00 sec) ERROR: No query specified mysql> update shop set price = price 1.5 where dealer='K';ERROR 1175 (HY000): You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column

4、有WHERE条件,但是WHERE条件列没有索引 + LIMIT限定

mysql> update shop set price = price 1.5 where dealer='K' limit 1000;Query OK, 1000 rows affected (0.13 sec)Rows matched: 1000 Changed: 1000 Warnings: 0

5、有WHERE条件的UPDATE,而且字段有索引

mysql> update shop set price = price 1.5 where article=6156;Query OK, 1 row affected (0.00 sec)Rows matched: 1 Changed: 1 Warnings: 0 mysql>

6、有WHERE条件,但是WHERE条件为常量

mysql> update shop set price = price 1.5 where 1=1;ERROR 1175 (HY000): You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column

7、有WHERE条件,但是WHERE条件为常量+LIMIT

mysql> update shop set price = price 1.5 where 1=1 limit 10;Query OK, 10 rows affected (0.00 sec)Rows matched: 10 Changed: 10 Warnings: 004实验--DELETE语句测试

1、 DELETE语句没有WHERE条件

mysql> delete from shop;ERROR 1175 (HY000): You are using safe update mode and you tried to update a table without a WHERE that uses a KEY columnmysql>

2、DELETE语句没有WHERE条件+ LIMIT

mysql> delete from shop 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 columnmysql>

3、DELETE语句有WHERE条件,而且查询条件的字段有索引

mysql> delete from shop where article =6156;Query OK, 1 row affected (0.01 sec) mysql>

4、DELETE语句有WHERE条件,而且查询条件的字段有索引 +LIMIT

mysql> delete from shop where article< 2000 limit 1000;Query OK, 1000 rows affected (0.02 sec) mysql>

5、DELETE语句有WHERE条件,但是查询条件的字段没有索引

mysql> delete from shop where dealer='K';ERROR 1175 (HY000): You are using safe update mode and you tried to update a table without a WHERE that uses a KEY columnmysql>

6、DELETE语句有WHERE条件,但是查询字段没有索引 +LIMIT语句

mysql> delete from shop where dealer='K' limit 20;Query OK, 20 rows affected (0.05 sec) mysql>

7、DELETE语句有WHERE条件,但是WHERE条件为常量

mysql> delete from shop where 1=1;ERROR 1175 (HY000): You are using safe update mode and you tried to update a table without a WHERE that uses a KEY columnmysql>

8、DELETE语句有WHERE条件,但是WHERE条件为常量 +LIMIT

mysql> delete from shop where 1=1 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 columnmysql>05实验结果

总结如下,至于是否要开启这个别系变量,须要结合实际情形,并且和开拓职员一起协商才行。

这个变量还是很故意思的,建议大家有空可以测试一下。
后面会分享更多devops和DBA方面的内容,感兴趣的朋友可以关注一下~

标签:

相关文章

php为无色透明技巧_水货钻石其实也还行

从各种钻石中,可以看到大大小小的“包裹体” 图片来源:参考文献包裹体的种类多样。比钻石形成更早的包裹体,叫“原生包裹体”;与钻石同...

网站建设 2024-12-19 阅读0 评论0

phpstudy发送gbk技巧_php的文件上传

这里首先声明一下这一章的内容比较多,比较难,你要抱着和自己去世磕的态度。细微之处不放过,多敲多练是王道。 学习就像爬山,得一步一步...

网站建设 2024-12-19 阅读0 评论0