什么是安全模式
在mysql中纹坐,如果在update和delete沒有加上where條件,數(shù)據(jù)將會全部修改吠裆。不只是初識mysql的開發(fā)者會遇到這個問題涂炎,工作有一定經(jīng)驗(yàn)的工程師難免也會忘記寫入where條件。為了避免失誤造成的數(shù)據(jù)全部修改和刪除赂蕴,可開啟mysql的安全模式柳弄。
安全模式的開啟與關(guān)閉
連接到數(shù)據(jù)庫后,查看當(dāng)前mysql的安全模式的狀態(tài)
mysql> show variables like 'sql_safe_updates';
+------------------+-------+
| Variable_name | Value |
+------------------+-------+
| sql_safe_updates | ON |
+------------------+-------+
1 row in set (0.00 sec)
上面查詢命令實(shí)例表示當(dāng)前mysql處于安全模式打開的狀態(tài)概说。
set sql_safe_updates=1; //安全模式打開狀態(tài)
set sql_safe_updates=0; //安全模式關(guān)閉狀態(tài)
在update操作中:當(dāng)where條件中列(column)沒有索引可用且無limit限制時會拒絕更新碧注。where條件為常量且無limit限制時會拒絕更新。
在delete操作中: 當(dāng)①where條件為常量糖赔,②或where條件為空萍丐,③或where條件中 列(column)沒有索引可用且無limit限制時拒絕刪除。
安全模式UPDATE操作實(shí)例
1挂捻、無where條件的update
mysql> update users set status=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
操作失敗碉纺,提示需要使用where條件作為主鍵。
2刻撒、無where條件但是有l(wèi)imit的update
mysql> update users set status=1 limit 1;
Query OK, 0 rows affected (0.00 sec)
Rows matched: 1 Changed: 0 Warnings: 0
操作成功骨田,雖然沒有where條件,但是加入了limit限制声怔。
3态贤、使用非索引字段作為條件進(jìn)行更新
mysql> update users set status=1 where reg_time>'2018-01-01 00:00:00';
ERROR 1175 (HY000): You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column
操作失敗,因?yàn)闂l件字段不是索引字段并且沒有加入limit限制醋火。
4悠汽、使用非索引字段作為條件并且加入limit進(jìn)行更新
mysql> update users set status=1 where reg_time>'2018-01-01 00:00:00' limit 10;
Query OK, 0 rows affected (0.01 sec)
Rows matched: 10 Changed: 0 Warnings: 0
操作成功,雖然條件字段不是索引字段芥驳,但是有加入limit限制柿冲,所以執(zhí)行成功。
5兆旬、使用索引字段作為條件并且不加入limit進(jìn)行更新
mysql> update users set status=1 where phone_num='13800138000';
Query OK, 0 rows affected (0.00 sec)
Rows matched: 1 Changed: 0 Warnings: 0
操作成功假抄,雖然沒有加入limit限制,但是條件字段為索引字段,所以執(zhí)行成功宿饱。
安全模式DELETE操作實(shí)例
1熏瞄、無where條件的delete
mysql> delete from users;
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條件谬以,直接不行强饮。
2、非索引鍵進(jìn)行條件刪除
mysql> delete from users where reg_time='2018-06-28 17:35:37';
ERROR 1175 (HY000): You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column
操作失敗为黎,因?yàn)閞eg_time字段不是索引鍵邮丰。
3、非索引鍵作為條件并且加入limit進(jìn)行刪除
mysql> delete from users where reg_time='2018-06-28 17:35:37' limit 1;
Query OK, 1 row affected (0.00 sec)
操作成功碍舍,即使不是索引鍵柠座,因?yàn)榧尤肓薼imit邑雅。
4片橡、使用索引鍵并且不加limit限制進(jìn)行刪除。
mysql> delete from users where user_id=100000;
Query OK, 1 row affected (0.01 sec)
操作成功淮野,因?yàn)閡ser_id是索引鍵捧书。
5、加入limit但是不使用where條件刪除骤星。
mysql> delete from users limit 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
操作失敗经瓷,因?yàn)闆]有加入where檢索條件。
總結(jié)
如果設(shè)置了sql_safe_updates=1洞难,那么update語句必須滿足如下條件之一才能執(zhí)行成功
1)使用where子句,并且where子句中列必須為prefix索引列
2)使用limit
3)同時使用where子句和limit(此時where子句中列可以不是索引列)
delete語句必須滿足如下條件之一才能執(zhí)行成功
1)使用where子句,并且where子句中列必須為prefix索引列
2)同時使用where子句和limit(此時where子句中列可以不是索引列)