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

PostgreSQL - UPDATE

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.

Syntax:

UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;

Key Points:

  1. SET Clause:

    • The SET clause specifies the columns you want to modify and the values you want to set.
    • You can update multiple columns by separating the column-value pairs with commas.
  2. WHERE Clause:

    • The 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!
    • Always be cautious and double-check the conditions in your WHERE clause to avoid unintentional data modifications.
  3. 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 *;
      
  4. 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;
      
  5. Concurrency and Locking:

    • When an UPDATE is executed, PostgreSQL locks the rows that are being updated to prevent other transactions from modifying them simultaneously.
    • If two transactions try to update the same row simultaneously, one will have to wait until the other completes, which can lead to locking contention in high concurrency environments.
  6. Performance Considerations:

    • Frequent updates can lead to increased disk I/O due to the way PostgreSQL handles the MVCC (Multi-Version Concurrency Control). Old row versions are kept and marked as obsolete, leading to possible table "bloat."
    • Regular maintenance, like running the VACUUM command, can help in cleaning up old row versions and recovering space.
  7. Precautions:

    • Before executing an UPDATE, especially on production databases, always ensure you have a backup or that the data can be recovered if needed.
    • It's a good practice to run a SELECT statement with the same WHERE clause first to review the rows that will be updated.
    • Consider performing the update inside a transaction, allowing you to ROLLBACK if something goes wrong.

Example:

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.

  1. PostgreSQL UPDATE statement example: Update values in a table using the UPDATE statement:

    UPDATE example_table
    SET column1 = new_value
    WHERE condition;
    
  2. UPDATE multiple columns in PostgreSQL: Update values in multiple columns:

    UPDATE example_table
    SET column1 = new_value1,
        column2 = new_value2
    WHERE condition;
    
  3. 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;
    
  4. Conditional UPDATE in PostgreSQL: Update values conditionally based on a specific condition:

    UPDATE example_table
    SET column1 = new_value
    WHERE condition;
    
  5. 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;
    
  6. 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;
    
  7. Batch UPDATE in PostgreSQL: Update multiple rows in a single statement:

    UPDATE example_table
    SET column1 = new_value
    WHERE id IN (1, 2, 3);
    
  8. 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;