---
title: "UPDATE statement"
description: "Change rows with the CoraDB UPDATE statement: SET with expressions and subqueries, ORDER BY and LIMIT, and multi-table updates."
url: "https://www.coradb.ai/manual/sql/query/update.html"
markdown_url: "https://www.coradb.ai/manual/markdown/sql/query/update.md"
product: "CoraDB"
version: "1.0"
keywords: "update statement, update set, update limit, update order by, update join, multi-table update, update view, update_use_attribute_references, coradb update"
---

# UPDATE statement

The **UPDATE** statement changes column values in existing rows. The `SET` clause names the columns and their new values; a [WHERE clause](https://www.coradb.ai/manual/sql/query/select.html#where-clause) selects the rows to change. Without `WHERE`, every row is updated.

> **Try it yourself:**
> The examples run on the [e-commerce sample database](https://www.coradb.ai/manual/sample-database.html) and show the real results. They change data, so start corasql with `corasql --no-auto-commit -u dba e-commerce` and finish with `ROLLBACK;` to leave the database as it was.


## Syntax

```text
-- 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](https://www.coradb.ai/manual/sql/query/select.html), 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.

## 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:

```sql
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:

```sql
UPDATE orders SET shipped_date = DATE'2026-01-05' WHERE shipped_date IS NULL;
```

```text
9 rows affected.
```

## 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:

```sql
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 |

## 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:

```sql
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](https://www.coradb.ai/manual/sql/trigger.html): row-level triggers fire in the order the rows are updated.

## Restoring default values

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

```sql
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

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):

```sql
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](https://www.coradb.ai/manual/admin/config_sql.html#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

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:

```sql
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;
```

```text
518 rows affected.
```

Change an order and its lines in one statement:

```sql
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](https://www.coradb.ai/manual/sql/query/cte.html#ctes-in-other-statements). For more on joins, see [Join query](https://www.coradb.ai/manual/sql/query/select.html#join-query).

## 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:

```sql
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](https://www.coradb.ai/manual/sql/schema/view_stmt.html#create-view).

## Updating vertices and edges

Rows of [vertex and edge tables](https://www.coradb.ai/manual/sql/schema/table_stmt.html#graph-tables-vertex-and-edge) 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.

```sql
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

- [INSERT](https://www.coradb.ai/manual/sql/query/insert.html) and [ON DUPLICATE KEY UPDATE](https://www.coradb.ai/manual/sql/query/insert.html#on-duplicate-key-update)
- [MERGE](https://www.coradb.ai/manual/sql/query/merge.html): update or insert in one statement
- [DELETE](https://www.coradb.ai/manual/sql/query/delete.html)
- [Transactions](https://www.coradb.ai/manual/sql/transaction.html)
