H2 Database - Data Manipulation
- H2 Database - Show
- H2 Database - Merge
- H2 Database - Explain
- H2 Database - Call
- H2 Database - Backup
- H2 Database - Delete
- H2 Database - Update
- H2 Database - Insert
- H2 Database - Select
H2 Database - Data Definition
- H2 Database - JDBC Connection
- H2 Database - Rollback
- H2 Database - Savepoint
- H2 Database - Grant
- H2 Database - Commit
- H2 Database - Truncate
- H2 Database - Drop
- H2 Database - Alter
- H2 Database - Create
H2 Database Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
H2 Database - Merge
MERGE command is used to update the existing rows and insert new rows into a table. The primary key column plays an important role while using this command; it is used to find the row.
Syntax
Following is the generic syntax of the MERGE command.
MERGE INTO tableName [ ( columnName [,...] ) ] [ KEY ( columnName [,...] ) ] { VALUES { ( { DEFAULT | expression } [,...] ) } [,...] | select }
In the above syntax, the KEY clause is used to specify the primary key column name. Along with VALUES clause, we can use primitive values to insert or we can retrieve and store another table values into this table using the select command.
Example
In this example, let us try to add a new record into Customers table. Following are the details of the new record in the table.
Column Name | Value |
---|---|
ID | 8 |
NAME | Lokesh |
AGE | 32 |
ADDRESS | Hyderabad |
SALARY | 2500 |
Using the following query, let us insert the given record into the H2 database query.
MERGE INTO CUSTOMER KEY (ID) VALUES (8, Lokesh , 32, Hyderabad , 2500);
The above query produces the following output.
Update count: 1
Let us verify the records of the Customer table by executing the following query.
SELECT * FROM CUSTOMER;
The above query produces the following output.
ID | Name | Age | Address | Salary |
---|---|---|---|---|
1 | Ramesh | 32 | Ahmedabad | 2000 |
2 | Khilan | 25 | Delhi | 1500 |
3 | Kaushik | 23 | Kota | 2000 |
4 | Chaitap | 25 | Mumbai | 6500 |
5 | Hardik | 27 | Bhopal | 8500 |
6 | Komal | 22 | MP | 4500 |
7 | Muffy | 24 | Indore | 10000 |
8 | Lokesh | 32 | Hyderabad | 2500 |
Now let us try to update the record using the Merge command. Following are the details of the record to be updated.
Column Name | Value |
---|---|
ID | 8 |
NAME | Loki |
AGE | 32 |
ADDRESS | Hyderabad |
SALARY | 3000 |
Use the following query to insert the given record into the H2 database query.
MERGE INTO CUSTOMER KEY (ID) VALUES (8, Loki , 32, Hyderabad , 3000);
The above query produces the following output.
Update count: 1
Let us verify the records of the Customer table by executing the following query.
SELECT * FROM CUSTOMER;
The above query produces the following output −
ID | Name | Age | Address | Salary |
---|---|---|---|---|
1 | Ramesh | 32 | Ahmedabad | 2000 |
2 | Khilan | 25 | Delhi | 1500 |
3 | Kaushik | 23 | Kota | 2000 |
4 | Chaitap | 25 | Mumbai | 6500 |
5 | Hardik | 27 | Bhopal | 8500 |
6 | Komal | 22 | MP | 4500 |
7 | Muffy | 24 | Indore | 10000 |
8 | Loki | 32 | Hyderabad | 3000 |