Mysql DBA 高级运维学习笔记-DML之修改表中的数据实战
2018-01-27 17:03
1431 查看
9.10 修改表中的数据
9.10.1 修改表中指定条件固定列的数据
1.命令语法:
update 表名 set 字段=新值,….where 条件(一定要注意条件)2.修改指定的行字段的内容
a.查看要修改的表system@ceshi 02:3907->select * from test; +----+-----------+ | id | name | +----+-----------+ | 1 | wwnwan| | 2 | zbf | | 3 | lisi | | 4 | woshishei | | 5 | nimei | +----+-----------+
b.修改id为3的行的名字为tiejun
system@ceshi 02:5335->update test set name='tiejun' where id=3; Query OK, 1 row affected (0.10 sec) Rows matched: 1 Changed: 1 Warnings: 0 system@ceshi 02:5348->select * from test; +----+-----------+ | id | name | +----+-----------+ | 1 | wwnwan| | 2 | zbf | | 3 | tiejun| | 4 | woshishei | | 5 | nimei | +----+-----------+
9.10.2 修改表中所有行的数据
严重案列(可能误操作导致数据对视)a.不带条件更改所有表的数据
system@ceshi 02:5352->update test set name='tiejun';
不加条件要十分小心,专业做法,一定要问开发确认,如果你发给开发的语句要括号注明,防止DBA误会。
b.更改了所有数据
system@ceshi 03:0109->select * from test; +----+--------+ | id | name | +----+--------+ | 1 | tiejun | | 2 | tiejun | | 3 | tiejun | | 4 | tiejun | | 5 | tiejun | +----+--------+
c.用备份的数据恢复
之前已经备份过了,这里就不重新备份了
[root@localhost ~]# mysql -usystem -pzbf666 wwn < /opt/zbf_bak.sql
这里的wwn是数据库不是表。
system@ceshi 03:1044->select * from test; +----+-----------+ | id | name | +----+-----------+ | 1 | wwnwan| | 2 | zbf | | 3 | lisi | | 4 | woshishei | | 5 | nimei | +----+-----------+
3.下面是老男孩老师防止误操作MySQL数据库案列
http://blog.51cto.com/oldboy/1321061怎么防止delete ,update误操作呢?
第一种方法:带参数-U登录mysql
(1)查看mysql帮助
[root@mysql ~]# mysql --help|grep dummy -U, --i-am-a-dummy Synonym for option --safe-updates, -U. i-am-a-dummy FALSE
在mysql命令上加上U,当发出没有where和limit关键字的update或delete时,mysql拒绝执行。
(2)指定-U登录mysql
[root@mysql ~]# mysql -uroot -p123456 -U Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 15 Server version: 5.5.32-log Source distribution Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> use zbf Database changed mysql> select * from student; +----+-----------+-----+--------+ | id | name | age | dept | +----+-----------+-----+--------+ | 1 | zbf666| 29 | linux | | 2 | lisi | 28 | mysql | | 3 | zhangsan | 21 | python | | 4 | woshishei | 24 | java | +----+-----------+-----+--------+ 4 rows in set (0.00 sec) mysql> update test set name='liushishi'; ERROR 1146 (42S02): Table 'zbf.test' doesn't exist mysql> update student set name='liushishi'; ERROR 1175 (HY000): You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column
第二种方法:指定别名
(1)指定别名,完了正常登录就可以了
[root@mysql ~]# alias mysql='mysql -U' [root@mysql ~]# mysql -uroot -p123456 Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 16 Server version: 5.5.32-log Source distribution Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> use zbf Database changed mysql> update student set name='liushishi'; ERROR 1175 (HY000): You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column
(2)让别名永久生效
[root@mysql ~]# vim /etc/profile ....省略.... export PATH=/usr/local/mysql/bin:$PATH alias mysql='mysql -U' [root@mysql ~]# sh /etc/profile [root@mysql ~]# mysql -uroot -p123456 Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 19 Server version: 5.5.32-log Source distribution Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> use zbf Database changed mysql> update student set name='liushishi'; ERROR 1175 (HY000): You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column
相关文章推荐
- Mysql DBA 高级运维学习笔记-Mysql增量备份及分库分表备份数据恢复实战
- Mysql DBA 高级运维学习笔记-MySQL备份与恢复实战案例及生产方案
- Mysql DBA 高级运维学习笔记-Mysql主从复制原理及实战部署
- Mysql DBA 高级运维学习笔记-DML语句之insert知识讲解
- Mysql DBA 高级运维学习笔记-增删表字段更改表名删除表实战
- Mysql DBA 高级运维学习笔记-MySQL数据库多实例介绍
- Mysql DBA 高级运维学习笔记-MySQL主从复制指定不同库表参数说明
- Mysql DBA 高级运维学习笔记-Mysql常见多实例配置方案及多实例安装
- Mysql DBA 高级运维学习笔记-Mysql数据库字符集知识
- Mysql DBA 高级运维学习笔记-Heartbeat实现web服务的高可用案例及维护要点
- Mysql DBA高级运维学习笔记-mysql数据库介绍
- Mysql DBA 高级运维学习笔记-mysql数据库常用管理应用
- Mysql DBA 高级运维学习笔记-一主多从宕机从库切换主库继续和从库同步过程
- Mysql DBA 高级运维学习笔记-DQL语句之select知识讲解
- Mysql DBA 高级运维学习笔记-创建Mysql用户及授权的多种方法实战
- Mysql DBA 高级运维学习笔记-MySQL主从复制故障解决
- Mysql DBA 高级运维学习笔记-Mysql双主及多主同步过程
- Mysql DBA 高级运维学习笔记-sql语言介绍及分类
- Mysql DBA高级运维学习笔记-Mysql数据库的多种安装方法
- Mysql DBA 高级运维学习笔记-heartbeat介绍