↑ arvau

Wikipedia / Merge (SQL)

A relational database management system uses SQL (also called upsert) statements to new records or or existing records depending on whether condition matches. It was officially introduced in the SQL:2003 standard, and expanded in the SQL:2008 standard.

Usage

A right join is employed over the Target (the table) and the Source (the table / view / sub-query)--where Target is the left table and Source is the right one. The four possible combinations yield these rules:

  • If the field(s) in the Source matches the field(s) in the Target, then

  • If the field(s) in the Source does not match the field(s) in the Target, then

  • If the field(s) does not exist in the Source but does exist in the Target, then no action is performed.

  • If the field(s) does not exist in either the Source or Target, then no action is performed.

If multiple Source rows match a given Target row, an error is mandated by SQL:2003 standards. You cannot update a Target row multiple times with a statement

Implementations

Database management systems PostgreSQL, Oracle Database, IBM Db2, Teradata, EXASOL, Firebird, CUBRID, H2, HSQLDB, MS SQL (Transact-SQL), MonetDB, Vectorwise, Apache Derby, and Spark SQL support the standard syntax. Some also add non-standard SQL extensions.

Synonymous

Some database implementations adopted the term upsert (a portmanteau of update and insert) to a database statement, or combination of statements, that inserts a record to a table in a database if the record does not exist or, if the record already exists, updates the existing record. This synonym is used in PostgreSQL (v9.5+) and SQLite (v3.24+).

Upsert most commonly refers to non-standard SQL extensions that provide this behavior, most commonly via an expansion of the INSERT statement:

  1. A MySQL INSERT extension which can be used to achieve a similar effect with the limitation that the join between target and source has to be made only on PRIMARY KEY or UNIQUE constraints, which is not required in the ANSI/ISO MERGE standard.

  2. Also supported by CUBRID.

  3. A MySQL INSERT extension which tells the server to ignore "duplicate key" errors and go on (existing rows will not be inserted or updated, but all new rows will be inserted).

  4. A PostgreSQL syntax also used by SQLite.

Other expansions used to reach a similar effect include:

  1. A MySQL statement which first attempts an insert, and if that fails, deletes the row, if exists, and then inserts the new one.

  2. Also supported by CUBRID.

  3. Also supported by SQLite under the names of and .

  4. A Firebird extension. Does not provide the option to take different actions on insert versus update (e.g. setting a new sequence value only for new rows, not for existing ones.)

In addition:

  • Apache Phoenix supports syntax in two versions: one directly followed by values (and having an optional clause), the other followed by a statement.

  • Spark SQL supports and clauses in actions.

  • Apache Impala supports .

Extensions

Non-standard extensions to the standard statement include:

Firebird supports though fails to throw an error when there are multiple Source data rows.

IBM Db2 extends the syntax with multiple and clauses, distinguishing them with guards.

Microsoft SQL Server extends with supporting guards and also with supporting Left Join via clauses.

Other data structures

NoSQL

A similar concept is applied in some NoSQL databases.

Most key-value databases, staring from the classical Unix dbm, have a "set" or "store" command that can simply overwrite the existing value.

  • dbm's takes an argument that decides whether to overwrite an existing value or to keep it.

  • In Redis, the operation defaults to replacing the value if it exists. Additional condition options can instruct it to only replace (i.e. to only set if present), or to set only if not present (working as a default/fallback value).

MongoDB is a document-oriented database, where each document is a BSON. normally take a query and a set of operations and applies the operations on the matching entry; an example operation is , which gives the simple overwriting behavior for setting a JSON path. It also has an mode, which specifies that a new value should be inserted if the query finds nothing.

Programming languages

ECMAScript 2026 formalizes an "upsert" proposal, which entails a getOrInsert(key, default) API. If the Map contains the requested key, this API simply returns the corresponding value. If the map does not contain the requested key, it would set the entry to the provided default value and return the default value.

See also

Join in particular:

References