update 更新/修改记录字段值
格式1:更新表内的所有记录
update 表名
set 字段1=字段1值,字段2=字段2值,字段N=字段N值;
格式2:只更新符合条件表内的部分记录
update 表名
set 字段1=字段1值,字段2=字段2值,字段N=字段N值
where 条件;
注意:
字段值要与字段类型相匹配
对于字符类型的字段,要用双引号引起来
若不使用where限定条件,会更新所有记录
限定条件时,只更新匹配条件的记录
mysql> select password,comment from user;
+----------+-----------------------------------------------------------------+
| password | comment |
+----------+-----------------------------------------------------------------+
| x | root |
| x | bin |
... ...
mysql> update user
-> set
-> password="A",comment="student";
mysql> select password,comment from user;
+----------+---------+
| password | comment |
+----------+---------+
| A | student |
| A | student |
... ...
mysql> select * from user where id=1;
+----+------+----------+------+------+---------+---------+-----------+
| id | name | password | uid | gid | comment | homedir | shell |
+----+------+----------+------+------+---------+---------+-----------+
| 1 | root | A | 0 | 0 | student | /root | /bin/bash |
+----+------+----------+------+------+---------+---------+-----------+
mysql> update user
-> set password="x",comment="root"
-> where id=1;
mysql> select * from user where id=1;
+----+------+----------+------+------+---------+---------+-----------+
| id | name | password | uid | gid | comment | homedir | shell |
+----+------+----------+------+------+---------+---------+-----------+
| 1 | root | x | 0 | 0 | root | /root | /bin/bash |
+----+------+----------+------+------+---------+---------+-----------+
mysql> select * from user where shell is null;
+----+------+----------+------+------+---------+---------+-------+
| id | name | password | uid | gid | comment | homedir | shell |
+----+------+----------+------+------+---------+---------+-------+
| 43 | tom | A | 4001 | 4002 | student | NULL | NULL |
+----+------+----------+------+------+---------+---------+-------+