A relational database management system uses SQL (also called upsert) statements to [[insert (SQL)|INSERT]]
new records or [[update (SQL)|UPDATE]]
existing records depending on whether condition matches. It was officially introduced in the standard, and expanded in the standard.
A right join is employed over the Target (the INTO table) and the Source (the USING table / view / sub-query)--where Target is the left table and Source is the right one. The four possible combinations yield these rules:
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 MERGE statement
Database management systems PostgreSQL,[1] Oracle Database, IBM Db2, Teradata, EXASOL, Firebird, CUBRID, H2, HSQLDB, MS SQL, Vectorwise and Apache Derby support the standard syntax. Some also add non-standard SQL extensions.
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+)[2] and SQLite (v3.24+).[3] It is also used to abbreviate the "MERGE" equivalent pseudo-code.
It is used in Microsoft Azure SQL Database.[4]
Some other database management systems support this, or very similar behavior, through their own, non-standard SQL extensions.
MySQL, for example, supports the use of syntax[5] 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 standard. It also supports syntax,[6] which first attempts an insert, and if that fails, deletes the row, if exists, and then inserts the new one. There is also an clause for the statement,[7] 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).
SQLite's works similarly. It also supports as an alias for compatibility with MySQL.[8]
Firebird supports though fails to throw an error when there are multiple Source data rows. Additionally there is a single-row version,, but the latter does not give you 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.)
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.
PostgreSQL supports merge since version 15 but previously supported merging via .[9]
CUBRID supports [10] statement. And supports the use of syntax.[11] It also supports for compatibility with MySQL.[12]
Apache Phoenix supports [13] and UPSERT SELECT
[14] syntax.
Spark SQL supports and clauses in actions.[15]
Apache Impala supports .[16]
A similar concept is applied in some NoSQL databases.
E.g. in MongoDB the fields in a value associated with a key can be updated with an operation. The raises an error if the key is not found.In the operation it is possible to set the flag: in this case a new value is stored associated to the given key if it does not exist, otherwise the whole value is replaced.
In Redis the operations sets the value associated with a given key. Redis does not know any detail of the internal structure of the value, so an update would have no meaning. So the operation has always a set or replace semantics.