PostgreSQL UPDATE
PostgreSQL UPDATE query is used to update column values of a table. You can apply WHERE condition to apply UPDATE only on those values that satisfy the condition in WHERE clause.
The syntax of UPDATE query is:
UPDATE table_name
SET column1 = value1, column2 = value2, columnN = valueN
WHERE condition;
where value of column1 is set to value1, value of column2 is set to vaule2, and so on as defined in the SET clause.
PostgreSQL UPDATE Single Column for a Single Row
To update a single row, you have to provide a condition in WHERE clause where only one row is selected. In this case, the condition is usually based on the column which is unique.
Consider the following students
table.
Now, we will update only a single column, attendance
where id = 3
. The complete update query would be:
UPDATE students
SET attendance=81
WHERE id=3;
PostgreSQL UPDATE Single Column for All Rows
You can also update a column value for all the rows. Just don’t provide WHERE condition. This selects all the rows for the update operation.
UPDATE students
SET attendance=73;
PostgreSQL UPDATE Multiple Columns
You can update multiple columns as specified in the syntax given at the starting of this tutorial.
We will update age and attendance where id = 3
.
UPDATE students
SET age=21, attendance=73
WHERE id=3;
Conclusion
In this PostgreSQL Tutorial, we used UPDATE query on PostgreSQL Table, to update column values of one or more rows.