EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials DB2 Tutorial DB2 INSERT
Secondary Sidebar
DB2 Tutorial
  • DB2 Tutorial
    • What is DB2?
    • DB2? current date
    • DB2 purescale
    • DB2 backup
    • DB2 restore
    • DB2 C Express
    • DB2 Version
    • DB2? Architecture
    • DB2? Data Types
    • DB2? load
    • DB2? order by
    • DB2 date
    • DB2 NVL
    • DB2? update
    • DB2 warehouse
    • DB2 grant
    • DB2 database
    • DB2 VARCHAR
    • DB2? INSERT
    • DB2 LISTAGG
    • DB2 LIKE
    • DB2 TRUNCATE TABLE
    • DB2 LIST TABLES
    • DB2 between
    • DB2? current timestamp
    • DB2? length
    • DB2? bind
    • DB2 limit rows
    • DB2? export
    • DB2 with
    • DB2 Create Table
    • DB2 case statement
    • DB2 CAST
    • DB2 Functions
    • DB2 Date Functions
    • DB2? row_number
    • DB2 trim
    • DB2? Translate
    • DB2 UNION
    • DB2 timestamp
    • DB2? TIMESTAMPDIFF
    • DB2? replace
    • DB2 merge
    • DB2 COALESCE
    • DB2 ISNULL
    • DB2? explain
    • DB2 Join
    • DB2 alter column
    • DB2 rename column
    • DB2? Describe Table
    • DB2? rename table
    • DB2 List Databases
    • DB2 LUW
    • DB2 Query
    • DB2 GROUP BY
    • DB2 TO_DATE

DB2 INSERT

DB2 INSERT

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.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

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 –

syntax

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:

DB2 insert

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:

result

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:

DB2 insert 1

Let us retrieve all the values using the same select statement and check the results which are as follows –

DB2 insert 2

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 –

  1. T-SQL INSERT
  2. MySQL INSERT IGNORE
  3. Insert into Teradata
  4. SQL Bulk Insert
Popular Course in this category
SQL Training Program (10 Courses, 8+ Projects)
  10 Online Courses |  8 Hands-on Projects |  80+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course
Primary Sidebar
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
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
EDUCBA

*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