您的位置:首页 > 数据库 > SQL

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