• Breaking News

    Aki13promy - Personal Blog

    Tuesday, January 31, 2023

    Mysql Merge

    MySQL Merge is a MySQL statement which allows us to update records in a specific table on the basis of values that matches from another database table. The MySQL Merge query command is responsible to perform three major query operations at the same time. Suppose, when we apply the CRUD operation commands such as INSERT, DELETE and UPDATE distinctly in our database queries then, we must have to 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 privilegeson the database tables withwhich you will map to a MERGE table.

    Merge Provides three different cases described as follows:

    Case 1:

    The source table may include few rows that are not present in the target table. Therefore, in this case we can use the INSERT command to input rows into the target table that are found in the source table.

    Case 2:

    The target table may include few rows that are not present in the source table. Therefore, in this case we can use the DELETE command to remove rows from the target table that are not found in the source table.

    Case 3:

    In this case, we will find some table rows in the source table containing similar keys as that of target table rows. However, in the non-key table columns these table rows contain different values. Here, we will update the table rows in the target table with the approaching values from the source table.



    Conclusion:

    In MySQL, MERGE is not supported and we apply INSERT…..ON DUPLICATE KEY UPDATE where MySQL performs update on old tables values based on the new ones.


    Hence, for MySQL we can follow the below Syntax:
    1. INSERT IGNORE
    2. INSERT…..ON DUPLICATE KEY UPDATE

    No comments:

    Post a Comment

    Personal Blog

    News

    Travel