H2 Database – Insert

The SQL INSERT statement is used to add new rows of data to a table in the database.

Syntax

Following is the basic syntax of the INSERT INTO statement.

INSERT INTO tableName 
{ [ ( columnName [,...] ) ] 
{ VALUES 
{ ( { DEFAULT | expression } [,...] ) } [,...] | [ DIRECT ] [ SORTED ] select } } | 
{ SET { columnName = { DEFAULT | expression } } [,...] }

Using this INSERT statement, we can insert a new record or new rows into a table. When using the DIRECT clause, the results are directly affected to the target table without any intermediate step. However, while adding values for all the columns of the table, make sure the order of the values is in the same order as the columns in the table.

Example

Let us take an example and try to insert the following given records into the Customer table.

IDNameAgeAddressSalary
1Ramesh32Ahmedabad2000
2Khilan25Delhi1500
3Kaushik23Kota2000
4Chaitali25Mumbai6500
5Hardik27Bhopal8500
6Komal22MP4500
7Muffy24Indore10000

We can get all the given records into the customer table by executing the following commands.

INSERT INTO CUSTOMER VALUES (1, 'Ramesh', 32, 'Ahmedabad', 2000); 
INSERT INTO CUSTOMER VALUES (2, 'Khilan', 25, 'Delhi', 1500); 
INSERT INTO CUSTOMER VALUES (3, 'kaushik', 23, 'Kota', 2000); 
INSERT INTO CUSTOMER VALUES (4, 'Chaitali', 25, 'Mumbai', 6500); 
INSERT INTO CUSTOMER VALUES (5, 'Hardik', 27, 'Bhopal', 8500); 
INSERT INTO CUSTOMER VALUES (6, 'Komal', 22, 'MP', 4500); 
INSERT INTO CUSTOMER VALUES (7, 'Muffy', 24, 'Indore', 1

Leave a Reply