PostgreSQL UPDATE
To update the existing records in a table one can use the UI or can use the PostgreSQL UPDATE statement.
Syntax:
UPDATE table_name SET column_1 = expr_1, column_2 = expr_2, ... column_n = expr_n WHERE conditions;
UPDATE statement using UI:
Other than Query tool, we can also UPDATE statement in PostgreSQL using UI. To UPDATE statement using UI in PostgreSQL, follow the below steps.
- Right-click on the selected table.
- Move your cursor over the option scripts.
- Click on the “UPDATE script” option.
- Put the values on the place of “?”.
- Fulfill the WHERE condition.
- Click on the “play” button.
Example 1: Updating a single column of a table.
Employment table before update:
ID | STATE | RATE |
1 | A | 60 |
2 | B | 70 |
3 | C | 65 |
4 | D | 80 |
5 | E | 78 |
UPDATE “EMPLOYMENT” SET “RATE” = 85 WHERE “ID” = 3; |
Explanation:
The RATE will be updated to 85 where ID is 3.
Employment table after update will be like this:
ID | STATE | RATE |
1 | A | 60 |
2 | B | 70 |
3 | C | 85 |
4 | D | 80 |
5 | E | 78 |
Example 2: Updating multiple columns of a table.
Employment table before update:
ID | STATE | RATE |
1 | A | 60 |
2 | B | 70 |
3 | C | 65 |
4 | D | 80 |
5 | E | 78 |
UPDATE “EMPLOYMENT” SET “RATE” = 85, “STATE” = ‘F’ WHERE “ID” = 3; |
Explanation:
The RATE will be updated to 85 and STATE will be updated to ‘F’ where ID is 3.
Employment table after update will be like this:
ID | STATE | RATE |
1 | A | 60 |
2 | B | 70 |
3 | F | 85 |
4 | D | 80 |
5 | E | 78 |