How to Update Multiple Columns in PostgreSQL

In PostgreSQL, users can update one or more columns of the table simultaneously using the update query. It's simple to update a single column, but updating multiple columns can be a bit more challenging. However, there's no need to worry because using the UPDATE command with a simple comma-separated syntax can update a bulk of columns in a single operation.

This post illustrates a comprehensive guide on updating multiple columns of a Postgres table.

How to Update Multiple Columns in PostgreSQL?

Use the comma-separated syntax for the UPDATE statement along with the SET clause to update multiple columns of a Postgres table:

UPDATE table_name
SET col_1 = val_1, col_2 = val_2, col_3 = val_3, ...
WHERE update_condition;

Here in this syntax:

- The table_name represents a table that needs to be updated/modified.

- In the SET clause, specify the columns to be updated, along with their corresponding modified values, separated by commas.

- Utilize the WHERE clause to specify update criteria.

Example: Updating Multiple Columns in Postgres

A sample table named "emp_bio" has already been created with the following details:

SELECT * FROM emp_bio;
img

Suppose we need to modify the “emp_sal” and “joining_date” columns of the emp_bio table. For this purpose, we will utilize the UPDATE command as follows:

UPDATE emp_bio
SET emp_sal = 55000, joining_date = '2019-08-01'
WHERE e_id >= 5;

The following screenshot shows that three records have been updated in the “emp_bio” table:

img

Execute the following query to view the updated records from the "emp_bio" table:

SELECT * FROM emp_bio;

The output snippet describes that the selected columns have been successfully updated:

img

This is how a user can update multiple columns of a table by executing a single “UPDATE” query.

Conclusion

To update multiple columns of a PostgreSQL table, use the comma-separated syntax within the UPDATE statement, combined with the SET clause. In the SET clause, specify the columns to be updated, along with their corresponding modified values, separated by commas. This write-up has illustrated the usage of the Postgres UPDATE query for updating multiple columns.