UPDATE

v3.2.0

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

KindAvailable
Arithmetic+ - * / % on numbers, `
Comparison and logic= != < <= > >=, AND, OR, NOT, IS [NOT] NULL (for bool columns). Inequality is !=; <> is not accepted.
FormsCASE, COALESCE, NULLIF, GREATEST, LEAST, CAST(x AS t), x::t
Numeric functionsABS, SIGN, CEIL, FLOOR, ROUND, TRUNC, MOD
Text functionsUPPER, LOWER, LENGTH, SUBSTRING, TRIM, LTRIM, RTRIM, POSITION(a IN b), REPLACE, LPAD, RPAD, CONCAT
Date and timeNOW(), CURRENT_DATE, DATE(ts)

Operator precedence follows PostgreSQL.

Types are exact

  • There is no implicit conversion. int32 + int64 needs a CAST. 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) or CAST, 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.
  • NULL propagates: SET n = n + 1 leaves a NULL as NULL. Assigning NULL to a NOT NULL column 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 WHERE literal 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 SET is the exception: it is checked per row, so SET qty = 'abc' with a WHERE that matches nothing replies UPDATED 0.

  • A column assigned twice in one statement is refused.

  • An UPDATE never 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. With RETURNING, the listed items for each updated row come first.

Limitations

  • Not available in SET expressions: IN, BETWEEN, LIKE, EXISTS, IS TRUE, IS DISTINCT FROM, boolean literals (TRUE, FALSE), <>, DEFAULT, INTERVAL, correlated subqueries, and subqueries over sys.* views. Each is refused at its byte, most by name, for example ERR 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 ... FROM takes one table, read only in WHERE: a SET value or a RETURNING item that names it is refused NOT_IMPLEMENTED.