MYSQL MERGE STATEMENT
Merge statements are available in the Microsoft SQL Server and will be available in PostgreSQL soon with the latest version mostly available in all databases. But whether it exists in MySQL too?🤔
This article is about MySQL Merge Statement. This statement is mostly used when we need to merge two tables based on the conditions. Let’s understand in detail about Merge.
Merge helps us to either update, insert or delete our data based on the 3 actions
(i) source and destination matched.
(ii) source and destination not matched.
(iii) not matched from either source or destination.
One of the most important things, in order to perform a merge on a table user, should have SELECT, INSERT, UPDATE and DELETE privileges on the source and target table.
Syntax for Merge
Merge source_tablename using destination_tablename
on merge_condition
when matched
then update_query
when not matched
then insert_query
when not matched by source
then delete_query;
Unfortunately, Merge is not supported in MySQL we have
(i) INSERT…ON DUPLICATE KEY…