WHERE clause to target specific rows; omitting it updates every row in the table.
Syntax
Parameters
| Parameter | Description |
|---|---|
conflict_action | One of REPLACE, IGNORE, ABORT, ROLLBACK, or FAIL. Controls behavior on constraint violations. Default is ABORT. |
table_name | The table whose rows are to be modified. |
column_name = expression | Assigns a new value to a column. The expression can reference the original (pre-update) values of any column in the same row. |
from_clause | Additional tables to join for use in SET expressions and the WHERE clause. |
expression | Filter condition. Only rows where this evaluates to true are modified. |
result_column | Column or expression to return for each modified row. |
sort_expression | Determines the order rows are considered when combined with LIMIT. |
Conflict handling
The optionalOR clause specifies how to handle constraint violations during the update.
| Algorithm | Behavior |
|---|---|
ABORT | Roll back the current statement and return an error. The transaction remains active. This is the default. |
ROLLBACK | Roll back the entire transaction and return an error. |
FAIL | Stop at the first constraint violation and return an error, but keep changes made by earlier rows in this statement. |
IGNORE | Skip the row that caused the constraint violation and continue processing remaining rows. |
REPLACE | Delete the conflicting row before updating the current row. If a NOT NULL constraint fails and there is no DEFAULT value, the statement behaves as ABORT. |
SET clause
Assigns new values to one or more columns. Each expression sees the original (pre-update) values of all columns in the row.FROM clause
Allows theUPDATE to reference other tables. Columns from the joined tables can be used in SET expressions and the WHERE clause.
FROM clause, it refers to a second instance of that table for self-joins.
WHERE clause
Restricts which rows are updated. Only rows where the expression evaluates to true are modified. If omitted, every row in the table is updated.RETURNING clause
Causes theUPDATE to return the new values of each modified row.
ORDER BY and LIMIT
ORDER BY determines the order in which rows are considered, and LIMIT caps the number of rows actually modified. ORDER BY requires LIMIT to be present.
LIMIT accepts an optional OFFSET to skip rows before applying the limit:
Examples
Update rows matching a condition
Update using a join with FROM
Update with RETURNING
Multiple column update
See also
- INSERT — add new rows
- DELETE — remove rows
- SELECT — query data
- Transactions — group statements atomically