MySQL防止无where条件update操作

   生产环境中,DBA的粗心造成的数据错误最危险,为了防止无where条件的update语句出现错误,可使用如下方法。
1、加-U登录数据库

点击(此处)折叠或打开

  1. [root@master ~]# mysql -uroot -pmysql -U
  2. mysql: [Warning] Using a password on the command line interface can be insecure.
  3. Welcome to the MySQL monitor. Commands end with ; or \g.
  4. Your MySQL connection id is 12
  5. Server version: 5.7.19-log Source distribution
  6. Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
  7. Oracle is a registered trademark of Oracle Corporation and/or its
  8. affiliates. Other names may be trademarks of their respective
  9. owners.
  10. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  11. mysql>
  12. mysql> use test;
  13. Reading table information for completion of table and column names
  14. You can turn off this feature to get a quicker startup with -A
  15. Database changed
  16. mysql> show tables;
  17. +----------------+
  18. | Tables_in_test |
  19. +----------------+
  20. | t1 |
  21. +----------------+
  22. 1 row in set (0.00 sec)
  23. mysql> show create table t1;
  24. +-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  25. | Table | Create Table |
  26. +-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  27. | t1 | CREATE TABLE `t1` (
  28.   `admin_id` int(11) NOT NULL DEFAULT '0',
  29.   `admin_name` varchar(200) NOT NULL,
  30.   `password` varchar(200) NOT NULL,
  31.   `role` int(11) NOT NULL DEFAULT '1',
  32.   `last_login_time` datetime DEFAULT NULL,
  33.   `last_login_ip` varchar(16) DEFAULT '',
  34.   `admin_status` int(11) NOT NULL DEFAULT '1',
  35.   `create_time` datetime DEFAULT NULL
  36. ) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
  37. +-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  38. 1 row in set (0.00 sec)
  39. mysql> update t1 set admin_status=1234;
  40. 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条件的update操作。2、根据这样的特性,对系统环境变量进行更改
vi /etc/profile
alias mysql='mysql -U'
source /etc/profile
3、正常命令登录MySQL,进行测试

点击(此处)折叠或打开

  1. [root@master ~]# mysql -uroot -pmysql
  2. mysql: [Warning] Using a password on the command line interface can be insecure.
  3. Welcome to the MySQL monitor. Commands end with ; or \g.
  4. Your MySQL connection id is 14
  5. Server version: 5.7.19-log Source distribution
  6. Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
  7. Oracle is a registered trademark of Oracle Corporation and/or its
  8. affiliates. Other names may be trademarks of their respective
  9. owners.
  10. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  11. mysql> use test;
  12. Reading table information for completion of table and column names
  13. You can turn off this feature to get a quicker startup with -A
  14. Database changed
  15. mysql> update t1 set admin_status=1234;
  16. ERROR 1175 (HY000): You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值