DEV Community

Malik M
Malik M

Posted on

UPDATE statement in PostgreSQL

In this tutorial, We will be exploring how to update the data in an existing table.
Let's get started...

The UPDATE statement is used to modify the data in an existing table.

Syntax

UPDATE table_name
SET column1 = value1,
    column2 = value2,
    ...
WHERE condition;
Enter fullscreen mode Exit fullscreen mode

In the above syntax, we can see that first we have specify the table name with the UPDATE keyword.
Second, we have to specify the columns that we want to update and their updated values after 'SET' keyword. Also note that those columns that do not appear in the 'SET' clause won't be modified.
Third, we have to write the conditions which rows to be updated in the table after the 'WHERE' clause.

If you want to return the row that is/are just modified use the following code in the end:

RETURNING *;
Enter fullscreen mode Exit fullscreen mode

EXAMPLE
Let's look at the example and practice it.
Suppose we have the following table which shows all the courses and description:

Image description

1) Update one row from table
The following query will modify one row from the table that has id=2 and sets it published_date to 2020-07-01.

UPDATE courses
SET published_date = '2020-07-01'
WHERE course_id = 2
RETURNING *;
Enter fullscreen mode Exit fullscreen mode

OUTPUT

Image description

Conclusion

In this article we learnt about the 'UPDATE' statement and it's example.

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

Heroku

This site is powered by Heroku

Heroku was created by developers, for developers. Get started today and find out why Heroku has been the platform of choice for brands like DEV for over a decade.

Sign Up

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay