How to Update Multiple Rows in PostgreSQL?
Last Updated :
04 Jul, 2024
In PostgreSQL, the UPDATE
statement is a powerful tool used to modify existing records within a table. We appoint two sorts of examples: the primary includes updating based totally on a single condition, while the second relates to updating based totally on multiple conditions.
Throughout this article, we can make use of PostgreSQL Server as our database and reference the SELECT keyword for illustrative purposes.
Update Multiple Rows in PostgreSQL
In PostgreSQL, the UPDATE statement is a powerful tool used to modify existing records within a table. It allows you to make precise changes to specific columns of one or more rows based on specified conditions. When using the UPDATE statement, you first identify the table you want to update, and then you set the new values for the desired columns using the SET clause.
Conditions for which rows to update are specified in the WHERE clause, ensuring that only the rows meeting those criteria are modified. This statement enables you to efficiently update data, whether it's adjusting employee salaries, changing positions, or updating other pertinent information. Additionally, careful consideration should be given to crafting update statements to avoid unintended changes and maintain data consistency.
Syntax:
UPDATE table_name
SET column1 = value1,
column2 = value2,
column3 = value3,
...
WHERE condition;
Steps to Update Multiple Rows in PostgreSQL
Step 1: Create a Database
First, create a database using the following query. We'll name our database "employee_database," but you can choose any name you prefer.
Query:
CREATE DATABASE employee_database;
Step 2: Use the Database
Now that your database is created, you have to use the data database that you have just created. To use the database that you have just created you will have to use the following query.
Query:
\c employee_database
Step 3: Create a Table
Create a table of employee inside the database employee_database. This table has 6 columns namely 'employee_id', 'name', 'email', 'salary', 'position', from the 'department' containing the id of the employee, name of the employee, email address of the employee, salary of the employee, position of the employee and the department in which the employee works in.
Query:
CREATE TABLE employee (
employee_id INTEGER,
name VARCHAR(100),
email VARCHAR(100),
salary DECIMAL(10, 2),
position VARCHAR(100),
department VARCHAR(100)
);
Step 4: Insert Data into the Table
After the table is created, now add data into your table using the query below.
Query:
INSERT INTO employee (employee_id, name, email, salary, position, department)
VALUES
(101, 'John Doe', '[email protected]', 50000.00, 'Software Engineer', 'Engineering'),
(102, 'Jane Smith', '[email protected]', 60000.00, 'Data Analyst', 'Analytics'),
(103, 'Michael Johnson', '[email protected]', 70000.00, 'Product Manager', 'Product Management'),
(104, 'Emily Davis', '[email protected]', 55000.00, 'Marketing Manager', 'Marketing'),
(105, 'David Lee', '[email protected]', 48000.00, 'Customer Support Specialist', 'Customer Service'),
(106, 'Sophia Garcia', '[email protected]', 62000.00, 'Financial Analyst', 'Finance'),
(107, 'Matthew Wilson', '[email protected]', 53000.00, 'HR Manager', 'Human Resources'),
(108, 'Olivia Brown', '[email protected]', 58000.00, 'Sales Representative', 'Sales'),
(109, 'Daniel Martinez', '[email protected]', 54000.00, 'Operations Coordinator', 'Operations'),
(110, 'Ava Taylor', '[email protected]', 60000.00, 'Business Analyst', 'Business Development');
Output:
Insert DataMethods to Update Multiple Rows in PostgreSQL
Once, the table has been created, we can now use two ways to update multiple rows in PostgreSQL. These are:
- Using VALUES Clause in a FROM Statement
- Using CASE Statement
1. Updating Multiple Rows in PostgreSQL using VALUES Clause
Now that you have inserted the data into your table, you now want to update some data from the employees table at once. To update multiple rows of your table at once you have to use the query below. UPDATE statement modifies existing records in a table by setting new values to specified columns. You can filter which records to update using the WHERE clause based on certain conditions.
Query:
UPDATE employee
SET salary = data.new_salary
FROM (
VALUES
('John Doe', 'Senior Software Engineer', 52000.00),
('Jane Smith', 'Senior Data Analyst', 61000.00),
('Michael Johnson', 72000.00),
('Emily Davis', 56000.00),
('David Lee', 49000.00),
('Sophia Garcia', 63000.00),
('Matthew Wilson', 54000.00),
('Olivia Brown', 59000.00),
('Daniel Martinez', 55000.00),
('Ava Taylor', 'Senior Business Analyst', 61000.00)
) AS data(name, position, new_salary)
WHERE employee.name = data.name;
Output:
Updated Data2. Updating Multiple Rows in PostgreSQL using CASE Statement
You can also use the CASE
statement within the UPDATE
statement to conditionally update values. If the value in column_name matches 'column_name1', then it updates column_value to column_value1.
Syntax:
UPDATE table_name
SET column_name =
CASE column_name
WHEN 'column_name1' THEN column_value1
WHEN 'column_name2' THEN column_value2
ELSE column_value
END
WHERE column_name IN ('column_name1', 'column_name2');
where,
- ELSE column_value: This part of the CASE statement specifies the default value for column_value if none of the previous conditions are met.
- END: This closes the CASE statement.
Query:
UPDATE Department
SET Position =
CASE employee_id
WHEN 101 THEN 'Senior Software Engineer'
WHEN 102 THEN 'Senior Data Analyst'
WHEN 103 THEN 'Senior Product Manager'
ELSE 'Position Not Uptated'
END;
Output:
Updated DataConclusion
In PostgreSQL, the UPDATE
statement is a powerful tool used to modify existing records within a table. It allows you to make precise changes to specific columns of one or more rows based on specified conditions.
By carefully crafting UPDATE
statements, you can efficiently update data, ensuring that your database remains accurate and up-to-date. This is essential for tasks such as adjusting employee salaries, changing positions, or updating other pertinent information. Always consider the conditions and structure of your UPDATE
statements to maintain data consistency and integrity.
Similar Reads
How to Insert Multiple Rows to a Table in PostgreSQL?
Inserting multiple rows into a table in PostgreSQL is a common and efficient operation, especially when handling large datasets. By executing a single SQL query, multiple rows can be added simultaneously, reducing overhead and enhancing performance. This method is particularly valuable in scenarios
5 min read
How to Update Top 100 Records in PostgreSQL?
PostgreSQL is one of the most advanced general-purpose object-relational database management systems and is open-source. Being an open-source software, its source code is available under the open-sourcePostgreSQL license, a liberal open-source license. Anyone with the right skills is free to use, mo
5 min read
How To Update Multiple Columns in MySQL?
To update multiple columns in MySQL we can use the SET clause in the UPDATE statement. SET clause allows users to update values of multiple columns at a time. In this article, we will learn how to update multiple columns in MySQL using UPDATE and SET commands. We will cover the syntax and examples,
3 min read
How to UPDATE Multiple ROWs in a Single Query in MySQL?
In the world of database management efficiency, perfection, accuracy & precision are considered to play a vital role. When it comes to MySQL stands for âStructured Query Languageâ, MySQL is the most common standardized language used for database management currently. A database is a structured c
5 min read
How to Update Multiple Rows at Once Using PL/SQL?
Updating multiple rows simultaneously is a common requirement in database management, especially when handling large datasets. PL/SQL, the procedural extension of SQL in Oracle databases, provides various techniques to accomplish this task efficiently. In this article, we will explore three powerful
4 min read
PostgreSQL - Insert Multiple Values in Various Rows
PostgreSQL, one of the most popular relational database management systems (RDBMS), is widely used for storing structured data in a tabular format, much like MySQL. In relational databases, data is stored in tables where each row represents a record and each column represents an attribute. One of th
3 min read
How to Select Row With Max Value in PostgreSQL
In PostgreSQL, efficiently selecting rows with maximum values from a table is a common task faced by developers and database administrators. Whether you're working on analytics, reporting, or data manipulation, knowing how to retrieve the maximum value per group can significantly enhance your SQL sk
4 min read
Dynamically Update Multiple Rows with PostgreSQL and Python
In this tutorial, we will explore how to dynamically update multiple rows in a PostgreSQL database using Python. By the end of this tutorial, you'll have a solid understanding of how to write efficient Python code that interacts with PostgreSQL to update multiple rows in a database. We'll cover topi
3 min read
How to Update Top 100 Records in PL/SQL?
In terms of database management, the ability to update specific subsets of data is crucial for maintaining system integrity and meeting user needs. In this article, we will understand two primary methods for updating top records. Using the ROWNUM function and Using the ORDER BY clause. Each method i
4 min read
How to Update Table Rows in PostgreSQL Using Subquery?
PostgreSQL is a general-purpose object-relational database management system and is open-source. It ensures data integrity features like constraints, transactions and foreign key support. In this article, We will understand How to update table rows in PostgreSQL using subquery using various methods,
5 min read