Postgresql 简明教程
PostgreSQL - UPDATE Query
PostgreSQL UPDATE 查询用于修改表中的现有记录。您可以将 WHERE 子句与 UPDATE 查询一起使用以更新选定的行。否则,所有行都将被更新。
The PostgreSQL UPDATE Query is used to modify the existing records in a table. You can use WHERE clause with UPDATE query to update the selected rows. Otherwise, all the rows would be updated.
Syntax
带 WHERE 子句的 UPDATE 查询的基本语法如下 −
The basic syntax of UPDATE query with WHERE clause is as follows −
UPDATE table_name
SET column1 = value1, column2 = value2...., columnN = valueN
WHERE [condition];
可以使用 AND 或 OR 运算符组合 N 个条件。
You can combine N number of conditions using AND or OR operators.
Example
考虑表 COMPANY ,具有如下记录 −
Consider the table COMPANY, having records as follows −
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 −
The following is an example, which would update ADDRESS for a customer, whose ID is 6 −
testdb=# UPDATE COMPANY SET SALARY = 15000 WHERE ID = 3;
现在,COMPANY 表将具有以下记录 −
Now, COMPANY table would have the following records −
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 查询如下所示 −
If you want to modify all ADDRESS and SALARY column values in COMPANY table, you do not need to use WHERE clause and UPDATE query would be as follows −
testdb=# UPDATE COMPANY SET ADDRESS = 'Texas', SALARY=20000;
现在,COMPANY 表将包含以下记录 −
Now, COMPANY table will have the following records −
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)