更新、修改

MySQL从小白到总裁完整教程目录:https://blog.csdn.net/weixin_67859959/article/details/129334507?spm=1001.2014.3001.5502

语法:

update 表名 列名=该列新值, 列名=该列新值, ... where 记录匹配条件;

说明:update  更新、修改
             set     设置
             如果不写where条件,表示对所有行都操作!

比如:更新、修改_第1张图片

案例:更新test03表,针对日期(etime)是2022-06-01的记录,将姓名(name)改为mary

update test03 set name='Mary' where etime='2022-06-01';

 结果:

mysql> select * from test03;
+------+------+--------+------------+---------+
| name | age  | salary | etime      | address |
+------+------+--------+------------+---------+
| king |   20 | 999.00 | NULL       | NULL    |
| tom  |   23 |   NULL | 2022-06-30 | NULL    |
| rose | NULL |   NULL | NULL       | 北京    |
| NULL |   19 | 777.00 | 2022-06-01 | NULL    |
| lucy |   21 |  88.00 | NULL       | 上海    |
+------+------+--------+------------+---------+
5 rows in set (0.05 sec)

mysql> update test03 set name='Mary' where etime='2022-06-01';
Query OK, 1 row affected (0.04 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from test03;
+------+------+--------+------------+---------+
| name | age  | salary | etime      | address |
+------+------+--------+------------+---------+
| king |   20 | 999.00 | NULL       | NULL    |
| tom  |   23 |   NULL | 2022-06-30 | NULL    |
| rose | NULL |   NULL | NULL       | 北京    |
| Mary |   19 | 777.00 | 2022-06-01 | NULL    |
| lucy |   21 |  88.00 | NULL       | 上海    |
+------+------+--------+------------+---------+
5 rows in set (0.00 sec)

更新、修改_第2张图片

 

练习:更新test03表,针对姓名(name)是rose的记录,将年龄(age)改为108岁

mysql> select * from test03;
+------+------+--------+------------+---------+
| name | age  | salary | etime      | address |
+------+------+--------+------------+---------+
| king |   20 | 999.00 | NULL       | NULL    |
| tom  |   23 |   NULL | 2022-06-30 | NULL    |
| rose | NULL |   NULL | NULL       | 北京    |
| Mary |   19 | 777.00 | 2022-06-01 | NULL    |
| lucy |   21 |  88.00 | NULL       | 上海    |
+------+------+--------+------------+---------+
5 rows in set (0.00 sec)

mysql> update test03 set age=108 where name='rose';
Query OK, 1 row affected (0.02 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from test03;
+------+------+--------+------------+---------+
| name | age  | salary | etime      | address |
+------+------+--------+------------+---------+
| king |   20 | 999.00 | NULL       | NULL    |
| tom  |   23 |   NULL | 2022-06-30 | NULL    |
| rose |  108 |   NULL | NULL       | 北京    |
| Mary |   19 | 777.00 | 2022-06-01 | NULL    |
| lucy |   21 |  88.00 | NULL       | 上海    |
+------+------+--------+------------+---------+
5 rows in set (0.00 sec)

更新、修改_第3张图片

练习:更新test03表,针对日期(etime)是2022-06-30的记录,将工资(salary)修改为999,地址(address)变更为广州  

mysql> select * from test03;
+------+------+--------+------------+---------+
| name | age  | salary | etime      | address |
+------+------+--------+------------+---------+
| king |   20 | 999.00 | NULL       | NULL    |
| tom  |   23 |   NULL | 2022-06-30 | NULL    |
| rose |  108 |   NULL | NULL       | 北京    |
| Mary |   19 | 777.00 | 2022-06-01 | NULL    |
| lucy |   21 |  88.00 | NULL       | 上海    |
+------+------+--------+------------+---------+
5 rows in set (0.00 sec)

mysql> update test03 set salary=999, address='广州'
    -> where etime='2022-06-30';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql>
mysql> #查询验证
mysql> select * from test03;
+------+------+--------+------------+---------+
| name | age  | salary | etime      | address |
+------+------+--------+------------+---------+
| king |   20 | 999.00 | NULL       | NULL    |
| tom  |   23 | 999.00 | 2022-06-30 | 广州    |
| rose |  108 |   NULL | NULL       | 北京    |
| Mary |   19 | 777.00 | 2022-06-01 | NULL    |
| lucy |   21 |  88.00 | NULL       | 上海    |
+------+------+--------+------------+---------+
5 rows in set (0.00 sec)

案例:针对test03表,将所有记录的日期(etime)都改为2022-01-01

分析:更新语句中,不写where子句,表示对所有的行的指定列进行修改!

mysql> update test03 set etime='2022-01-01';
Query OK, 5 rows affected (0.03 sec)
Rows matched: 5  Changed: 5  Warnings: 0

mysql> select * from test03;
+------+------+--------+------------+---------+
| name | age  | salary | etime      | address |
+------+------+--------+------------+---------+
| king |   20 | 999.00 | 2022-01-01 | NULL    |
| tom  |   23 | 999.00 | 2022-01-01 | 广州    |
| rose |  108 |   NULL | 2022-01-01 | 北京    |
| Mary |   19 | 777.00 | 2022-01-01 | NULL    |
| lucy |   21 |  88.00 | 2022-01-01 | 上海    |
+------+------+--------+------------+---------+
5 rows in set (0.00 sec)

 练习:针对test03表,将地址(address)为北京的记录,工资(salary)改为666

mysql> update test03 set salary=666 where address='北京';
Query OK, 1 row affected (0.02 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from test03;
+------+------+--------+------------+---------+
| name | age  | salary | etime      | address |
+------+------+--------+------------+---------+
| king |   20 | 999.00 | 2022-01-01 | NULL    |
| tom  |   23 | 999.00 | 2022-01-01 | 广州    |
| rose |  108 | 666.00 | 2022-01-01 | 北京    |
| Mary |   19 | 777.00 | 2022-01-01 | NULL    |
| lucy |   21 |  88.00 | 2022-01-01 | 上海    |
+------+------+--------+------------+---------+
5 rows in set (0.00 sec)

 

你可能感兴趣的:(android)