Updated March 4, 2023
Introduction to DB2 INSERT
DB2 INSERT statement helps us to insert the rows or values of a column in a particular table or view that is present in database while using DB2 RDBMS. Maintaining the database requires storing a lot of values in the database. Most of the times, the data is stored in the format of rows and columns in table. In DB2 relational database, data is stored in tables and we can insert the new row values in the tables by using the INSERT statement. We can also use the INSERT statement to add multiple rows in a single query statement in DB2. Further, we can even insert the data of a particular table into some other table by using SELECT and INSERT statements together.
In this article, we will study of INSERT statement in DB2 RDBMS, its syntax, and implementation with the help of certain examples.
Working
In order to insert the values into a particular table, it is necessary that the user should have the insert privileges for that particular table. If we try to insert the row without specifying the value for a certain column, then DB2 will directly internally follow the following rules to give the value to the unspecified columns. If none of the rule is applicable, then an error is thrown in the output –
- If the unspecified value column is an identity column then an auto-incremented value is stored in it.
- If default value is specified in the definition of that column of the table then the default value is inserted when not specified in INSERT statement.
- If the column has the NULLABLE attribute set to true then NULL value is inserted for unspecified value of that column.
- If it is a generated column, then the computed value is inserted if a value is not specified while inserting the row using INSERT statement.
We can insert the data for a single row value using a single INSERT statement and also multiple rows using a single query of INSERT statement accompanied with SELECT statement which gets the data of some other table and inserts in the current table. The bulk insertion of the data into the database can also be done by using the LOAD utility of DB2, creating an application program which can insert the values into DB2 database, copying the data of one table to another. Using the INSERT FOR n (number of rows) ROWS statement can be done for mass insertion of records into the table which can be further accompanied by using host variable arrays.
We can add NULL values, host variables, constants, or default values too into columns while inserting the data using INSERT statement. We can also insert row values in a view. When the data is inserted in the view automatically the corresponding tables from which the view is created are also updated and rows are inserted there too. The order in which we specify the names of the columns in the insert statement should match and correspond to the list of the values that we are trying to insert in the table by using our INSERT statement. Also, note that it is necessary to specify all the columns of the table in the INSERT statement’s column list place or else it will throw an error.
Syntax:
The following is the syntax for using the INSERT statement in DB2 to insert the row values into a particular table –
INSERT INTO name of the table [(list of the column names of that table)]
VALUES (list of the column values of that table)
In the above syntax, the list of column names of the table is the comma-separated names of the columns of table which are enclosed in simple round parenthesis which can be specified in any order while list of column values are the values that are comma-separated and enclosed in parenthesis that are specified in the same order in which the column names are written and which are to be inserted in the database. It is optional to specify the list of the column names of the table. However, if mentioned then the order should match with the order in which we have specified the values to be inserted. In case, if the column name list is not specified the default order is the order in which the columns are defined in the definition of the table and same order is referred while inserting the column values too.
The syntax of the INSERT statement as provided by the IBM for DB2 RDBMS is as shown in the below image –
Example
Let us consider the table named Sales is created by using the following query statement –
CREATE TABLE Sales(
product_id INT GENERATED BY DEFAULT AS IDENTITY NOT NULL,
product_name VARCHAR(150) NOT NULL,
details VARCHAR(255),
sold_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
Let us insert the rows in the table sales. We will insert first row by using following statement –
INSERT INTO Sales(product_name)
VALUES('Moisturizer');
The execution of above query will give following output:
As product_id is auto-generated we don’t need to mention that value while inserting a system will automatically detect the latest value and assign the unique incremented value to that column. If we don’t provide details it will automatically take the NULL value for it as it ha VARCHAR datatype and no NOT NULL constraint. The sold_date column is of TIMESTAMP data type hence it will take current date and time as the default timestamp value. We need to mention product_name compulsorily as it has NOT NULL constraint. We can retrieve the contents of the table by using the SELECT statement in the following way –
SELECT * FROM Sales;
Which turns out to give following output:
We will now insert the product_name and details column values using following insert statement –
INSERT INTO Sales(product_name,details)
VALUES('Shampoo', 'Hair Wash for intense repair and anti-dandruff treatment');
The execution of above query will give following output:
Let us retrieve all the values using the same select statement and check the results which are as follows –
Conclusion
We can make the use of INSERT statement in DB2 to insert the records in the table. We should keep in mind certain restrictions and usage syntax while doing so which are specified above.
Recommended Articles
This is a guide to DB2 INSERT. Here we discuss the Introduction, syntax, How DB2 INSERT works? and examples with code implementation. You may also have a look at the following articles to learn more –