INSERT Query in DBMS
INSERT Query in DBMS
On this article, we will learn about INSERT Query in DBMS. A database is not permanent, User can make any changes to the database table by adding, deleting, updating existing records by using DML (data manipulation language ) command.INSERT Query in DBMS
INSERT
is a widely used data manipulation language(DML)command for adding new data to the existing database table- Insert command is used to add one or more rows of data to the database table with specified column values
Syntax for INSERT
INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);
Consider a sample table student, initially as empty.
Simple INSERT Query in DBMS
- In this type of insert, all the values should be provided to all the columns that exist in the table without specifying the column name
- The order of values declared in the values clause should follow the original order of the columns in the table
Example
insert into student values(66'trishaank','computers','24-07-1998',92);O/P
1 row added
STUID | SNAME | BRANCH | DOB | SCORE |
---|---|---|---|---|
66 | Trishaank | computers | 24-07-1998 | 92 |
Inserting data into required columns
In this case, the order of columns declared in insert need not be the same as that of the original table order, only the specified column values are added and null values added to the remaining column values in that tupleExample
insert into student(stuid,sname,branch) values (82,'Srinivas','Computers');O/P
Number of Records: 2
STUID | SNAME | BRANCH | DOB | SCORE |
---|---|---|---|---|
66 | Trishaank | computers | 24-07-1998 | 92 |
82 | Srinivas | computers | null | null |
Dynamic INSERT Query in DBMS (using ampersand &)
In this type of insert, the values are entered by the user at the execution time
Example
insert into student values(&stuid,'&sname','&branch','&dob',&score);Action
enter value for stuid:73 enter value for sname:Prashanth enter value for brance:Physics enter value for dob:17-08-1997 enter value for score:78O/P
Number of Records: 3
STUID | SNAME | BRANCH | DOB | SCORE |
---|---|---|---|---|
66 | Trishaank | computers | 24-07-1998 | 92 |
82 | Srinivas | computers | null | null |
73 | Prashanth | Physics | 17-08-1997 | 78 |
Using select in the INSERT command
Copying all columns of a table
- We can copy the data(rows) from one table and insert into another table by using this combination of select and insert
- Consider the table sample1 with following data and sample2 which is empty as of now with the same structure as sample1, Now we can copy all the rows present in the sample1 table to sample2 table
Number of Records: 5
NAME | AGE |
---|---|
trish | 20 |
prash | 21 |
sanju | 22 |
srinu | 21 |
Chandana | 20 |
INSERT INTO sample2 SELECT * FROM sample1;O/P
select * from sample2;
Number of Records: 5
NAME | AGE |
---|---|
trish | 20 |
prash | 21 |
Sanju | 22 |
srinu | 21 |
Chandana | 20 |
Copying specific columns of a table
You can also copy only required columns from one table to another table using insert into and select combinationINSERT INTO sample2(name) SELECT name FROM sample2;O/P
select * from sample2;
Number of Records: 5
NAME | AGE |
---|---|
trish | null |
prash | null |
Sanju | null |
srinu | null |
Chandana | null |
Copying specific rows from a table
You can add only specific records by filtering the rows based on the condition specified in the where clauseINSERT INTO sample2 SELECT * FROM sample1 WHERE age=20;O/P
select * from sample2Number of Records: 2
NAME | AGE |
---|---|
trish | 20 |
Chandana | 20 |
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment