---
title: "REPLACE statement"
description: "Insert or overwrite rows with the CoraDB REPLACE statement: a row that duplicates a primary or unique key replaces the old one."
url: "https://www.coradb.ai/manual/sql/query/replace.html"
markdown_url: "https://www.coradb.ai/manual/markdown/sql/query/replace.md"
product: "CoraDB"
version: "1.0"
keywords: "replace statement, replace into, upsert, replace values, replace set, replace select, coradb replace"
---

# REPLACE statement

The **REPLACE** statement works like [INSERT](https://www.coradb.ai/manual/sql/query/insert.html), with one difference: when a new row would duplicate a **PRIMARY KEY** or **UNIQUE** value, CoraDB deletes the existing row and inserts the new one instead of returning an error. When there is no conflict, `REPLACE` simply inserts.

Because the old row is deleted, columns you do not give a value for get their default value, not the value they had before. To change only some columns of an existing row, use [INSERT ... ON DUPLICATE KEY UPDATE](https://www.coradb.ai/manual/sql/query/insert.html#on-duplicate-key-update) instead.

> **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
REPLACE [INTO] table_name [(column_name, ...)]
    {VALUES | VALUE} ({expr | DEFAULT}, ...) [, ({expr | DEFAULT}, ...) ...]

REPLACE [INTO] table_name
    SET column_name = {expr | DEFAULT} [, column_name = {expr | DEFAULT} ...]

REPLACE [INTO] table_name [(column_name, ...)]
    <select_statement>
```

- *table_name*: the table to write to.
- *column_name*: the columns that receive values. Without a list, give a value for every column in table order.
- *expr* | **DEFAULT**: the value for each column, or `DEFAULT` for the column's default value.
- *select_statement*: a query whose rows are written to the table.

## Examples

Make a working copy of three countries:

```sql
CREATE TABLE country_copy LIKE countries;
INSERT INTO country_copy SELECT * FROM countries WHERE code IN ('DEU', 'FRA', 'ITA');

SELECT * FROM country_copy ORDER BY code;
```

| code | name |
|---|---|
| DEU | Germany |
| FRA | France |
| ITA | Italy |

`DEU` already exists and is replaced; `ESP` is new and is inserted:

```sql
REPLACE INTO country_copy VALUES ('DEU', 'Federal Republic of Germany'), ('ESP', 'Spain');
```

```text
3 rows affected.
```

The count includes the deleted row: 2 for `DEU` (one deleted, one inserted) and 1 for `ESP`. The `SET` form works the same way:

```sql
REPLACE INTO country_copy SET code = 'ITA', name = 'Italian Republic';

SELECT * FROM country_copy ORDER BY code;
```

| code | name |
|---|---|
| DEU | Federal Republic of Germany |
| ESP | Spain |
| FRA | France |
| ITA | Italian Republic |

With a query as the source, every returned row either replaces a matching row or is added:

```sql
REPLACE INTO country_copy (code, name)
SELECT code, UPPER(name) FROM countries WHERE code IN ('FRA', 'PRT');

SELECT * FROM country_copy ORDER BY code;
```

| code | name |
|---|---|
| DEU | Federal Republic of Germany |
| ESP | Spain |
| FRA | FRANCE |
| ITA | Italian Republic |
| PRT | PORTUGAL |

## Things to keep in mind

- `REPLACE` has to look up every new row in the table's primary key and unique indexes. On a table without a primary key or unique index it behaves like `INSERT`, so use `INSERT` there.
- A replaced row is deleted, so foreign keys that reference it are checked as for a [DELETE](https://www.coradb.ai/manual/sql/query/delete.html#foreign-keys). Cities reference `countries`, so replacing a country that has cities fails, while `ON DUPLICATE KEY UPDATE` succeeds:

  ```sql
  REPLACE INTO countries VALUES ('DEU', 'Federal Republic of Germany');
  ```

  ```text
  ERROR: Update/Delete operations are restricted by the foreign key 'fk_city_country_code'.
  ```

## See also

- [INSERT ... 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)
