国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數據庫 > MySQL > 正文

MySQL中參數sql_safe_updates在生產環境的使用詳解

2024-07-24 12:50:51
字體:
來源:轉載
供稿:網友

前言

在應用 BUG或者 DBA誤操作的情況下,會發生對全表進行更新:update delete 的情況。MySQL提供 sql_safe_updates 來限制次操作。

set sql_safe_updates = 1;

設置之后,會限制update delete 中不帶 where 條件的SQL 執行,較嚴格。會對已有線上環境帶來不利影響。對新系統、應用做嚴格審核,可以確保不會發生全表更新的問題。

CREATE TABLE working.test01 (id INT NOT NULL AUTO_INCREMENT,NAME VARCHAR(20),age INT,gmt_created DATETIME,PRIMARY KEY(id)); insert into test01(name,age,gmt_created) values('xiaowang',2,now()); insert into test01(name,age,gmt_created) values('huahua',5,now()); insert into test01(name,age,gmt_created) values('gougou',9,now()); insert into test01(name,age,gmt_created) values('heihei',12,now()); insert into test01(name,age,gmt_created) values('baibai',134,now()); # 過濾字段上沒有索引updateupdate test01 set name = 'xiaoxiao' where age = 2 ;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 test01 set name = 'xiaoxiao';ERROR 1175 (HY000): You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column# 加入limit的更新update test01 set name = 'xia' limit 1;Query OK, 1 row affected (0.00 sec)Rows matched: 1 Changed: 1 Warnings: 0# 新增索引create index idx_age on test01(age);update test01 set name = 'xiaoxiao' where age = 2;Query OK, 1 row affected (0.01 sec)Rows matched: 1 Changed: 1 Warnings: 0update test01 set name = 'hhh' where age = 9 limit 10;Query OK, 1 row affected (0.00 sec)Rows matched: 1 Changed: 1 Warnings: 0alter table test01 drop index idx_age;create index idx_age_name on test01(age,name);update test01 set age= 100 where name = 'hhh';ERROR 1175 (HY000): You are using safe update mode and you tried to update a table without a WHERE that uses a KEY columnupdate test01 set age= 100 where name = 'hhh' limit 10;Query OK, 1 row affected (0.00 sec)Rows matched: 1 Changed: 1 Warnings: 0

由此,update 時,在沒有 where 條件或者where 后不是索引字段時,必須使用 limit ;在有 where 條件時,為索引字段

最近在工作中又發現了一個問題,mysql sql_safe_updates 不支持子查詢的更新。

考慮到開發人員有時候不小心誤更新數據,要求線上庫的 MySQL 實例都設置 sql_safe_updates=1 來避免沒有索引的 update、delete。

結果有一天開發發現下面的一個SQL 沒法正確執行:

update t1 set col2=1 where key1 in (select col2 from t2 where key2='ABcD');

錯誤如下:

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

也就是說沒法對沒有走到索引的where條件進行更新。搜索了下發現,的確不行。及時 key1 和key2 分別是 t1、t2 的索引[我換成主鍵都不行] 。說明是不支持子查詢的update。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 蒙自县| 和顺县| 六安市| 无锡市| 甘洛县| 黎城县| 抚远县| 安阳市| 凤冈县| 丰都县| 海林市| 蚌埠市| 涡阳县| 蒲城县| 太和县| 惠东县| 米泉市| 临高县| 沙湾县| 崇礼县| 高雄县| 台江县| 浏阳市| 贞丰县| 宣化县| 马关县| 彩票| 锦屏县| 华安县| 卢氏县| 尉犁县| 嘉峪关市| 海门市| 隆安县| 郑州市| 宝坻区| 永州市| 苗栗县| 浪卡子县| 锦屏县| 奇台县|