---
metadata:
  - name: generator
    content: Diplodoc Platform v5.52.0
alternate:
  - https://ydb.tech/docs/en/postgresql/statements/update.md?version=v25.3
  - https://ydb.tech/docs/ru/postgresql/statements/update.md?version=v25.3
  - href: en/postgresql/statements/update.md
    type: text/markdown
    title: Markdown version
  - href: ../../llms.txt
    type: text/markdown
    title: llms.txt
sourcePath: en/core/postgresql/statements/update.md
---
> **Documentation Index:** Fetch the complete configuration index at https://ydb.tech/docs/en/llms.txt

# UPDATE

<!-- markdownlint-disable blanks-around-fences -->

<!-- source: en/postgresql/_includes/alert_preview.md -->
{% note warning %}

At the moment, YDB's compatibility with PostgreSQL **is under development**, so not all PostgreSQL constructs and [functions](https://ydb.tech/docs/en/postgresql/functions.md?version=v25.3) are supported yet. PostgreSQL compatibility is available for testing in the form of a Docker container, which can be deployed by following these [instructions](https://ydb.tech/docs/en/postgresql/docker-connect.md?version=v25.3).

{% endnote %}
<!-- endsource: en/postgresql/_includes/alert_preview.md -->

The syntax of the `UPDATE` statement:

<!-- source: en/postgresql/_includes/statements/update/syntax.md -->
```sql
UPDATE <table name>
SET <column name> = [<new value>, CASE ... END]
WHERE <search column name> = [<search value>, IN]
```
<!-- endsource: en/postgresql/_includes/statements/update/syntax.md -->

The `UPDATE ... SET ... WHERE` statements works as follows:

1. **Table name is specified** – `UPDATE <table name>`, where the data will be updated;
2. **Column name is indicated** – `SET <column name>`, where the data will be updated;
3. **New value is set** – `<new value>`;
4. **Search criteria are specified** – `WHERE` with the indication of the search column `<search column name>` and the value that the search criterion should match `<search value>`. If `CASE` is used, then the `IN` operator is specified with a list of values `<column name>`.

## Updating a single row in a table with conditions

#|
|| Update without conditions | Update with conditions ||
||

<!-- source: en/postgresql/_includes/statements/update/update_where.md -->
```sql
UPDATE people
SET name = 'Alexander'
WHERE lastname = 'Doe';
```
<!-- endsource: en/postgresql/_includes/statements/update/update_where.md -->

|

<!-- source: en/postgresql/_includes/statements/update/update_where_and.md -->
```sql
UPDATE people
SET age = 31
WHERE country = 'USA' AND city = 'Los Angeles';
```
<!-- endsource: en/postgresql/_includes/statements/update/update_where_and.md -->

||
|#

In the example "Update with conditions", the condition combining operator `AND` is used – the condition will only be satisfied when both parts meet the truth conditions. The operator `OR` can also be used – the condition will be satisfied if at least one part meets the truth conditions. There can be multiple condition operators:

<!-- source: en/postgresql/_includes/statements/update/update_where_and_or.md -->
```sql
UPDATE people
SET age = 31
WHERE country = 'USA' AND city = 'Los Angeles' OR city = 'Florida';
```
<!-- endsource: en/postgresql/_includes/statements/update/update_where_and_or.md -->

## Updating a single record in a table using expressions or functions: {#update_set_func_where}

Frequently during updates, it is necessary to perform mathematical actions on the data or to modify it using functions.

#|
|| Update with the use of expressions | Update with the use of functions ||
||

<!-- source: en/postgresql/_includes/statements/update/update_set_where.md -->
```sql
UPDATE people
SET age = age + 1
WHERE country = 'Canada';
```
<!-- endsource: en/postgresql/_includes/statements/update/update_set_where.md -->

|

<!-- source: en/postgresql/_includes/statements/update/update_set_func_where.md -->
```sql
UPDATE people
SET name = UPPER(name)
WHERE country = 'USA';
```
<!-- endsource: en/postgresql/_includes/statements/update/update_set_func_where.md -->

||
|#


## Updating multiple fields of a table row {#update_set_where}

Data can be updated in multiple columns simultaneously. For this, a list of `<column name> = <column new value>` is made after the keyword `SET`:

<!-- source: en/postgresql/_includes/statements/update/update_set_few_col_where.md -->
```sql
UPDATE people
SET country = 'Russia', city = 'Moscow'
WHERE lastname = 'Smith';
```
<!-- endsource: en/postgresql/_includes/statements/update/update_set_few_col_where.md -->

## Updating multiple rows in a table using the **CASE ... END** construction {#update_set_case_end_where}

For simultaneous updating of different values in different rows, the `CASE ... END` instruction can be used with nested data selection conditions `WHEN <column name> <condition> (=, >, <) THEN <new value>`. This is followed by the `WHERE <column name> IN (<column value>, ...)` construct, which allows setting a list of values for which the condition will be executed.

Example of changing the age (`age`) of people (`people`) depending on their names:

<!-- source: en/postgresql/_includes/statements/update/update_set_case_where.md -->
```sql
UPDATE people
SET age = CASE
            WHEN name = 'John' THEN 32
            WHEN name = 'Jane' THEN 26
          END
WHERE name IN ('John', 'Jane');
```
<!-- endsource: en/postgresql/_includes/statements/update/update_set_case_where.md -->

<!-- source: en/postgresql/_includes/alert_locks.md -->
{% note info %}

Unlike PostgreSQL, YDB uses optimistic locking. This means that transactions check the conditions for the necessary locks at the end of their operation, not at the beginning. If the lock has been violated during the transaction's execution, such a transaction will end with a `Transaction locks invalidated` error. In this case, you can try to execute a similar transaction again.

{% endnote %}
<!-- endsource: en/postgresql/_includes/alert_locks.md -->