以下是 UPDATE 语句修改数据的通用 SQL 语法:

UPDATE table_name
SET column1 = value1, column2 = value2...., columnN = valueN
WHERE [condition];
我们可以同时更新一个或者多个字段。
  • 我们可以在 WHERE 子句中指定任何条件。
  • 创建 COMPANY 表( 下载 COMPANY SQL 文件 ),数据内容如下:

    runoobdb# select * from COMPANY;
     id | name  | age | address   | salary
    ----+-------+-----+-----------+--------
      1 | Paul  |  32 | California|  20000
      2 | Allen |  25 | Texas     |  15000
      3 | Teddy |  23 | Norway    |  20000
      4 | Mark  |  25 | Rich-Mond |  65000
      5 | David |  27 | Texas     |  85000
      6 | Kim   |  22 | South-Hall|  45000
      7 | James |  24 | Houston   |  10000
    (7 rows)

    以下实例将更新 COMPANY 表中 id 为 3 的 salary 字段值:

    runoobdb=# UPDATE COMPANY SET SALARY = 15000 WHERE ID = 3;

    得到结果如下:

    id | name  | age | address    | salary
    ----+-------+-----+------------+--------
      1 | Paul  |  32 | California |  20000
      2 | Allen |  25 | Texas      |  15000
      4 | Mark  |  25 | Rich-Mond  |  65000
      5 | David |  27 | Texas      |  85000
      6 | Kim   |  22 | South-Hall |  45000
      7 | James |  24 | Houston    |  10000
      3 | Teddy |  23 | Norway     |  15000

    从结果上看,COMPANY 表中的 id 为 3 的 salary 字段值已被修改。

    以下实例将同时更新 salary 字段和 address 字段的值:

    runoobdb=# UPDATE COMPANY SET ADDRESS = 'Texas', SALARY=20000;

    得到结果如下:

    id | name  | age | address | salary
    ----+-------+-----+---------+--------
      1 | Paul  |  32 | Texas   |  20000
      2 | Allen |  25 | Texas   |  20000
      4 | Mark  |  25 | Texas   |  20000
      5 | David |  27 | Texas   |  20000
      6 | Kim   |  22 | Texas   |  20000
      7 | James |  24 | Texas   |  20000
      3 | Teddy |  23 | Texas   |  20000
    (7 rows)