EDUCBA

EDUCBA

MENUMENU
  • Blog
  • Free Courses
  • All Courses
  • All in One Bundle
  • Login
Home Data Science Data Science Tutorials MySQL Tutorial MySQL Merge

MySQL Merge

By Aanchal SharmaAanchal Sharma

MySQL Merge

Definition of MySQL Merge

MySQL Merge is a MySQL statement that allows us to update records in a specific table based on values that match from another database table. The MySQL Merge query command performs three primary query operations simultaneously. Suppose when we apply the CRUD operation commands such as INSERT, DELETE, and UPDATE distinctly in our database queries. We must build up three different MySQL statements so that the data in the destination table can be modified using the corresponding rows from the database source table.

For this, the users should have SELECT, INSERT, DELETE, and UPDATE privileges on the database tables with which you will map to a MERGE table.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

Syntax of MySQL Merge

The succeeding code of structure defines an elementary syntax for MySQL Merge statement query:

Merge TargetTableName USING SourceTableName
ON Merging_Condition
WHEN MATCHED
THEN Update_Query
WHEN NOT MATCHED
THEN Insert_Query
WHEN NOT MATCHED BY SOURCE
THEN DELETE;

Let us discuss the syntax as follows:

  • Initially, we will define the target and source database tables in the MERGE clause query.
  • Then, we will add the merging condition that decides how the table rows from the table source will match the table rows from the table target. This can be said as similar to a JOIN condition used in the JOIN clause. Usually, we will apply the key columns that can be either UNIQUE Key or PRIMARY Key for matching.
  • After that, this merge condition provides three simple states to query as below:
    • Matched: It defines the table rows that will match the Merge condition, and for this, we need to alter the table rows columns in the table target with the values from the table source. We use the UPDATE statement here.
    • Not Matched: It denotes the table rows from the source table with no equivalent rows present in the target table. In this case, we should supplement the rows from the source table to the defined target table. Remember that NOT MATCHED can also be noted as NOT MATCHED BY TARGET. We will use the INSERT query here.
    • Not Matched by Source: It defines the table rows in the given target table with no equivalent rows in the particular source table. We have to apply the match condition to remove or delete rows from the given target table if we want to coordinate the target table with the records from the source one. We will use the DELETE query here.

How does Merge Work in MySQL?

Assume we have two database tables: source table and target table. We need to match values from the source table and update the target table using these tables. It provides three different cases described as follows:

  • The source table may include a few rows not present in the target table. Therefore, in this case, we can use the INSERT command to input rows into the target table found in the source table.
  • The target table may include a few rows that are not present in the source table. Therefore, we can use the DELETE command to remove rows from the target table not found in the source table.
  • In this case, we will find some table rows in the source table containing similar keys to target table rows. However, these table rows have different values in the non-key table columns. Here, we will update the table rows in the target table with the approaching values from the source table.

A MERGE table is also recognized as an anMRG_MyISAM table in the database, which denotes anassembly of comparableMyISAM tables that can be used as a single table. We can perform only SELECT, UPDATE, and DELETE procedures on this group of tables. But suppose we want to drop the MERGE table; then only the MERGE specifications will be released after dropping. Here, the tables created comprise identical table columns and essential info because we cannot apply MERGE over those tables with columns packed inversely or keys having another order. In MySQL, the merge can be performed with UNION, INSERT, or JOINS on two or more tables to combine the data values based on the UNIQUE or PRIMARY keys.

Examples of MySQL Merge

We will create two tables in the database named Products and Products_Info containing product information.

CREATE TABLE Products (ProductID INT PRIMARY KEY, Product_Name VARCHAR(255) NOT NULL, Cost INT NOT NULL);

Inserting some items as:

INSERT INTO Products(ProductID, Product_Name, Cost) VALUES
(1,'Parle G',100),
(2, 'Maggie', 112),
(3, 'GoodDay Biscuit', 150);

View the table Products:

SELECT * FROM Products;

Output:

 View the table Products -1.1

Again,

CREATE TABLE Products_Info (ProductID INT PRIMARY KEY, Product_Name VARCHAR(255) NOT NULL, Cost INT NOT NULL);

Inserting some items as:

INSERT INTO Products_Info(ProductID, Product_Name, Cost) VALUES
(1,'Parle G', 100),
(2, 'Maggie', 112),
(3, 'GoodDay Biscuit', 115),
(4, 'Nestle Coffee', 125),
(5, 'TATA Tea', 80);

View the table Products:

SELECT * FROM Products_Info;

Output:

View table product-1.2

Now, with the values from the Products_Info table, which is as source table by using the MERGE statement to update data to the Products table as the target table we will use this below query in SQL because the MySQL version may not support MERGE so we will write code for it also to demonstrate:

MERGE Products t
USING Products_Info s
ON (s.ProductID = t.ProductID)
WHEN MATCHED
THEN UPDATE SET
t.Product_Name = s.Product_Name,
t.Cost = s.Cost
WHEN NOT MATCHED BY TARGET
THEN INSERT (ProductID, Product_Name,Cost)
VALUES(s.ProductID,s.Product_Name,s.Cost)
WHEN NOT MATCHED BY SOURCE
THEN DELETE;

Output:

 MySQL Merge-1.3

In MySQL, MERGE is not supported, and we apply INSERT…..ON DUPLICATE KEY UPDATE, where MySQL updates old table values based on the new ones. Hence, for MySQL, we can follow the below queries to combine two tables:

INSERT IGNORE INTO Products SELECT * FROM Products_Info;

Output:

MySQL Merge-1.4

SELECT * FROM Products UNION DISTINCT SELECT * FROM Products_Info;

Output:

 MySQL Merge-1.5

SELECT * FROM Products INNER JOIN Products_Info ON Products.ProductID = Products_Info.ProductID;

Output:

 MySQL Merge-1.6

Conclusion

  • MySQL Merge solves a lot of problems. It helps manage log tables set easily, provides extra speed, can do the searches and repair operations proficiently, and more.
  • The MySQL Merge helps map various files to a single one instantly and implements additional file descriptors. But in Merge tables, you cannot use the REPLACE query.

Recommended Articles

We hope that this EDUCBA information on “MySQL Merge” benefited you. You can view EDUCBA’s recommended articles for more information.

  1. MySQL having
  2. Working with MySQL BLOB
  3. MySQL today()
  4. MySQL Create Function
MICROSOFT POWER BI Training
48+ Hours of HD Videos
8 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
CYBER SECURITY & ETHICAL HACKING Certification Course
89+ Hours of HD Videos
13 Courses
3 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
MICROSOFT AZURE
97+ Hours of HD Videos
15 Courses
12 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
KALI LINUX Certification Course
26+ Hours of HD Videos
6 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
Primary Sidebar
Popular Course in this category
MYSQL Certification Course
 115+ Hours of HD Videos
18 Courses
3 Mock Tests & Quizzes
  Verifiable Certificate of Completion
  Lifetime Access
4.5
Price

View Course
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more