PostgreSQL Tutorial
Data Types
Querying & Filtering Data
Managing Tables
Modifying Data
Conditionals
Control Flow
Transactions & Constraints
Working with JOINS & Schemas
Roles & Permissions
Working with Sets
Subquery & CTEs
User-defined Functions
Important In-Built Functions
PostgreSQL PL/pgSQL
Variables & Constants
Stored Procedures
Working with Triggers
Working with Views & Indexes
Errors & Exception Handling
The UPDATE
statement in PostgreSQL is used to modify existing records in a table. It allows you to change data based on specific conditions, ensuring that you can maintain and correct the information in your tables as needed.
UPDATE table_name SET column1 = value1, column2 = value2, ... WHERE condition;
SET Clause:
SET
clause specifies the columns you want to modify and the values you want to set.WHERE Clause:
WHERE
clause is essential in the UPDATE
statement to ensure that you're updating the correct rows. If you omit the WHERE
clause, all rows in the table will be updated!WHERE
clause to avoid unintentional data modifications.Returning Modified Rows:
PostgreSQL offers the RETURNING
clause with the UPDATE
statement, which returns the rows as they are after the update:
UPDATE table_name SET column1 = value1 WHERE condition RETURNING *;
Updating Using Data from Another Table:
You can also update a table based on data in another table. For instance:
UPDATE table1 SET table1.column1 = table2.column2 FROM table2 WHERE table1.column3 = table2.column4;
Concurrency and Locking:
UPDATE
is executed, PostgreSQL locks the rows that are being updated to prevent other transactions from modifying them simultaneously.Performance Considerations:
VACUUM
command, can help in cleaning up old row versions and recovering space.Precautions:
UPDATE
, especially on production databases, always ensure you have a backup or that the data can be recovered if needed.SELECT
statement with the same WHERE
clause first to review the rows that will be updated.ROLLBACK
if something goes wrong.Let's say you have a table named employees
and you want to update the salary
of an employee with the employee_id
of 5:
UPDATE employees SET salary = 60000 WHERE employee_id = 5;
In summary, the UPDATE
statement is a powerful tool in PostgreSQL to modify existing records in a table. Proper caution and understanding of its behavior are essential to ensure accurate and safe data modifications.
PostgreSQL UPDATE statement example:
Update values in a table using the UPDATE
statement:
UPDATE example_table SET column1 = new_value WHERE condition;
UPDATE multiple columns in PostgreSQL: Update values in multiple columns:
UPDATE example_table SET column1 = new_value1, column2 = new_value2 WHERE condition;
PostgreSQL UPDATE with JOIN:
Update values in a table using a JOIN
condition:
UPDATE table1 SET column1 = new_value FROM table2 WHERE table1.id = table2.id;
Conditional UPDATE in PostgreSQL: Update values conditionally based on a specific condition:
UPDATE example_table SET column1 = new_value WHERE condition;
UPDATE rows based on a condition in PostgreSQL:
Update rows based on a condition using a CASE
statement:
UPDATE example_table SET column1 = CASE WHEN condition1 THEN new_value1 WHEN condition2 THEN new_value2 ELSE column1 END;
Using subqueries with UPDATE in PostgreSQL: Update values using subqueries:
UPDATE example_table SET column1 = (SELECT new_value FROM another_table WHERE condition) WHERE condition;
Batch UPDATE in PostgreSQL: Update multiple rows in a single statement:
UPDATE example_table SET column1 = new_value WHERE id IN (1, 2, 3);
Update with CASE statement in PostgreSQL:
Use the CASE
statement for conditional updates:
UPDATE example_table SET column1 = CASE WHEN condition1 THEN new_value1 WHEN condition2 THEN new_value2 ELSE column1 END;