UPDATE statement
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.
Syntax
Section titled “Syntax”-- 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 tablesUPDATE <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
FROMclause 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
DEFAULTfor the column's default value. - search_condition: only rows that satisfy the condition are updated.
ORDER BYandLIMIT(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.
Updating one table
Section titled “Updating one table”Product 1795 sells for 29.00 and has 31 units in stock. Raise the price and take one unit out of stock:
UPDATE productSET unit_price = 32.50, units_in_stock = units_in_stock - 1WHERE 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.Using a subquery for the new value
Section titled “Using a subquery for the new value”A subquery in SET must return a single value. Here, order 455 is charged the average shipping cost of all shipped orders:
UPDATE ordersSET 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 |
ORDER BY and LIMIT
Section titled “ORDER BY and LIMIT”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 productSET units_in_stock = 100WHERE units_in_stock = 0ORDER BY unit_price DESCLIMIT 3;
SELECT id, name, unit_price, units_in_stockFROM productWHERE units_in_stock = 100ORDER 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.
Restoring default values
Section titled “Restoring default values”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 |
Referring to other columns in SET
Section titled “Referring to other columns in SET”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.
Updating several tables
Section titled “Updating several tables”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.idSET oi.discount_pct = 10WHERE 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.idSET o.shipping_cost = 0, oi.discount_pct = 5WHERE o.id = 455;
SELECT o.shipping_cost, oi.product_id, oi.discount_pctFROM orders o JOIN order_items oi ON oi.order_id = o.idWHERE o.id = 455ORDER 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.
Updating through a view
Section titled “Updating through a view”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.
Updating vertices and edges
Section titled “Updating vertices and edges”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';See also
Section titled “See also”- INSERT and ON DUPLICATE KEY UPDATE
- MERGE: update or insert in one statement
- DELETE
- Transactions