扫码一下
查看教程更方便
postgresql update 语句用于修改表中的现有记录。可以使用 where 子句和 update 来更新选定的行。否则,所有行都将被更新。
以下是 update 语句修改数据的通用 sql 语法:
update table_name
set column1 = value1, column2 = value2...., columnn = valuen
where [condition];
我们可以同时更新一个或者多个字段,字段之间使用逗号分隔。
创建 company 表( ),数据内容如下:
jiyik_db=# 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 字段值:
jiyik_db=# 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 字段的值:
jiyik_db=# 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)