UPDATE
Change column values in existing rows. Values can be expressions over the row's current values.
In this article
Changes column values in the rows that match a condition. The primary key cannot be changed.
Syntax
UPDATE <table> SET <column> = <expression> [, <column> = <expression>] ...
[FROM <table> [AS <alias>]] [WHERE <condition>] [RETURNING <item> [, ...]]
UPDATE <table> SET (<column>, ...) = (<expression>, ...) [WHERE <condition>]
WHERE takes the same conditions as SELECT, subqueries included. Without WHERE, every row is updated. Column names match ignoring case.
Examples
Set a value
UPDATE items SET in_stock = 1 WHERE id = 5;
UPDATED 1
Compute from the current value
UPDATE purchases SET qty = qty + 1 WHERE id = 2;
This is one statement, with no gap between reading and writing in which another writer’s change could be lost.
Swap two columns
Every expression reads the row as it was before the statement, so this swaps the item’s name and category:
UPDATE items SET name = category, category = name WHERE id = 2;
Use a scalar subquery
UPDATE purchases SET qty = qty + (SELECT MAX(qty) FROM purchases WHERE account_id = 1) WHERE id = 3;
The subquery runs once, before the first row is updated.
Update from another table
FROM names one more table that the WHERE can read. A row is updated when some row of that table matches:
UPDATE purchases SET qty = 9 FROM accounts AS a
WHERE a.id = purchases.account_id AND a.owner = 'bob';
A target row that matches more than one FROM row is refused, because which one should set it would be a guess:
ERR UPDATE ... FROM matched a row with more than one FROM row; which one sets it would be a guess (BU-Q16)
Return the updated rows
kdbs> UPDATE purchases SET qty = qty + 1 WHERE id = 1 RETURNING id, qty
id,qty
1,2
UPDATED 1
Expressions
| Kind | Available |
|---|---|
| Arithmetic | + - * / % on numbers, ` |
| Comparison and logic | = != < <= > >=, AND, OR, NOT, IS [NOT] NULL (for bool columns). Inequality is !=; <> is not accepted. |
| Forms | CASE, COALESCE, NULLIF, GREATEST, LEAST, CAST(x AS t), x::t |
| Numeric functions | ABS, SIGN, CEIL, FLOOR, ROUND, TRUNC, MOD |
| Text functions | UPPER, LOWER, LENGTH, SUBSTRING, TRIM, LTRIM, RTRIM, POSITION(a IN b), REPLACE, LPAD, RPAD, CONCAT |
| Date and time | NOW(), CURRENT_DATE, DATE(ts) |
Operator precedence follows PostgreSQL.
Types are exact
- There is no implicit conversion.
int32 + int64needs aCAST. A literal takes the type of the column or operand next to it. - A decimal result of a different scale must be narrowed with
ROUND(x, s)orCAST, which round half away from zero. - Every operation is checked in its own type. A result out of range is refused at that row, naming the operator and the row’s key. Division by zero is refused.
NULLpropagates:SET n = n + 1leaves aNULLasNULL. AssigningNULLto aNOT NULLcolumn is refused.
The primary key cannot be updated
The primary key is the row’s identity. Assigning it is refused when the statement is compiled, so nothing is written:
kdbs> UPDATE items SET id = 9 WHERE id = 1;
^
ERR tuple key column 'id' cannot be updated at byte 17; it is the tuple's identity, not a field of it
Column names match ignoring case, so SET ID = 9 is refused the same way.
Remarks
-
A
WHEREliteral of the wrong type for its column is refused when the statement is compiled, before any row is touched, whether or not a row would match:kdbs> UPDATE purchases SET qty = 1 WHERE qty = '999'; ^ ERR column 'qty' literal at byte 41: expects an integer literal - unquoted, with no fraction -
A statement whose expression does not type-check is refused even if no row matches. A bare literal assigned by
SETis the exception: it is checked per row, soSET qty = 'abc'with aWHEREthat matches nothing repliesUPDATED 0. -
A column assigned twice in one statement is refused.
-
An
UPDATEnever moves a row; every row has a fixed size. -
Assertions are checked by how much the update changes each group; foreign keys by the new parent; a Transition by the move from the old value to the new one.
-
Reply:
UPDATED <n>, the number of rows changed. WithRETURNING, the listed items for each updated row come first.
Limitations
- Not available in
SETexpressions:IN,BETWEEN,LIKE,EXISTS,IS TRUE,IS DISTINCT FROM, boolean literals (TRUE,FALSE),<>,DEFAULT,INTERVAL, correlated subqueries, and subqueries oversys.*views. Each is refused at its byte, most by name, for exampleERR NOT_IMPLEMENTED retryable=0 'IN' in a SET value is not supported (byte 41). - A scalar subquery must return one column and at most one row, and cannot read the row being updated.
UPDATE ... FROMtakes one table, read only inWHERE: aSETvalue or aRETURNINGitem that names it is refusedNOT_IMPLEMENTED.