1.语法

  1. UPDATE table_name
  2. SET column1 = value1, column2 = value2...., columnN = valueN
  3. WHERE [condition];

可以同时更新一个或者多个字段;
也可以在 WHERE 子句中指定任何条件,来筛选要更新的对象。

2.示例

2.1更新单个字段数据

更新company表中ID=3的salary字段的值:

runoobdb=# UPDATE company SET salary = 15000 WHERE ID = 3;
UPDATE 1

--查看确认,发现ID=3的 salary值已经被修改为了15000
runoobdb=# select * from company where salary = 15000;
 id | name  | age |                      address                       | salary
----+-------+-----+----------------------------------------------------+--------
  2 | Allen |  25 | Texas                                              |  15000
  3 | Teddy |  23 | Norway                                             |  15000
(2 行记录)

2.2同时更新多个字段数据

runoobdb=# UPDATE company SET address = 'haha',salary = 1;
UPDATE 7

--查看更新结果
runoobdb=# select * from company;
 id | name  | age |                      address                       | salary
----+-------+-----+----------------------------------------------------+--------
  1 | Paul  |  32 | haha                                               |      1
  2 | Allen |  25 | haha                                               |      1
  4 | Mark  |  25 | haha                                               |      1
  5 | David |  27 | haha                                               |      1
  6 | Kim   |  22 | haha                                               |      1
  7 | James |  24 | haha                                               |      1
  3 | Teddy |  23 | haha                                               |      1
(7 行记录)


--指定条件更新多个数据
runoobdb=# UPDATE company SET address = '北京',salary = '100000' WHERE age > 25;
UPDATE 2

runoobdb=# select * from company;
 id | name  | age |                       address                        | salary
----+-------+-----+------------------------------------------------------+--------
  2 | Allen |  25 | haha                                                 |      1
  4 | Mark  |  25 | haha                                                 |      1
  6 | Kim   |  22 | haha                                                 |      1
  7 | James |  24 | haha                                                 |      1
  3 | Teddy |  23 | haha                                                 |      1
  1 | Paul  |  32 | 北京                                                 | 100000
  5 | David |  27 | 北京                                                 | 100000
(7 行记录)