UPDATE statement updates rows in a table.
Required privileges
You must have theSELECT and UPDATE privileges on the table.
Syntax
Parameters
See Common Table Expressions.
The name of the table that contains the rows you want to update.
An alias for the table name. When an alias is provided, it completely hides the actual table name.
The name of the column whose values you want to update.
The new value you want to use, the aggregate function you want to perform, or the scalar expression you want to use.To fill columns with their default values, use
DEFAULT VALUES in place of the value expression. To fill a specific column with its default value, use DEFAULT at the appropriate position.Specify a table to reference, but not update, in
UPDATE expressions, or in RETURNING and WHERE clauses.A scalar expression that returns Boolean values using columns. Update rows that return
TRUE.Without a WHERE clause, UPDATE updates all rows in the table.An
ORDER BY clause for ordering query results.A
LIMIT clause to limit the number of rows updated.Return values based on rows updated, where
target_list can be specific column names from the table, * for all columns, or computations using scalar expressions.To return nothing in the response, not even the number of rows updated, use RETURNING NOTHING.Force index selection
By using the explicit index annotation (also known as “index hinting”), you can override CockroachDB’s index selection and use a specific index for updating rows.Reference other tables
To reference values from a table other than the table being updated, add aFROM clause that specifies one or more tables in the cluster.
Bulk-update data
To update a large number of rows (tens of thousands of rows or more), we recommend iteratively updating subsets of the rows until all rows have been updated.Examples
Update a single column in a single row
Update multiple columns in a single row
Update using SELECT statement
Update with default values
Update using values from a different table
Update all rows
If the
sql_safe_updates session variable is set to true, the client will prevent the update. sql_safe_updates is set to true by default.SET statement to set session variables:
Update and return values
In this example, theRETURNING clause returns the id value of the row updated:
Update with index hints
See also
- INSERT
- UPSERT
- DELETE
- SELECT