Skip to content

UPDATE statement

View as Markdown

The UPDATE statement changes column values in existing rows. The SET clause names the columns and their new values; a WHERE clause selects the rows to change. Without WHERE, every row is updated.

-- one table (or view)
UPDATE table_name
SET column_name = {<expr> | DEFAULT} [, column_name = {<expr> | DEFAULT} ...]
[WHERE <search_condition>]
[ORDER BY {col_name | <expr>} [ASC | DESC], ...]
[LIMIT row_count]
-- several tables
UPDATE <table_specifications>
SET column_name = {<expr> | DEFAULT} [, column_name = {<expr> | DEFAULT} ...]
[WHERE <search_condition>]
  • table_name: the table or updatable view to change.
  • table_specifications: the tables to read, written like the FROM clause of a SELECT, including joins. Columns of any of these tables can be updated.
  • column_name: the column to change. In a multi-table update, qualify it with the table name or alias.
  • expr | DEFAULT: the new value. It can be any expression, including a subquery that returns one value, or DEFAULT for the column's default value.
  • search_condition: only rows that satisfy the condition are updated.
  • ORDER BY and LIMIT (one table only): the order in which rows are updated and the maximum number of rows to update. row_count can be an unsigned integer, a host variable or a simple expression.

Product 1795 sells for 29.00 and has 31 units in stock. Raise the price and take one unit out of stock:

UPDATE product
SET unit_price = 32.50, units_in_stock = units_in_stock - 1
WHERE id = 1795;
SELECT id, name, unit_price, units_in_stock FROM product WHERE id = 1795;
id name unit_price units_in_stock
1795 Balboa Persistence Tee 32.50 30

Every unshipped order gets a shipping date:

UPDATE orders SET shipped_date = DATE'2026-01-05' WHERE shipped_date IS NULL;
9 rows affected.

A subquery in SET must return a single value. Here, order 455 is charged the average shipping cost of all shipped orders:

UPDATE orders
SET shipping_cost = (SELECT AVG(shipping_cost) FROM orders WHERE shipped_date IS NOT NULL)
WHERE id = 455;
SELECT shipping_cost FROM orders WHERE id = 455;
shipping_cost
16.94

With one table, ORDER BY decides the order in which rows are updated and LIMIT caps the number of rows. Restock only the three most expensive products that are sold out:

UPDATE product
SET units_in_stock = 100
WHERE units_in_stock = 0
ORDER BY unit_price DESC
LIMIT 3;
SELECT id, name, unit_price, units_in_stock
FROM product
WHERE units_in_stock = 100
ORDER BY unit_price DESC;
id name unit_price units_in_stock
29 Thüringer Rostbratwurst 123.79 100
1110 Ingrid Running Jacket 84.00 100
159 Juno Jacket 77.00 100

The update order also matters when the table has triggers: row-level triggers fire in the order the rows are updated.

DEFAULT sets a column back to its default value. The example uses a small table for support tickets:

CREATE TABLE support_ticket (
id INT AUTO_INCREMENT PRIMARY KEY,
customer_id INT NOT NULL,
subject VARCHAR(100) NOT NULL,
status VARCHAR(10) DEFAULT 'open',
priority INT DEFAULT 3
);
INSERT INTO support_ticket (customer_id, subject, status, priority)
VALUES (5, 'Wrong size delivered', 'closed', 1);
UPDATE support_ticket SET status = DEFAULT, priority = DEFAULT WHERE id = 1;
SELECT * FROM support_ticket;
id customer_id subject status priority
1 5 Wrong size delivered open 3

By default, a column referenced on the right side of SET has the value stored in the row before the update, even if the same statement assigns it a new value. Continuing with the ticket above (priority 3):

UPDATE support_ticket SET priority = 1, status = priority;
SELECT * FROM support_ticket;
id customer_id subject status priority
1 5 Wrong size delivered 3 1

status received the old priority, 3, not the new value 1. The update_use_attribute_references parameter changes this: when it is yes, assignments are applied from left to right and later assignments see the new values (here, status would be 1). The default is no.

In a multi-table update, the tables are joined as in a SELECT and the SET clause can change columns of any of them.

Give a 10% discount on every undiscounted line of the product Erika Running Short, using the product name from the product table:

UPDATE order_items oi
INNER JOIN product p ON oi.product_id = p.id
SET oi.discount_pct = 10
WHERE p.name = 'Erika Running Short' AND oi.discount_pct = 0;
518 rows affected.

Change an order and its lines in one statement:

UPDATE orders o
INNER JOIN order_items oi ON oi.order_id = o.id
SET o.shipping_cost = 0, oi.discount_pct = 5
WHERE o.id = 455;
SELECT o.shipping_cost, oi.product_id, oi.discount_pct
FROM orders o
JOIN order_items oi ON oi.order_id = o.id
WHERE o.id = 455
ORDER BY oi.product_id;
shipping_cost product_id discount_pct
0.00 123 5
0.00 335 5
0.00 1795 5

When one row of the updated table joins several rows of another table, it is updated once, with the values from the first joined row found. Write the join so that each updated row matches one row, or aggregate first in a CTE. For more on joins, see Join query.

An updatable view can be the target of an UPDATE; the change is applied to the underlying table. The view below lists products that are almost sold out:

CREATE VIEW low_stock AS
SELECT id, name, units_in_stock FROM product WHERE units_in_stock < 5;
UPDATE low_stock SET units_in_stock = 5 WHERE id = 5;
SELECT id, name, units_in_stock FROM product WHERE id = 5;
id name units_in_stock
5 Chef Antons Gumbo Mix 5

If a view cannot be updated, CoraDB rejects the statement with Vclass <view_name> is not updatable. See CREATE VIEW.

Rows of vertex and edge tables are updated with the same statement. Writing UPDATE VERTEX or UPDATE EDGE makes the intent explicit; the rest of the syntax is unchanged. Updating an edge changes its properties, not the vertices it connects.

UPDATE VERTEX product SET units_in_stock = units_in_stock + 10 WHERE id = 1795;
UPDATE EDGE likes SET since = SYSDATETIME WHERE since < DATETIME'2020-01-01 00:00:00';