PostgreSQL – UPDATE
Last Updated :
24 Jul, 2024
In PostgreSQL, the UPDATE statement is important for modifying existing data in a table. Let us learn about the syntax and practical examples, enhancing the understanding of the UPDATE Statement in PostgreSQL.
Syntax
UPDATE table
SET column1 = value1,
column2 = value2, ...
WHERE condition; Let’s analyze the above syntax:
- Table Specification: Identify the table where the changes are intended.
- SET Clause: List the columns whose values need to be updated.
- WHERE Clause: Define the condition to specify which rows should be updated.
Setting Up a Sample Database and Table
Let’s create a sample database and table to demonstrate the UPDATE statement. Create a database named “company” with the below command:
PostgreSQL
CREATE DATABASE company;
CREATE TABLE employee (
employee_id INT PRIMARY KEY,
first_name VARCHAR (255) NOT NULL,
last_name VARCHAR (255) NOT NULL,
manager_id INT,
FOREIGN KEY (manager_id)
REFERENCES employee (employee_id)
ON DELETE CASCADE
);
INSERT INTO employee (
employee_id,
first_name,
last_name,
manager_id
)
VALUES
(1, 'Sandeep', 'Jain', NULL),
(2, 'Abhishek ', 'Kelenia', 1),
(3, 'Harsh', 'Aggarwal', 1),
(4, 'Raju', 'Kumar', 2),
(5, 'Nikhil', 'Aggarwal', 2),
(6, 'Anshul', 'Aggarwal', 2),
(7, 'Virat', 'Kohli', 3),
(8, 'Rohit', 'Sharma', 3);
SELECT * FROM employee;
The value in the ‘manager_id’ column represents the senior manager who the employee reports to. If it’s NULL, he/she doesn’t report to anyone. The overall hierarchy looks like the below image:
Our current tables looks like below:
Now let’s update data in the above mentioned table.
PostgreSQL UPDATE Statement Examples
Let us take a look at some of the examples of the UPDATE Statement in PostgreSQL to better understand the concept.
Example 1: Updating a Single Row
Here we will update the employee “Raju Kumar” name to “Raju Singh” using the UPDATE statement.
Query:
UPDATE employee
SET last_name = 'Singh'
WHERE first_name = 'Raju';
Output:

Explanation: The last name of Raju Kumar is updated to Raju Singh.
Example 2: Updating Multiple Rows
In the above we made an update to a single row, but here we will make changes to multiple rows. Here we will change the last name of everyone to “Gupta” whose last name is “Aggarwal”.
Query:
UPDATE employee
SET last_name = 'Gupta'
WHERE last_name = 'Aggarwal';
Output:
Explanation: All employees with the last name ‘Aggarwal’ will have their last name changed to ‘Gupta’.
Important Points About PostgreSQL UPDATE Statement
- Use the
RETURNING clause to return the updated rows, which can be useful for verification. - Be cautious when updating columns that may contain
NULL values. Use the IS NULL or IS NOT NULL conditions appropriately in the WHERE clause. - Ensure that updates do not violate any constraints such as primary keys, foreign keys, unique constraints, or check constraints. PostgreSQL will throw an error if any constraint is violated.
- Multiple columns can be updated in a single
UPDATE statement.