mysql利用參數(shù)sql_safe_updates限制update/delete范圍詳解
前言
大家應(yīng)該都知道,我們在mysql運維中出現(xiàn)過不少因為update/delete條件錯誤導(dǎo)致數(shù)據(jù)被誤更新或者刪除的case,為避免類似問題的發(fā)生,可以用sql_safe_updates參數(shù)來對update/delete做限制。這個參數(shù)設(shè)置為on后,可防止因程序bug或者DBA手工誤操作導(dǎo)致的整個表被更新或者刪除的情況。下面話不多說了,來一起看看詳細(xì)的介紹吧。
設(shè)置這個參數(shù)時需要注意一下幾點:
a、設(shè)置前需要確認(rèn)程序中所有的update和delete都符合sql_safe_updates的限制規(guī)范,不然程序會報錯。
b、5.0,5.1都是session級別的,5.6是global&session級別;低版本的數(shù)據(jù)庫只能在程序創(chuàng)建session時設(shè)置帶上set sql_safe_updates=on;
高版本的數(shù)據(jù)庫可以直接set global set sql_safe_updates=on
,設(shè)置完成后讓程序重連后生效。
限制規(guī)范:
示例表結(jié)構(gòu):
CREATE TABLE `delay_monitor` ( `id` int(11) NOT NULL, `Ftime` datetime DEFAULT NULL, `Fgtid` varchar(128) NOT NULL DEFAULT '', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin
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、執(zhí)行條件:帶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;
帶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的限制會更為嚴(yán)格;where條件為常量或者為空,將不予執(zhí)行。
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、執(zhí)行條件:帶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;
總結(jié)如下表: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 |
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
相關(guān)文章
Mysql將查詢結(jié)果集轉(zhuǎn)換為JSON數(shù)據(jù)的實例代碼
這篇文章主要介紹了Mysql將查詢結(jié)果集轉(zhuǎn)換為JSON數(shù)據(jù)的實例代碼,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-03-03mysql如果數(shù)據(jù)不存在,則插入新數(shù)據(jù),否則更新的實現(xiàn)方法
mysql如果數(shù)據(jù)不存在,則插入新數(shù)據(jù),否則更新的實現(xiàn)方法2011-11-11mysql 數(shù)據(jù)插入優(yōu)化方法之concurrent_insert
在MyISAM里讀寫操作是串行的,但當(dāng)對同一個表進行查詢和插入操作時,為了降低鎖競爭的頻率,根據(jù)concurrent_insert的設(shè)置,MyISAM是可以并行處理查詢和插入的2021-07-07sqoop export導(dǎo)出 map100% reduce0% 卡住的多種原因及解決
這篇文章主要介紹了sqoop export導(dǎo)出 map100% reduce0% 卡住的多種原因及解決,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01