PostgreSQL 更新查询
PostgreSQL UPDATE 查询用于修改表中的现有记录。您可以使用WHERE子句与UPDATE查询一起使用以更新所选行。否则,将更新所有行。
语法
带有WHERE子句的UPDATE查询的基本语法如下:
UPDATE table_name
SET column1 = value1, column2 = value2...., columnN = valueN
WHERE [condition];
您可以使用AND或OR运算符将N个条件结合起来。
示例
考虑下表公司,记录如下:
testdb# 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)
下面是一个示例,将更新ID为6的客户的ADDRESS字段。
testdb=# UPDATE COMPANY SET SALARY = 15000 WHERE ID = 3;
现在,COMPANY表将具有以下记录 –
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
(7 rows)
如果您想修改 COMPANY 表中所有 ADDRESS 和 SALARY 列的值,您不需要使用 WHERE 子句,UPDATE 查询如下所示−
testdb=# UPDATE COMPANY SET ADDRESS = 'Texas', SALARY=20000;
现在,COMPANY表将有以下记录:
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)