HOME

TheInfoList



OR:

A
relational database management system A relational database is a (most commonly digital) database based on the relational model of data, as proposed by E. F. Codd in 1970. A system used to maintain relational databases is a relational database management system (RDBMS). Many relatio ...
uses SQL (also called ''upsert'') statements to
INSERT Insert may refer to: *Insert (advertising) *Insert (composites) *Insert (effects processing) *Insert (filmmaking) *Insert key on a computer keyboard, used to switch between insert mode and overtype mode *Insert (molecular biology) *Insert (SQL) *Fi ...
new records or UPDATE existing records depending on whether condition matches. It was officially introduced in the SQL:2003 standard, and expanded in the SQL:2008 standard.


Usage

MERGE INTO tablename USING table_reference ON (condition) WHEN MATCHED THEN UPDATE SET column1 = value1 column2 = value2 ... WHEN NOT MATCHED THEN INSERT (column1 column2 ... VALUES (value1 value2 ...; 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 the ON field(s) in the Source matches the ON field(s) in the Target, then UPDATE * If the ON field(s) in the Source does not match the ON field(s) in the Target, then INSERT * If the ON field(s) does not exist in the Source but does exist in the Target, then no action is performed. * If the ON 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 MERGE statement


Implementations

Database management systems
PostgreSQL PostgreSQL (, ), also known as Postgres, is a free and open-source relational database management system (RDBMS) emphasizing extensibility and SQL compliance. It was originally named POSTGRES, referring to its origins as a successor to the In ...
,
Oracle Database Oracle Database (commonly referred to as Oracle DBMS, Oracle Autonomous Database, or simply as Oracle) is a multi-model database management system produced and marketed by Oracle Corporation. It is a database commonly used for running online t ...
,
IBM Db2 Db2 is a family of data management products, including database servers, developed by IBM. It initially supported the relational model, but was extended to support object–relational features and non-relational structures like JSON a ...
,
Teradata Teradata Corporation is an American software company that provides cloud database and analytics-related software, products, and services. The company was formed in 1979 in Brentwood, California, as a collaboration between researchers at Caltech ...
, EXASOL,
Firebird Firebird and fire bird may refer to: Mythical birds * Phoenix (mythology), sacred firebird found in the mythologies of many cultures * Bennu, Egyptian firebird * Huma bird, Persian firebird * Firebird (Slavic folklore) Bird species ''Various sp ...
,
CUBRID CUBRID ( "cube-rid") is an open-source SQL-based relational database management system (RDBMS) with object extensions developed by CUBRID Corp. for OLTP. The name CUBRID is a combination of the two words ''cube'' and ''bridge'', ''cube'' standi ...
, H2,
HSQLDB HSQLDB (''Hyper SQL Database'') is a relational database management system written in Java. It has a JDBC driver and supports a large subset of SQL-92, SQL:2008, SQL:2011, and SQL:2016 standards. It offers a fast, small (around 1300 kilobyte ...
, MS SQL,
Vectorwise Actian Vector (formerly known as VectorWise) is an SQL relational database management system designed for high performance in analytical database applications. It published record breaking results on the Transaction Processing Performance Coun ...
and
Apache Derby Apache Derby (previously distributed as IBM Cloudscape) is a relational database management system (RDBMS) developed by the Apache Software Foundation that can be embedded in Java programs and used for online transaction processing. It has a 3. ...
support the standard syntax. Some also add non-standard SQL extensions.


Synonymous

Some database implementations adopted the term "Upsert" (a
portmanteau A portmanteau word, or portmanteau (, ) is a blend of wordsdatabase In computing, a database is an organized collection of data stored and accessed electronically. Small databases can be stored on a file system, while large databases are hosted on computer clusters or cloud storage. The design of databases sp ...
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 PostgreSQL (, ), also known as Postgres, is a free and open-source relational database management system (RDBMS) emphasizing extensibility and SQL compliance. It was originally named POSTGRES, referring to its origins as a successor to the In ...
(v9.5+) and
SQLite SQLite (, ) is a database engine written in the C programming language. It is not a standalone app; rather, it is a library that software developers embed in their apps. As such, it belongs to the family of embedded databases. It is the m ...
(v3.24+). It is also used to abbreviate the "MERGE" equivalent pseudo-code. It is used in
Microsoft Azure SQL Database Microsoft Azure SQL Database (formerly SQL Azure, SQL Server Data Services, SQL Services, and Windows Azure SQL Database) is a managed cloud database ( PaaS) provided as part of Microsoft Azure. A cloud database is a database that runs on a clou ...
.


Other non-standard implementations

Some other database management systems support this, or very similar behavior, through their own, non-standard SQL extensions.
MySQL MySQL () is an open-source relational database management system (RDBMS). Its name is a combination of "My", the name of co-founder Michael Widenius's daughter My, and "SQL", the acronym for Structured Query Language. A relational database o ...
, for example, supports the use of syntax 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, 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, 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 SQLite (, ) is a database engine written in the C programming language. It is not a standalone app; rather, it is a library that software developers embed in their apps. As such, it belongs to the family of embedded databases. It is the m ...
's works similarly. It also supports as an alias for compatibility with MySQL.
Firebird Firebird and fire bird may refer to: Mythical birds * Phoenix (mythology), sacred firebird found in the mythologies of many cultures * Bennu, Egyptian firebird * Huma bird, Persian firebird * Firebird (Slavic folklore) Bird species ''Various sp ...
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 Db2 is a family of data management products, including database servers, developed by IBM. It initially supported the relational model, but was extended to support object–relational features and non-relational structures like JSON a ...
extends the syntax with multiple and clauses, distinguishing them with guards.
Microsoft SQL Server Microsoft SQL Server is a relational database management system developed by Microsoft. As a database server, it is a software product with the primary function of storing and retrieving data as requested by other software applications—which ma ...
extends with supporting guards and also with supporting Left Join via clauses.
PostgreSQL PostgreSQL (, ), also known as Postgres, is a free and open-source relational database management system (RDBMS) emphasizing extensibility and SQL compliance. It was originally named POSTGRES, referring to its origins as a successor to the In ...
supports merge since version 15 but previously supported merging via .
CUBRID CUBRID ( "cube-rid") is an open-source SQL-based relational database management system (RDBMS) with object extensions developed by CUBRID Corp. for OLTP. The name CUBRID is a combination of the two words ''cube'' and ''bridge'', ''cube'' standi ...
supports statement. And supports the use of syntax. It also supports for compatibility with MySQL.
Apache Phoenix Apache Phoenix is an open source, massively parallel, relational database engine supporting OLTP for Hadoop using Apache HBase as its backing store. Phoenix provides a JDBC driver that hides the intricacies of the NoSQL store enabling users to cr ...
supports and UPSERT SELECT syntax. Spark SQL supports and clauses in actions.
Apache Impala Apache Impala is an open source massively parallel processing (MPP) SQL query engine for data stored in a computer cluster running Apache Hadoop. Impala has been described as the open-source equivalent of Google F1, which inspired its developmen ...
supports .


Usage in NoSQL

A similar concept is applied in some
NoSQL A NoSQL (originally referring to "non- SQL" or "non-relational") database provides a mechanism for storage and retrieval of data that is modeled in means other than the tabular relations used in relational databases. Such databases have existed ...
databases. E.g. in
MongoDB MongoDB is a source-available cross-platform document-oriented database program. Classified as a NoSQL database program, MongoDB uses JSON-like documents with optional schemas. MongoDB is developed by MongoDB Inc. and licensed under the Serve ...
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 Redis (; Remote Dictionary Server) is an in-memory data structure store, used as a distributed, in-memory key–value database, cache and message broker, with optional durability. Redis supports different kinds of abstract data structures, su ...
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.


See also

* Join in particular: **
Join (SQL) A join clause in SQL – corresponding to a join operation in relational algebra – combines columns from one or more tables into a new table. Informally, a join stitches two tables and puts on the same row records with matching fields : INNER, ...
**
join (Unix) join is a command in Unix and Unix-like operating systems that merges the lines of two sorted text files based on the presence of a common field. It is similar to the join operator used in relational databases but operating on text files. Overvi ...


References

* *


External links


Oracle 11g Release 2 documentation
on

on


Microsoft SQL Server documentation




{{SQL SQL keywords Articles with example SQL code