Explain about insert command in Structured query language in DBMS


Insert command is data manipulation commands, which is used to manipulate data by inserting the information into the tables.

This command is used to add records to a table. While inserting a record using the insert statement, the number of records being entered should match in the columns of the table. In case the number of items being created is less than the number of columns, the field names also need to be specified along with the insert statement.

Insert command

It is used for inserting the records into the table.

The syntax is as follows −

INSERT INTO table-name VALUES(field1, field2,……..)

Example

Given below is an example for the command: INSERT INTO student values(101,’bob’,’CSE’).

create table employee(ename NVARCHAR2(30),department NCHAR2(20));
insert into employee values('pinky’,'CSE');
insert into employee values('priya','ECE');
insert into employee values('hari','EEE');
select * from employee;

Output

You will get the following output −

pinky|CSE
priya|ECE
hari|EEE

Inserting a record that has some null attributes

It requires identifying the fields that actually get data.

The syntax is as follows −

INSERT INTO table-name(field1,field4) VALUES (value1,value2);

Example

Given below is an example of insert command used for inserting a record that has some null attributes −

create table employee(ename varchar2(30),department char(20), age varchar2(30), marks number(30));
INSERT INTO employee(ename,marks) VALUES ('lucky',450);
INSERT INTO employee(ename,marks) VALUES ('bob',300);
select * from employee;

Output

You will get the following output −

lucky|||450
bob|||300

Inserting records from another table

Insert command is used to insert the values which are present in another table.

The syntax is as follows −

INSERT INTO table-name1 SELECT * FROM table-name2;

Example

Given below is an example of insert command for inserting records from another table −

create table employee(ename varchar(30),department varchar(20));
insert into employee values('pinky','CSE');
insert into employee values('priya','ECE');
insert into employee values('hari','EEE');
select * from employee;

create table department(name varchar(30),dname varchar(30));
insert into department select * from employee;
select * from department;

Output

You will get the following output −

pinky|CSE
priya|ECE
hari|EEE
pinky|CSE
priya|ECE
hari|EEE

Updated on: 03-Jul-2021

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements