PostgreSQL – How to Update Multiple Columns

To update multiple columns in a PostgreSQL table, you can use the UPDATE statement. Below is the basic syntax for updating multiple columns:

UPDATE table_name SET column1 = value1, column2 = value2, ... WHERE condition;

where

  • table_name is the name of the table you want to update.
  • column1, column2, etc.: are the names of the columns you want to update.
  • value1, value2, etc.: are the new values you want to set for each column.
  • WHERE condition: is an optional condition that specifies which rows to update. If you omit the WHERE clause, all rows in the table will be updated.

Here’s an example of how to update multiple columns in a PostgreSQL table:

Suppose you have a table named “employees” with the following columns: “first_name,” “last_name,” and “salary,” and you want to update the salary and last name for employees with a certain condition (e.g., employees with a specific ID). You can do it like this:

UPDATE employees SET last_name = 'NewLastName', salary = 50000 WHERE employee_id = 123;

In this example, we are setting the “last_name” to “NewLastName” and the “salary” to 50000 for the employee with an ID of 123. You can adjust the WHERE clause to match the specific condition you need for your update.

Make sure to use the appropriate data types and values in your SET clause, and be cautious when using the UPDATE statement, especially in production databases, as it can modify data irreversibly. Always test your update statements thoroughly and consider taking a backup before making significant changes to your data.

Similar Posts