update multiple columns in postgresql

How to Update Multiple Columns in PostgreSQL

Sometimes you may need to change multiple column values in PostgreSQL. You can modify multiple column values using a single UPDATE statement. In this article, we will look at how to update multiple columns in PostgreSQL.


How to Update Multiple Columns in PostgreSQL

It is very easy to update multiple columns in PostgreSQL. Here is the syntax to update multiple columns in PostgreSQL.

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

In the above SQL statement, you need to specify table name, and mention column names and their new values as a comma-separated list, and optionally mention a WHERE condition to update only certain rows in your table.

It returns the number of rows updated as shown below where count is the number of rows updated.

UPDATE count

Also read : How to Compare Arrays in PostgreSQL

For example, let us say you have the following table.

postgres=# create table sales(id int, 
           order_date date, 
           amount int);

postgres=# insert into sales(id, order_date, amount) 
           values(1, '2020-12-01', 100),
                 (2,'2020-12-02',250),
                 (3,'2020-12-03',300);

postgres=# select * from sales;
id | order_date | amount
----+------------+--------
1 | 2020-12-01 | 100
2 | 2020-12-02 | 250
3 | 2020-12-03 | 300

Also read : How to Change User Password in PostgreSQL

Here is the SQL query to update order_date and amount columns in sales table where id=3.

postgres=# update sales 
           set order_date='2020-12-04', 
               amount=250 where id=3;

postgres=# select * from sales;
id | order_date | amount
----+------------+--------
1 | 2020-12-01 | 100
2 | 2020-12-02 | 250
3 | 2020-12-04 | 250
UPDATE 1

As you can see it is very easy to update multiple columns in PostgreSQL.

Need a reporting tool for PostgreSQL? Ubiq makes it easy to visualize data in minutes, and monitor in real-time dashboards. Try it Today!

mm

About Ubiq

Ubiq is a powerful dashboard & reporting platform. Build dashboards, charts & reports for your business in minutes. Try it for free!