The document discusses the SQL INSERT command which is used to insert data into tables in a database. It describes the basic syntax for inserting a single row with and without specifying column names. It also provides the syntax for inserting multiple rows or selecting data from one table to insert into another table. The UPDATE command is also summarized, providing the syntax for updating column values in a table.
1. Mrs. Sunita M Dol, CSE Department
WIT, Solapur Page 1
4. Interactive data-manipulation language (DML)
The SQL DML includes a query language based on both the relational algebra and the tuple relational calculus. It
includes also commands to
insert tuples into,
delete tuples from, and
modify tuples in the database.
INSERT Command
It is possible to write the INSERT INTO statement in two ways.
Method 1: The first way specifies both the column names and the values to be inserted:
INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);
Method 2: If you are adding values for all the columns of the table, you do not need to specify the
column names in the SQL query. However, make sure the order of the values is in the same order as
the columns in the table. The INSERT INTO syntax would be as follows:
INSERT INTO table_name
VALUES (value1, value2, value3, ...);
You can populate data into a table through select statement over another table provided another table has a set of
fields, which are required to populate first table. Here is the syntax:
INSERT INTO first_table_name [(column1, column2, ... columnN)]
SELECT column1, column2, ...columnN
FROM second_table_name
[WHERE condition];
Insert multiple rows in relation:
Method 1:The syntax for the INSERT ALL statement:
INSERT ALL
INTO mytable (column1, column2,... column_n) VALUES (expr1, expr2,... expr_n)
INTO mytable (column1, column2,... column_n) VALUES (expr1, expr2,... expr_n)
INTO mytable (column1, column2,... column_n) VALUES (expr1, expr2,... expr_n)
SELECT * FROM dual;
Method 2: The syntax for the INSERT statement is
INSERT INTO table_name (column1, column2,...columnN.) VALUES (&column1, &column2, ...
&columnN)
If the type of attribute is CHAR or VARCHAR then use single quote in values e.g. ‘&column1’ else
simply use &column1. For inserting remaining rows, use ‘/’.
The department relation
SQL> insert into department values('Biology','Watson',90000);
2. Mrs. Sunita M Dol, CSE Department
WIT, Solapur Page 2
1 row created.
SQL> insert into department values('Comp.Sci.','Taylor',100000);
1 row created.
SQL> insert into department values('Elec.Eng.','Taylor',85000);
1 row created.
SQL> insert into department values('Finance','Painter',120000);
1 row created.
SQL> insert into department values('History','Painter',50000);
1 row created.
SQL> insert into department values('Music','Packard',80000);
1 row created.
SQL> insert into department values('Physics','Watson',70000);
1 row created.
SQL> select * from department;
DEPT_NAME BUILDING BUDGET
-------------------- -------------------- ----------
Biology Watson 90000
Comp.Sci. Taylor 100000
Elec.Eng. Taylor 85000
Finance Painter 120000
History Painter 50000
Music Packard 80000
Physics Watson 70000
7 rows selected.
The course relation
SQL> insert into course values ('BIO-101','Intro. to Biology','Biology',4 );
3. Mrs. Sunita M Dol, CSE Department
WIT, Solapur Page 3
1 row created.
SQL> insert into course values ('BIO-301','Genetics','Biology',4 );
1 row created.
SQL> insert into course values ('BIO-399','Computational Biology','Biology', 3);
1 row created.
SQL> insert into course values ('CS-101','Intro. to Computer Science','Comp.Sci.',4 );
1 row created.
SQL> insert into course values ('CS-190','Game Design','Comp.Sci.',4 );
1 row created.
SQL> insert into course values ('CS-315','Robotics','Comp.Sci.', 3);
1 row created.
SQL> insert into course values ('CS-319','Image Processing','Comp.Sci.', 3);
1 row created.
SQL> insert into course values ('CS-347','Database System Concepts','Comp.Sci.',3 );
1 row created.
SQL> insert into course values ('EE-181','Intro. to Digital Systems','Elec.Eng.',3 );
1 row created.
SQL> insert into course values ('FIN-201','Investment Banking','Finance',3 );
1 row created.
SQL> insert into course values ('HIS-351','World History','History', 3);
1 row created.
SQL> insert into course values ('MU-199','Music Video Production','Music',3 );
4. Mrs. Sunita M Dol, CSE Department
WIT, Solapur Page 4
1 row created.
SQL> insert into course values ('PHY-101','Physical Principles','Physics',4 );
1 row created.
SQL> select * from course;
COURSE_ TITLE DEPT_NAME
------- -------------------------------------------------- --------------------
CREDITS
----------
BIO-101 Intro. to Biology Biology
4
BIO-301 Genetics Biology
4
BIO-399 Computational Biology Biology
3
COURSE_ TITLE DEPT_NAME
------- -------------------------------------------------- --------------------
CREDITS
----------
CS-101 Intro. to Computer Science Comp.Sci.
4
CS-190 Game Design Comp.Sci.
4
CS-315 Robotics Comp.Sci.
3
COURSE_ TITLE DEPT_NAME
------- -------------------------------------------------- --------------------
CREDITS
----------
CS-319 Image Processing Comp.Sci.
3
5. Mrs. Sunita M Dol, CSE Department
WIT, Solapur Page 5
CS-347 Database System Concepts Comp.Sci.
3
EE-181 Intro. to Digital Systems Elec.Eng.
3
COURSE_ TITLE DEPT_NAME
------- -------------------------------------------------- --------------------
CREDITS
----------
FIN-201 Investment Banking Finance
3
HIS-351 World History History
3
MU-199 Music Video Production Music
3
COURSE_ TITLE DEPT_NAME
------- -------------------------------------------------- --------------------
CREDITS
----------
PHY-101 Physical Principles Physics
4
13 rows selected.
The instructor relation
SQL> insert into instructor values(10101 ,'Srinivasan','Comp.Sci.',65000);
1 row created.
SQL> insert into instructor values(12121,'Wu','Finance',90000);
1 row created.
SQL> insert into instructor values(15151,'Mozart','Music',40000);
1 row created.
6. Mrs. Sunita M Dol, CSE Department
WIT, Solapur Page 6
SQL> insert into instructor values(22222,'Einstein','Physics',95000);
1 row created.
SQL> insert into instructor values(32343,'El Said','History',60000);
1 row created.
SQL> insert into instructor values(33456,'Gold','Physics',87000);
1 row created.
SQL> insert into instructor values(45565,'Katz','Comp.Sci.',75000);
1 row created.
SQL> insert into instructor values(58583,'Califieri','History',62000);
1 row created.
SQL> insert into instructor values(76543,'Singh','Finance',80000);
1 row created.
SQL> insert into instructor values(76766,'Crick','Biology',72000);
1 row created.
SQL> insert into instructor values(83821,'Brandt','Comp.Sci.',92000);
1 row created.
SQL> insert into instructor values(98345,'Kim','Elec.Eng.',80000);
1 row created.
SQL> select * from instructor;
ID NAME DEPT_NAME SALARY
----- -------------------- -------------------- ----------
10101 Srinivasan Comp.Sci. 65000
12121 Wu Finance 90000
15151 Mozart Music 40000
7. Mrs. Sunita M Dol, CSE Department
WIT, Solapur Page 7
22222 Einstein Physics 95000
32343 El Said History 60000
33456 Gold Physics 87000
45565 Katz Comp.Sci. 75000
58583 Califieri History 62000
76543 Singh Finance 80000
76766 Crick Biology 72000
83821 Brandt Comp.Sci. 92000
ID NAME DEPT_NAME SALARY
----- -------------------- -------------------- ----------
98345 Kim Elec.Eng. 80000
12 rows selected.
The section relation
SQL> insert into section values('BIO-101',1,'Summer',2009,'Painter',514,'B');
1 row created.
SQL> insert into section values('BIO-301',1,'Summer',2010,'Painter',514,'A');
1 row created.
SQL> insert into section values('CS-101',1,'Fall',2009,'Packard',101,'H');
1 row created.
SQL> insert into section values('CS-101',1,'Spring',2010,'Packard',101,'F');
1 row created.
SQL> insert into section values('CS-190',1,'Spring',2009,'Taylor',3128,'E');
1 row created.
SQL> insert into section values('CS-190',2,'Spring',2009,'Taylor',3128,'A');
1 row created.
SQL> insert into section values('CS-315',1,'Spring',2010,'Watson',120,'D');
1 row created.
8. Mrs. Sunita M Dol, CSE Department
WIT, Solapur Page 8
SQL> insert into section values('CS-319',1,'Spring',2010,'Watson',100,'B');
1 row created.
SQL> insert into section values('CS-319',2,'Spring',2010,'Taylor',3128,'C');
1 row created.
SQL> insert into section values('CS-347',1,'Fall',2009,'Taylor',3128,'A');
1 row created.
SQL> insert into section values('EE-181',1,'Spring',2009,'Taylor',3128,'C');
1 row created.
SQL> insert into section values('FIN-201',1,'Spring',2010,'Packard',101,'B');
1 row created.
SQL> insert into section values('HIS-351',1,'Spring',2010,'Painter',514,'C');
1 row created.
SQL> insert into section values('MU-199',1,'Spring',2010,'Packard',101,'D');
1 row created.
SQL> insert into section values('PHY-101',1,'Fall',2009,'Watson',100,'A');
1 row created.
SQL> select * from section;
COURSE_I SEC_ID SEMEST YEAR BUILDING ROOM_NU TIME
-------- -------- ------ ---------- --------------- ------- ----
BIO-101 1 Summer 2009 Painter 514 B
BIO-301 1 Summer 2010 Painter 514 A
CS-101 1 Fall 2009 Packard 101 H
CS-101 1 Spring 2010 Packard 101 F
CS-190 1 Spring 2009 Taylor 3128 E
CS-190 2 Spring 2009 Taylor 3128 A
CS-315 1 Spring 2010 Watson 120 D
9. Mrs. Sunita M Dol, CSE Department
WIT, Solapur Page 9
CS-319 1 Spring 2010 Watson 100 B
CS-319 2 Spring 2010 Taylor 3128 C
CS-347 1 Fall 2009 Taylor 3128 A
EE-181 1 Spring 2009 Taylor 3128 C
COURSE_I SEC_ID SEMEST YEAR BUILDING ROOM_NU TIME
-------- -------- ------ ---------- --------------- ------- ----
FIN-201 1 Spring 2010 Packard 101 B
HIS-351 1 Spring 2010 Painter 514 C
MU-199 1 Spring 2010 Packard 101 D
PHY-101 1 Fall 2009 Watson 100 A
15 rows selected.
The teaches relation
SQL> insert into teaches values(10101,'CS-101',1,'Fall',2009);
1 row created.
SQL> insert into teaches values(10101,'CS-315',1,'Spring',2010);
1 row created.
SQL> insert into teaches values(10101,'CS-347',1,'Fall',2009);
1 row created.
SQL> insert into teaches values(12121,'FIN-201',1,'Spring',2010);
1 row created.
SQL> insert into teaches values(15151,'MU-199',1,'Spring',2010);
1 row created.
SQL> insert into teaches values(22222,'PHY-101',1,'Fall',2009);
1 row created.
SQL> insert into teaches values(32343,'HIS-351',1,'Spring',2010);
1 row created.
10. Mrs. Sunita M Dol, CSE Department
WIT, Solapur Page 10
SQL> insert into teaches values(45565,'CS-101',1,'Spring',2010);
1 row created.
SQL> insert into teaches values(45565,'CS-319',1,'Spring',2010);
1 row created.
SQL> insert into teaches values(76766,'BIO-101',1,'Summer',2009);
1 row created.
SQL> insert into teaches values(76766,'BIO-301',1,'Summer',2010);
1 row created.
SQL> insert into teaches values(83821,'CS-190',1,'Spring',2009);
1 row created.
SQL> insert into teaches values(83821,'CS-190',2,'Spring',2009);
1 row created.
SQL> insert into teaches values(83821,'CS-319',2,'Spring',2010);
1 row created.
SQL> insert into teaches values(98345,'EE-181',1,'Spring',2009);
1 row created.
SQL> select * from teaches;
ID COURSE_I SEC_ID SEMEST YEAR
----- -------- -------- ------ ----------
10101 CS-101 1 Fall 2009
10101 CS-315 1 Spring 2010
10101 CS-347 1 Fall 2009
12121 FIN-201 1 Spring 2010
15151 MU-199 1 Spring 2010
22222 PHY-101 1 Fall 2009
32343 HIS-351 1 Spring 2010
45565 CS-101 1 Spring 2010
11. Mrs. Sunita M Dol, CSE Department
WIT, Solapur Page 11
45565 CS-319 1 Spring 2010
76766 BIO-101 1 Summer 2009
76766 BIO-301 1 Summer 2010
ID COURSE_I SEC_ID SEMEST YEAR
----- -------- -------- ------ ----------
83821 CS-190 1 Spring 2009
83821 CS-190 2 Spring 2009
83821 CS-319 2 Spring 2010
98345 EE-181 1 Spring 2009
15 rows selected.
UPDATE Command
The syntax for the UPDATE statement when updating a table in SQL is:
UPDATE table
SET column1 = expression1,
column2 = expression2,
...
[WHERE conditions];
OR
The syntax for the SQL UPDATE statement when updating a table with data from another table is:
UPDATE table1
SET column1 = (SELECT expression1
FROM table2
WHERE conditions)
[WHERE conditions];
OR
The syntax for the SQL UPDATE statement when updating multiple tables (not permitted in Oracle) is:
UPDATE table1, table2, ...
SET column1 = expression1,
column2 = expression2,
...
WHERE table1.column = table2.column
[AND conditions];
SQL> desc student
Name Null? Type
----------------------------------------- -------- ----------------------------
ROLL_NO CHAR(10)
FIRST_NAME CHAR(30)
MIDDLE_NAME CHAR(30)
LAST_NAME CHAR(30)
12. Mrs. Sunita M Dol, CSE Department
WIT, Solapur Page 12
ADDRESS CHAR(50)
CITY CHAR(20)
PINCODE NUMBER(6)
STATE CHAR(20)
MOBILE_NUMBER CHAR(10)
SQL> insert into student (roll_no,first_name,middle_name,last_name,address,city,pincode,state,mobil
e_number) values('&roll_no','&first_name','&middle_name','&last_name','&address','&city',&pincode,'&
state',&mobile_number);
Enter value for roll_no: TECSE-16
Enter value for first_name: Namrata
Enter value for middle_name: M
Enter value for last_name: Bura
Enter value for address: Bhadrawati peth
Enter value for city: Solapur
Enter value for pincode: 413005
Enter value for state: Maharashtra
Enter value for mobile_number: 9421067511
old 1: insert into student (roll_no,first_name,middle_name,last_name,address,city,pincode,state,m
new 1: insert into student (roll_no,first_name,middle_name,last_name,address,city,pincode,state,m
1 row created.
SQL> /
Enter value for roll_no: TECSE-26
Enter value for first_name: Pradnya
Enter value for middle_name: N
Enter value for last_name: Dudam
Enter value for address: Sakhar Peth
Enter value for city: Solapur
Enter value for pincode: 413005
Enter value for state: Maharashtra
Enter value for mobile_number: 7745865343
old 1: insert into student (roll_no,first_name,middle_name,last_name,address,city,pincode,state,m
new 1: insert into student (roll_no,first_name,middle_name,last_name,address,city,pincode,state,m
1 row created.
SQL> select * from student;
ROLL_NO FIRST_NAME MIDDLE_NAME
---------- ------------------------------ ------------------------------
LAST_NAME
------------------------------
13. Mrs. Sunita M Dol, CSE Department
WIT, Solapur Page 13
ADDRESS CITY
-------------------------------------------------- --------------------
PINCODE STATE MOBILE_NUM
---------- -------------------- ----------
TECSE-16 Namrata M
Bura
Bhadrawati peth Solapur
413005 Maharashtra 9421067511
ROLL_NO FIRST_NAME MIDDLE_NAME
---------- ------------------------------ ------------------------------
LAST_NAME
------------------------------
ADDRESS CITY
-------------------------------------------------- --------------------
PINCODE STATE MOBILE_NUM
---------- -------------------- ----------
TECSE-26 Pradnya N
Dudam
Sakhar Peth Solapur
413005 Maharashtra 7745865343
SQL> update student set middle_name='Minesh' where roll_no='TECSE-16';
1 row updated.
SQL> select * from student;
ROLL_NO FIRST_NAME MIDDLE_NAME
---------- ------------------------------ ------------------------------
LAST_NAME
------------------------------
ADDRESS CITY
-------------------------------------------------- --------------------
PINCODE STATE MOBILE_NUM
---------- -------------------- ----------
TECSE-16 Namrata Minesh
Bura
Bhadrawati peth Solapur
413005 Maharashtra 9421067511
14. Mrs. Sunita M Dol, CSE Department
WIT, Solapur Page 14
ROLL_NO FIRST_NAME MIDDLE_NAME
---------- ------------------------------ ------------------------------
LAST_NAME
------------------------------
ADDRESS CITY
-------------------------------------------------- --------------------
PINCODE STATE MOBILE_NUM
---------- -------------------- ----------
TECSE-26 Pradnya N
Dudam
Sakhar Peth Solapur
413005 Maharashtra 7745865343
DELETE Command
The syntax for the DELETE statement in SQL is:
DELETE FROM table
[WHERE conditions];
SQL> select * from student;
ROLL_NO FIRST_NAME MIDDLE_NAME
---------- ------------------------------ ------------------------------
LAST_NAME
------------------------------
ADDRESS CITY
-------------------------------------------------- --------------------
PINCODE STATE MOBILE_NUM
---------- -------------------- ----------
TECSE-16 Namrata Minesh
Bura
Bhadrawati peth Solapur
413005 Maharashtra 9421067511
ROLL_NO FIRST_NAME MIDDLE_NAME
---------- ------------------------------ ------------------------------
LAST_NAME
------------------------------
ADDRESS CITY
-------------------------------------------------- --------------------
PINCODE STATE MOBILE_NUM
---------- -------------------- ----------
TECSE-26 Pradnya N
15. Mrs. Sunita M Dol, CSE Department
WIT, Solapur Page 15
Dudam
Sakhar Peth Solapur
413005 Maharashtra 7745865343
SQL> delete from student where roll_no='TECSE-26';
1 row deleted.
SQL> select * from student;
ROLL_NO FIRST_NAME MIDDLE_NAME
---------- ------------------------------ ------------------------------
LAST_NAME
------------------------------
ADDRESS CITY
-------------------------------------------------- --------------------
PINCODE STATE MOBILE_NUM
---------- -------------------- ----------
TECSE-16 Namrata Minesh
Bura
Bhadrawati peth Solapur
413005 Maharashtra 9421067511
16. Mrs. Sunita M Dol, CSE Department
WIT, Solapur Page 16
Practice Problem Statement
The patient relation
The doctors relation
The department relation
The worker relation
17. Mrs. Sunita M Dol, CSE Department
WIT, Solapur Page 17
The emergency relation
References:
Database system concepts by Abraham Silberschatz, Henry F. Korth, S. Sudarshan (McGraw Hill
International Edition) sixth edition.
Database system concepts by Abraham Silberschatz, Henry F. Korth, S. Sudarshan (McGraw Hill
International Edition) fifth edition.
http://codex.cs.yale.edu/avi/db-book/db4/slide-dir/
http://codex.cs.yale.edu/avi/db-book/db5/slide-dir/
http://codex.cs.yale.edu/avi/db-book/db6/slide-dir/
MOOCs: Database Management System:
https://onlinecourses.nptel.ac.in/noc18_cs15