PostgreSQL Tutorial

  • Home
  • Stored Procedures
  • Triggers
  • Views
  • Interfaces
    • PostgreSQL PHP
    • PostgreSQL Python
    • PostgreSQL JDBC
  • Functions
    • Aggregate Functions
    • Date / Time Functions
    • String Functions
    • Math Functions
Home / PostgreSQL Tutorial / PostgreSQL UPDATE

PostgreSQL UPDATE

Summary: in this tutorial, you will learn how to use the PostgreSQL UPDATE statement to update existing data in a table.

PostgreSQL UPDATE syntax

To change the values of the columns in a table, you use the UPDATE statement. The following illustrates the syntax of the UPDATE statement:

1
2
3
4
5
UPDATE table
SET column1 = value1,
    column2 = value2 ,...
WHERE
condition;

Let’s examine the syntax of the statement in detail:

First, specify the table name where you want to update the data after UPDATE clause.

Second, list the columns whose values you want to change in the SET clause. If you update values in multiple columns, you use a comma (,) to separate each pair of column and value. The columns that are not on the list retain their original values.

Third, determine which rows you want to update in the condition of the WHERE clause. If you omit the WHERE clause, all the rows in the table are updated.

PostgreSQL UPDATE examples

We will use the link table created in the INSERT tutorial for the demonstration.

Let’s examine the content of the link table:

1
2
3
4
SELECT
*
FROM
link;

PostgreSQL update example

PostgreSQL update table partially example

To change the NULL values of the last_update column to the current date, you use the following statement:

1
2
3
4
UPDATE link
SET last_update = DEFAULT
WHERE
last_update IS NULL;

PostgreSQL update table partially

The WHERE clause filters rows whose values in the last_update column is not NULL. We used the DEFAULT keyword because the last_update column accepts the current date as the default value.

PostgreSQL update all rows in a table

To update the values of the rel column to nofollow for all rows in the link table, you omit the WHERE clause in the UPDATE statement as follows:

1
2
UPDATE link
SET rel = 'nofollow';

PostgreSQL update all rows

You can also update data of a column from another column within the same table. The following statement copies the values of the name column to the description column of the link table:

1
2
UPDATE link
SET description = name;

PostgreSQL update values same table

PostgreSQL update join example

Let’s examine the link_tmp table, which has the same structure as the link table:
link_tmp table

The following statement updates values that come from the link table for the columns in the link_tmp table:

1
2
3
4
5
6
7
8
UPDATE link_tmp
SET rel = link.rel,
description = link.description,
last_update = link.last_update
FROM
link
WHERE
link_tmp.id = link.id;

PostgreSQL UPDATE JOIN

Notice that we used the FROM clause in the UPDATE statement to specify the second table ( link) that involves in the update.

This kind of UPDATE statement sometimes referred to as UPDATE JOIN or UPDATE INNER JOIN because two or more tables are involved in the UPDATE statement. The join condition is specified in the WHERE clause.

PostgreSQL update with returning clause

The UPDATE statement returns the number of affected rows by default. The PostgreSQL UPDATE statement also returns updated entries using the RETURNING clause. This addition is a PostgreSQL’s extension to the SQL standard.

The following statement updates the row with id 1 in the link table and returns the updated entries:

1
2
3
4
5
6
7
8
UPDATE link
SET description = 'Learn PostgreSQL fast and easy',
rel = 'follow'
WHERE
ID = 1
RETURNING id,
description,
rel;

PostgreSQL UPDATE RETURNING

To verify the update, you can select data from the link table as the following query:

1
2
3
4
5
6
SELECT
*
FROM
link
WHERE
ID = 1;

PostgreSQL link table with id 1

In this tutorial, you have learned how to update data from a table by using the PostgreSQL UPDATE statement.

Related Tutorials

  • PostgreSQL Upsert Using INSERT ON CONFLICT statement
Previous Tutorial: PostgreSQL INSERT
Next Tutorial: PostgreSQL UPDATE Join with A Practical Example

PostgreSQL Quick Start

  • What is PostgreSQL?
  • Install PostgreSQL
  • Connect to Database
  • Download PostgreSQL Sample Database
  • Load Sample Database
  • Explore Server and Database Objects

PostgreSQL Fundamentals

  • PostgreSQL Select
  • PostgreSQL Order By
  • PostgreSQL Select Distinct
  • PostgreSQL Where
  • PostgreSQL LIMIT
  • PostgreSQL IN
  • PostgreSQL Between
  • PostgreSQL Like
  • PostgreSQL Inner Join
  • PostgreSQL Left Join
  • PostgreSQL Full Outer Join
  • PostgreSQL Cross Join
  • PostgreSQL Natural Join
  • PostgreSQL Group By
  • PostgreSQL Having
  • PostgreSQL Union
  • PostgreSQL Intersect
  • PostgreSQL Except
  • PostgreSQL Subquery
  • PostgreSQL Insert
  • PostgreSQL Update
  • PostgreSQL Delete

Managing Table Structure

  • PostgreSQL Data Types
  • PostgreSQL Create Table
  • PostgreSQL Alter Table
  • PostgreSQL Drop Table
  • PostgreSQL Truncate Table
  • PostgreSQL CHECK Constraint
  • PostgreSQL Not-Null Constraint
  • PostgreSQL Foreign Key
  • PostgreSQL Primary Key
  • PostgreSQL UNIQUE Constraint

PostgreSQL Views

  • Managing PostgreSQL Views
  • Creating Updatable Views
  • PostgreSQL Materialized Views
  • The WITH CHECK OPTION Views
  • PostgreSQL Recursive View

PostgreSQL Triggers

  • Introduction to Trigger
  • Creating A Trigger
  • Managing PostgreSQL Triggers

About PostgreSQL Tutorial

PostgreSQLTutorial.com is a website dedicated to developers and database administrators who are working on PostgreSQL database management system.

We constantly publish useful PostgreSQL tutorials to keep you up-to-date with the latest PostgreSQL features and technologies. All PostgreSQL tutorials are simple, easy-to-follow and practical.

Recent PostgreSQL Tutorials

  • PostgreSQL ANY Operator
  • PostgreSQL EXISTS
  • How To Delete Duplicate Rows in PostgreSQL
  • PostgreSQL TO_CHAR Function
  • PostgreSQL TO_NUMBER Function
  • PostgreSQL TO_TIMESTAMP Function
  • PostgreSQL CEIL Function
  • PostgreSQL MOD Function
  • PostgreSQL FLOOR Function
  • PostgreSQL ABS Function

More Tutorials

  • PostgreSQL Cheat Sheet
  • PostgreSQL Administration
  • PostgreSQL PHP
  • PostgreSQL Python
  • PostgreSQL JDBC
  • PostgreSQL Resources

Site Info

  • Home
  • About Us
  • Contact Us
  • Privacy Policy

Copyright © 2017 by PostgreSQL Tutorial Website. All Rights Reserved.