0% found this document useful (0 votes)
3 views67 pages

Dbms Lab Manual (2022 23) - 1

The document is a lab manual for a Database Management System course at Nrupathunga University, detailing course structure, assessment methods, and practical exercises. It covers various SQL commands, including DDL and DML operations, as well as PL/SQL functions and nested queries. The manual includes examples and exercises for creating and manipulating databases, along with guidance from faculty members.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views67 pages

Dbms Lab Manual (2022 23) - 1

The document is a lab manual for a Database Management System course at Nrupathunga University, detailing course structure, assessment methods, and practical exercises. It covers various SQL commands, including DDL and DML operations, as well as PL/SQL functions and nested queries. The manual includes examples and exercises for creating and manipulating databases, along with guidance from faculty members.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 67

DATABASE MANAGEMENT

SYSTEM

LAB MANUAL

1 NRUPATHUNGA UNIVERSITY
NRUPATHUNGAUNIVERSITY
DEPARTMENT OF COMPTER SCIENCE
MODELCOURSECONTENTFORBCA,SEMESTERIII

Course Title: Database Management System CourseCode:21BCA37L

Course Credits:=3+02 Duration of SEE/Exam:03hrs

Formative Assessment Marks:40 Practical Assessment:25

Summative Assessment Marks:6 Sem End Exams Practical:25

Index
S.NO NAME PAGE
PART – A
1 DDL Commands 5
2 DML Commands 8
3 Implement Nested Queries 11
4 Implement Join Operation 15
5 Library Database 17
6 Salary Database 19
7 Views for a Employee Table 22
8 Employee Database 25
9 Inner join or Equi join operation using Purchase and 28
item table
10 Cross join operation using Person &Job Table 30
PART – B
1 Insurance Database 33
2 Bank Database 44
3 Student Enrollment Database 37
4 Branch –customer loan database 49
5 Sales Person Database 50
6 String Functions 57

2 NRUPATHUNGA UNIVERSITY
7 Date Functions 54
PL/SQL
1 Largest of THREE Numbers 60
2 Generate Reverse for Given Number 60
3 Find Factorial 60
4 Prime or Not-Prime 61
5 Fibonacci Series 61
6 Inserting a ROW into a Table 62
7 Handling a Predefined Exception 62
8 Creating PROCEDURE for Adding TWO Numbers 62
9 Creating PROCEDURE to Check for Leap-Year 63
PL/SQL – Function Declaring
1 Viewing Specific Employee Salary 64
2 Increasing Salary of a Employee 65

Manual Credits: with the guidance of

Done by: Sangamesh , Asha N ma’am,

Sudhanva Gayatri v ma’am

With support:

1.Jayalaxmi H

2.Gouri M

3.Monusha S

[ BCA 2022-23 BATCH STUDENTS]


3 NRUPATHUNGA UNIVERSITY
4 NRUPATHUNGA UNIVERSITY
PART-A
1. Execute DDL Commands

1. Create
2.Alter
3.Truncate
4.Drop

1. Create:the command is used to create a table structures in sql.

Syntax: create table table_name


{
Column_1datatype,
Column_2datatype,
Column_3datatype,

}
Example:Create a Table structure to store the student information college.

Query:

Create table student_information(college_id number(6),student_name


varchar(30),college_name varchar(20));

Table created

Example:To view the table definition or structure of the table

Query:

Desc student_information;

5 NRUPATHUNGA UNIVERSITY
Object Type TABLE Object STUDENT_INFORMATION
Data Leng Precisi Scal Primary Nullab De fau Comme
Table Column Type th on e Key le lt nt
STUDENT_INFORMATI
COLLEGE_ID Number - 6 0 - - -
ON
STUDENT_NA
Varchar2 30 - - - - -
ME
COLLEGE_NA
Varchar2 20 - - - - -
ME

1 -3

2. Alter: The command is used to alter the structure definition of the table like
add,delete or change columns In the existing table structure.

Syntax: Alter Table table_name

1. Example: Add a new column to the existing table definition

Query:

Alter table student_information add CGPA number;

Table altered
Object Type TABLE Object STUDENT_INFORMATION
D ata L engt Precisio Scal Prim ary Null abl D efau Comm e
T able Colum n T ype h n e K ey e lt nt

STUDENT_INFORMATI
COLLEGE_ID Number - 6 0 - - -
ON
STUDENT_NA
Varchar2 30 - - - - -
ME
COLLEGE_NA
Varchar2 20 - - - - -
ME
CGPA Number - - - - - -

2. Example:alter table student_information modify college_id varchar(6);

Query: alter table student_information modify college_id varchar(6);

Table altered

6 NRUPATHUNGA UNIVERSITY
Object Type TABLE Object STUDENT_INFORMATION
Da ta Lengt P recisio Scal P ri mary N ullabl D efa ul Co mmen
Tabl e Col umn Type h n e Key e t t

STUDENT_INFORMATIO
COLLEGE_ID Varchar2 6 - - - - -
N
STUDENT_NAM
Varchar2 30 - - - - -
E
COLLEGE_NAM
Varchar2 20 - - - - -
E
CGPA Number - - - - - -

1 -4

3. Truncate:The command is used to delete all the rows in the table ,but weneed
to have data or rows of Data in the table. To do so we need to use Insert
command. Also the table row value shave to be displayed using
select command.

Syntax: Insert

Insert into table values(‘data1’,’data2’,’data3’…’data n’);

Syntax: Select

Select*from table_name;

Syntax: Truncate

Truncate table table_name;

Example1:
Insert into student_information values('NRU123','deepa','NRUPATHUNGA',5);
1 row(s) inserted

Insert into student_information values('NRU124','Veena','NRUPATHUNGA',3);

1 row(s) inserted

7 NRUPATHUNGA UNIVERSITY
Insert into student_information values('NRU125','Venu','NRUPATHUNGA',2);

1 row(s) inserted

To display the inserted rows

Example2 :Select*from student_information


C O LLEG E_ ID STUD ENT_ N AME C O LLEG E_ N AME C GP A

NRU123 deepa NRUPATHUNGA 5


NRU124 Veena NRUPATHUNGA 3
NRU125 Venu NRUPATHUNGA 2
3 rows returned in 0.14
CSV Export
seconds

Example3:Truncate table student_information;

Table truncated.

4. DROP:The command is used to remove the table definition or structure of the


table.

Synatx: drop table table_name;

Example: drop table student_information;

Tabledropped.

**************************

2. Execute DML Commands

Create a database with relations:

Student(Name,Number,Class,Major)
Course(CourseName,CourseNumber,Hours,Dept)

Query 1: Insert atleast 5 rows in each table


Query 2: Change class of student ‘Smith’ to 2

8 NRUPATHUNGA UNIVERSITY
Query 3: Remove the record for the student whose name is ‘Smith’.
Query 4: Add a new course(‘KnowledgeEngg’,’CS4390’,’3’,’cs’).
Query1:
Create table student(name varchar(20),regno int,class varchar(2),major
varchar(20));

Desc student;

Object Type TABLE Object STUDENT34


Colum Data Lengt Precisio Scal Primary Nullabl Defaul Comme
Table n Type h n e Key e t nt
STUDENT34 NAME Varchar2 20 - - - - -
REGN
Number - - 0 - - -
O
CLASS Varchar2 2 - - - - -
MAJOR Varchar2 20 - - - - -
1 -4

Create table course(coursename varchar(20) ,coursenumber varchar(5) ,hours


integer,dept varchar(20));

desc course;

Object Type TABLE Object COURSE


Data Lengt Precisio Scal Primary Nullab Defau Comme
Table Column Type h n e Key le lt nt
COURSE
COURSE Varchar2 20 - - - - -
NAME
COURSE
Varchar2 5 - - - - -
NUMBER
HOURS Number - - 0 - - -
DEPT Varchar2 20 - - - - -

1 -4

Query2:

Insert into student values('asha','121',2,'physics');


Insert into student values('smitha','122',1,'cs');
Insert into student values('veena','123',1,'maths');
Insert into student values('mona','124',3,'maths');
Insert into student values('neha','125',1,'physics');
9 NRUPATHUNGA UNIVERSITY
select*from student;

NAME REGNO CLASS MAJOR


asha 121 2 physics

smitha 122 1 cs

veena 123 1 maths

mona 124 3 maths

neha 125 1 physics

Insert into course values('optics','p401','4','physics');

Insert into course values('programminginc','c202','4','compsc');

Insert into course values('linearalgebra','m105','6','mathematics');

Insert into course values('co-ordinategeometry','m110','6','mathematics');

Insert into course values('magnetism','p208','3','physics');

select*from course;

COURSENAME COURSENUMBER HOURS DEPT


Optics p401 4 physics

Programming in c c202 4 Comp sc

Linear algebra m105 6 mathematics

co-ordinate geometry m110 6 mathematics

Magnetism p208 3 physics

Query3:

Update student set class='2';

10 NRUPATHUNGA UNIVERSITY
select*from student;

NAME REGNO CLASS MAJOR


asha 121 2 physics

smitha 122 2 cs
veena 123 2 maths
mona 124 2 maths
neha 125 2 physics

Query4:

Delete from student where name='smitha';

select*from student;
NAME REGNO CLASS MAJOR
Asha 121 2 physics
Veena 123 2 maths
Mona 124 2 maths
Neha 125 2 physics
4 rows returned in 0.00 CSV
seconds Export

Query5:

Insert into course values('knowledgeengg','cs43',3,'cs');

select*from course;

COURSENAME COURSENUMBER HOURS DEPT


Optics p401 4 physics

Programming in c c202 4 Comp sc

Linear algebra m105 6 mathematics


co-ordinate
m110 6 mathematics
geometry

11 NRUPATHUNGA UNIVERSITY
magnetism p208 3 physics

Knowledge eng g cs43 3 cs

**************************
3. Implement the Nested Queires

1. Create a the emp_db table

2. Enter 5 tuples into the emp_db database

3.Display all the tuples in the database

4. Select the employees where salary greater than 4500

5. Create a table employee backup with same attribute as emp_db

6.Copy the values of emp_db to emp_bkp

7. update emp_db from emp_bkp to increase the salary by 25% where age
greater than 30

8. Delete that employee whose age is greater than 40

Create Employee table

Query1:

1.create table emp_db1(id number,name varchar(10),age number(2),address


varchar(20),salary number);

Table created

Desc emp_db1;

Object Type TABLE Object EMP_DB1


Table Column Data Lengt Precisio Scal Primary Null Defaul Comme

12 NRUPATHUNGA UNIVERSITY
Type h n e Key able t nt
EMP_DB1 ID Number - - - - - -
NAME Varchar2 10 - - - - -
AGE Number - 2 0 - - -
ADDRES
Varchar2 20 - - - - -
S
SALARY Number - - - - - -
1 -5

Query2:
Insert into emp_db1 values(123,'deepa',34,'blr',4500);
Insert into emp_db1 values(124,'deepa1',34,'blr',3500);
Insert into emp_db1 values(125,'deepa1',34,'blr',7500);
Insert into emp_db1 values(125,'veena1',34,'blr',8500);
Insert into emp_db1 values(125,'veena2',34,'blr',1500);

Query3:
select*from emp_db1;

ID NAME AGE ADDRESS SALARY


123 Deepa 34 blr 4500
124 deepa1 34 blr 3500
125 deepa1 34 blr 7500
125 veena1 34 blr 8500
125 veena2 34 blr 1500

Query 4:

select*from emp_db1 where id in (select id from emp_db1 where salary>=4500)

ID NAME AGE ADDRESS SALARY


123 deepa 34 blr 4500

125 veena1 34 blr 8500


125 deepa1 34 blr 7500

Query 5:

13 NRUPATHUNGA UNIVERSITY
Create table emp_bkp(id number,name varchar(10),age number(2),address
varchar(20),salary number);

Table created

Desc emp_bkp;

Object Type TABLE Object EMP_BKP


Data Lengt Precisio Scal Primary Nullabl Defaul Commen
Table Column Type h n e Key e t t
EMP_BKP ID Number - - - - - -
NAME Varchar2 10 - - - - -
AGE Number - 2 0 - - -
ADDRES
Varchar2 20 - - - - -
S
SALARY Number - - - - - -

1 -5

Query6:

Insert into emp_bkp select*from emp_db1 where id in(select id from emp_db1);

select*from emp_bkp;

ID NAME AGE ADDRESS SALARY


123 deepa 34 blr 4500
124 deepa1 34 blr 3500
125 veena2 34 blr 1500
125 veena1 34 blr 8500
125 deepa1 34 blr 7500

Query7:

Update emp_db1 set salary=salary*0.25 where age in(select age from emp_bkp
where age>=30);

1 row updated

14 NRUPATHUNGA UNIVERSITY
select*from emp_db1;

Query8:

Delete from emp_db1 where age in(select age from emp_bkp where age>=40)

0 row(s) deleted

select*from emp_db1;

ID NAME AGE ADDRESS SALARY

123 deepa 34 blr 4500

124 deepa1 34 blr 3500

125 deepa1 34 blr 7500

125 veena1 34 blr 8500

125 veena2 34 blr 1500

**************************
4. Implement Join operations in SQL

1. Create two tables P and Q as


P(idnumber,namestring);
Q(idnumber,namestring);

2. Insert rows into tables P and Q.

3. Display all records of both tables P and Q.

4. Dispaly only the records that exist in both tables P and Q.

5.Display all records that are in table P but not in table Q.

15 NRUPATHUNGA UNIVERSITY
Query 1:

Create table p(id number primary key,name varchar(20));

Desc p;
Object Type TABLE Object P
Table Column Data Type Length Precision Scale Primary Key Nullable Default Comment
P ID Number - - - 1 - - -
NAME Varchar2 20 - - - - -
1 -2

Create table q(id number primary key,name varchar(20));

Desc q;

Object Type TABLE Object Q


Table Column Data Type Length Precision Scale Primary Key Nullable Default Comment
Q ID Number - - - 1 - - -
NAME Varchar2 20 - - - - -

1 -2

Query 2:
Insert into p values('101','asha');
Insert into p values('103','smitha');
Insert into p values('104','lavanya');
Insert into p values('107','veena');
Insert into p values('110','divya');
Insert into p values('112','sowmya');

select*from p;
ID NAME
101 asha

103 Smitha

16 NRUPATHUNGA UNIVERSITY
104 Lavanya

107 Veena

110 Divya

112 Sowmya

Insert into q values(103,'smitha');


Insert into q values(104,'lavanya');
Insert into q values(106,'bharathi');
Insert into q values(110,'divya');

select*from q;

ID NAME
103 Smitha

104 Lavanya

106 Bharathi

110 Divya

Query 3:

select*from p UNION select*from q;


ID NAME
101 Asha
103 Smitha
104 Lavanya
106 Bharathi
107 Veena
110 Divya
112 Sowmya

17 NRUPATHUNGA UNIVERSITY
Query 4:

select*from p INTERSECT select*from q;

ID NAME
103 smitha
104 lavanya
110 divya

Query 5:

select*from p MINUS select*from q;

ID NAME

101 Asha

107 veena

112 sowmya

**************************
5. Library Database

LIBRARY(bookid:int,title:string,author:string,publisher:string,Pubyear:in
t,Price:real)

i) Create the above table.


ii) Enter the five tuples into the table
iii)Display all the tuples in library table.
iv) Display the different publishers from the list.
v) Arrange the tuples in the alphabetical order of the book titles.

18 NRUPATHUNGA UNIVERSITY
vi) List the details of all the books whose price ranges between Rs.100
and Rs.300

i)Answer
1. create table library(bookid int primary key,title varchar(10),author
varchar(10),publisher varchar(10),year_pub int,price decimal(6,2));

Desc library;
Object Type TABLE Object LIBRARY1
Data Lengt Precisio Scal Primary Nullab Defau Comme
Table Column Type h n e Key le lt nt
LIBRARY1 BOOK_ID Number - - 0 1 - - -
TITLE Varchar2 10 - - - - -
AUTHOR Varchar2 10 - - - - -
PUBLISH
Varchar2 10 - - - - -
ER
YEAR_PU
Number - - 0 - - -
B
PRICE Number - 6 2 - - -
1–6

2. INSERT INTO library values(111,'maths','atre','himalaya',2009,250.00);

INSERT INTO library values(112,'physics','asahu','subhas',2010,300.00);

INSERT INTO library values(113,'compact,'rama','subhas',2010,220.00);

INSERT INTO library values(114,'chemistry','bosco','himalaya',2009,275.00);

INSERT INTO library values(115,'physics','ranganath','mes',2009,250.00);

3. Select*from Library;

BOOK_ID TITLE AUTHOR PUBLISHER YEAR_PUB PRICE


111 maths atre himalaya 2009 250
112 physics asahu subhas 2010 300
113 compact rama subhas 2010 220

19 NRUPATHUNGA UNIVERSITY
114 chemistry bosco himalaya 2009 275
115 physics ranganath mes 2009 250

4. select distinct publisher from library;


PUBLISHER
mes
subhas
himalaya

5. select*from library order by title;


BOOK_ID TITLE AUTHOR PUBLISHER YEAR_PUB PRICE
114 chemistry bosco himalaya 2009 275
113 compact rama subhas 2010 220
111 maths atre himalaya 2009 250
115 physics ranganath mes 2009 250
112 physics asahu subhas 2010 300

6. select*from library where price between 100.00 and 250.00;


BOOK_ID TITLE AUTHOR PUBLISHER YEAR_PUB PRICE
111 maths atre himalaya 2009 250
113 compact rama subhas 2010 220
115 physics ranganath mes 2009 250
3 rows returned in 0.00
CSV Export
seconds

**********************************

6.Salary Database with following attributes.

EMPSALARY(EmpCode:int,EmpName:String,DOB:Date,Department:Str
ing,Salary:Real)

i)Create the above table.


i) Enter the five tuples into the table
ii) Display all the number of employees working in each department.
iii)Find the sum of the salaries of all employees.
iv) Find the sum and average of the salaries of employees of a particular
department

20 NRUPATHUNGA UNIVERSITY
v) Find the least and highest salaries that an employee draws.

Answers
1. create table EmpSalary(EmpCode int primary key,EmpName
varchar(10),DOB Date,Dept varchar(10),salary decimal(10,2));

Desc EmpSalary;

Object Type TABLE Object EMPSALARY

Data Lengt Precisio Scal Primary Nullabl Defaul Commen


Table Column Type h n e Key e t t

EMPSALARY EMPCODE Number - - 0 1 - - -

EMPNAM
Varchar2 10 - - - - -
E

DOB Date 7 - - - - -

DEPT Varchar2 10 - - - - -

SALARY Number - 10 2 - - -

1–5

2.
Insert into EmpSalary values(101,'mathew','02-jan-1991','hr',20000);
Insert into empsalary values(102,'srinivas','12-june-1988','production',10000);
Insert into empsalary values(103,'vikram','02-feb-1989','computer',15000);
Insert into empsalary values(104,'manisha','04-dec-1992','hr',20000);
Insert into empsalary values(105,'vinay','17-sep-1992','finance',30000);

select * from empsalary;


EM PCODE EM PNAM E DOB DEPT SALARY
101 mathew 02-JAN-91 hr 20000
102 srinivas 12-JUN-88 production 10000
103 vikram 02-FEB-89 computer 15000
104 manisha 04-DEC-92 hr 20000
105 vinay 17-SEP-92 finance 30000

21 NRUPATHUNGA UNIVERSITY
3. select dept,count(empcode)no_of_emps from empsalary group by
dept;
DEPT NO_OF_EMPS
Hr 2
finance 1
production 1
computer 1

4. select sum(salary)sum_salary from empsalary;


SU M_SAL AR Y

95000

5. select dept,sum (salary)sum_of_sal,avg(salary)avg_of_sal from


empsalary group by dept Having dept='hr';
D EPT SU M_OF_ SAL AVG_OF_ SAL

Hr 40000 20000

6. select max(salary)max_sal from empsalary;


MAX _SAL

30000

7. select min(salary)min_sal from empsalary;


MI N _SAL

10000

8. select dept,sum(salary)dept_sum_sal from empsalary group by dept;


D EPT D EPT _SU M _SA L

Hr 40000
finance 30000
production 10000
computer 15000

22 NRUPATHUNGA UNIVERSITY
9. update empsalary set salary=salary+1000 where dept='computer';

select*from empsalary;
EMPCODE EMPNAME DOB DEPT SALARY
101 mathew 02-JAN-91 hr 20000
102 srinivas 12-JUN-88 production 10000
103 vikram 02-FEB-89 computer 16000
104 manisha 04-DEC-92 hr 20000
105 vinay 17-SEP-92 finance 30000

************************

7. View For A Employee Table.

1. Create employee_db table with the following attributes.


Empcode,Empname,DOB,Dept,Salary

2. Insert atleast 5 tuples into the employee_db database.

3.Display all the tuples in the employee_dbdatabase.

4.Create a salary_db views for the following attributes.


Empname,dept and salary

5.Display the tuples in the salary_db.

6. Update the salary of the employee_db by 20%.

7. Display the tuples of employee_db and salary_db to observe the changes


made to employee_db.

Query 1:
Create employee_db table with the following attributes.
Empcode,Empname,DOB,Dept,Salary

23 NRUPATHUNGA UNIVERSITY
Create table employee_db(Empcode varchar(10),Empname varchar(20),DOB
date,Dept varchar(10),Salary decimal(10,2));

desc employee_db;
Object Type TABLE Object EMPLOYEE_DB

Table Column Data Type Length Precision Scale Primary Key Nullable Default Comment

EMPLOYEE_DB EMPCODE Varchar2 10 - - - - -

EMPNAME Varchar2 20 - - - - -

DOB Date 7 - - - - -

DEPT Varchar2 10 - - - - -

SALARY Number - 10 2 - - -

1–5

2. Insert atleast 5 tuples into the employee_db database.

Insert into employee_db values('emp123','venu','12-dec-


200','marketing',12000.20);

Insert into employee_db values('emp127','venugopal','12-jan-


2001','production',15000.50);

Insert into employee_db values('emp150','manigopal','24-jul-


2004','finance',20000.50);

Insert into employee_db values('emp180','neerja','18-jan-2005','sales',10000.50);

Insert into employee_db values('emp200','rashmi','02-aug-


2001','production',15000.50);

3. Display all the tuples in the employee_db database.

Select *from employee_db;


24 NRUPATHUNGA UNIVERSITY
EMPCODE EMPNAME DOB DEPT SALARY
emp123 venu 12-DEC-00 marketing 12000.2
emp127 venugopal 12-JAN-01 production 15000.5
emp150 manigopal 24-JUL-04 finance 20000.5
emp180 neerja 18-JAN-05 sales 10000.5
emp200 rashmi 02-AUG-01 production 15000.5

4. Create a salary_db views for the following attributes.


Empname,dept and salary

Create view salary_db as select empname,dept,salary from employee_db;

View created.

5. Displaythetuplesinthesalary_db.

select*from salary_db;
EMPN AME D EPT SAL ARY

venu marketing 12000.2

venugopal production 15000.5

manigopal finance 20000.5

neerja sales 10000.5

rashmi production 15000.5

6. Updatethesalaryoftheemployee_dbby20%.

Update employee_db set salary=salary*20/100;

5 rows updated

7. Display the tuples of employee_db and salary_db to observe the


changes made to employee_db

select*from employee_db;

25 NRUPATHUNGA UNIVERSITY
EMPCODE EMPNAME DOB DEPT SALARY
emp123 venu 12-DEC-00 marketing 2400.04
emp127 venugopal 12-JAN-01 production 3000.1
emp150 manigopal 24-JUL-04 finance 4000.1
emp180 neerja 18-JAN-05 sales 2000.1
emp200 rashmi 02-AUG-01 production 3000.1

select*from salary_db;

EMP_NAME DEPT SALARY


yunus marketing 2400.04
mahesh production 3000.1
rakesh finance 4000.1
jameer sales 2000.1
shivu production 3000.1

***************************

8. employee database.

1. Create a table employee with the following fields:


EMPLOYEE(empid,empname,sex,age);

2. insert 5 rows into the table

3. Display the names of all employees whose age is greater


than or equal to 30.

4. Display the names of all female whose age greater than 25.

5. Display the names of the employees whose age ranges between 22


and 25.

6. Display the details of employees whose names start with alphabet V.

26 NRUPATHUNGA UNIVERSITY
Query 1:
Create table employee(empid varchar(5) primary key,empname varchar(20),sex
varchar(6),age int);

Desc employee;

Object Type TABLE Object EMPLOYEE

Data Lengt Precisio Scal Primary Nullabl Defaul Commen


Table Column Type h n e Key e t t

EMPLOYEE EMPID Varchar2 5 - - 1 - - -

EMPNAM
Varchar2 20 - - - - -
E

SEX Varchar2 6 - - - - -

AGE Number - - 0 - - -

1-4

Query2:

Insert into employee values('e01','mohan','male',40);


Insert into employee values('e05','veena','female',48);
Insert into employee values('e07','kiran','male',28);
Insert into employee values('e08','geetha','female',22);
Insert into employee values('e03','monisha','female',24);
Insert into employee values('e04','vijay','male',33);

select*from employee;

EMPID EMPNAME SEX AGE


e01 mohan male 40
e05 veena female 48
e07 kiran male 28
e08 geetha female 22

27 NRUPATHUNGA UNIVERSITY
e03 monisha female 24
e04 vijay male 33

Query3:

Select empname,age from employee where age>=30;


EMPNAME AGE
mohan 40
veena 48
vijay 33

Query4:

Select empname,age from employee where age>25 and sex='female';


EMPNAME AGE
veena 48

Query5:

Select empname,age from employee where age between 22 and 25;

EMPNAME AGE
geetha 22
monisha 24

Query6:

Select empname from employee where empname like'v%';


EMPNAME
veena
vijay

********************************

28 NRUPATHUNGA UNIVERSITY
9. Inner join or equi join operations using purchase & item
table.
1. Create the following tables.
Item(itemcode integer,itemname varchar(20),price decimal(10,2);
Purchase(itemcode integer,quantity integer);
2. insert atleast 5 rows into each table.

3. Demonstrate INNERJOIN or EQUI JOIN operation on these tables.

1. create table item(itemcode int primary key,itemname varchar(10),price


decimal(10,2));

Desc item;

Object Type TABLE Object ITEM

Table Column Data Type Length Precision Scale Primary Key Nullable Default Comment

ITEM ITEMCODE Number - - 0 1 - - -

ITEMNAME Varchar2 10 - - - - -

PRICE Number - 10 2 - - -

1-3

Create table purchase(itemcode int,quantity int);

Desc purchase;

Object Type TABLE Object PURCHASE

Data Lengt Precisio Scal Primary Nullabl Defaul Commen


Table Column Type h n e Key e t t

ITEMCOD
PURCHASE Number - - 0 - - -
E

QUANTITY Number - - 0 - - -

1-2

29 NRUPATHUNGA UNIVERSITY
2. insert into item values(1001,'pen',7.50);
Insert into item values(1002,'pencil',3.50);
Insert into item values(1003,'calculator',300);
Insert into item values(1004,'book',20);
Insert into item values(1005,'stapler',30);

select*from item;

ITEMCODE ITEMNAME PRICE


1001 pen 7.5
1002 pencil 3.5
1003 calculator 300
1004 book 20
1005 stapler 30

insert into purchase values(1001,7);


insert into purchase values(1002,20);
insert into purchase values(1003,2);
insert into purchase values(1004,5);
insert into purchase values(1005,2);

select*from purchase;

ITEMCODE QUANTITY
1001 7
1002 20
1003 2
1004 5
1005 2

3. select itemname from item inner join purchase on


item.itemcode=purchase.itemcode;
ITEMNAME
pen
pencil
calculator
book

30 NRUPATHUNGA UNIVERSITY
stapler

*****************************

10.cross join operation using person and job table.

1. Create the following two tables Person and Job.

Person(pid varchar(5),name varchar(20));


Job(jobid varchar(3));

2. Insertrowsintothetwotables.

3. Demonstrate the set operation Cartesian Product (CrossJoin) on


these two tables.

Query 1: Create table person(pid varchar(5),name varchar(20));

Desc person;
Object Type TABLE Object PERSON

Table Column Data Type Length Precision Scale Primary Key Nullable Default Comment

PERSON PID Varchar2 5 - - - - -

NAME Varchar2 20 - - - - -

1-2

Create table job(jobid varchar(3));

Desc job;

Object Type TABLE Object JOB

31 NRUPATHUNGA UNIVERSITY
Table Column Data Type Length Precision Scale Primary Key Nullable Default Comment

JOB JOBID Varchar2 3 - - - - -

1-1

Query 2: Insert into person values('101','asha');

Insert into person values('102','smitha');

Insert into person values('103','lavanya');

Insert into person values('104','bharath');

Insert into person values('101','veena');

select*from person;

PID NAME
101 Asha
102 Smitha
103 Lavanya
104 Bharath
101 Veena

insert into job values ('j1');


insert into job values ('j2');

select*from job;

JOBID
j1
j2

32 NRUPATHUNGA UNIVERSITY
Query 3: select pid,p.name,j.jobid from person p cross join job j;

PID NAME JOBID


101 Asha j1
102 Smitha j1
103 Lavanya j1
104 Bharath j1
101 veena j1
101 asha j2
102 smitha j2
103 lavanya j2
104 bharath j2
101 veena j2

***************************

33 NRUPATHUNGA UNIVERSITY
PART-B
1. Consider the insurance database given below. The primary keys are underlined
and the data types are specified.

PERSON (driver-id-no: string, name: string,

address:strong)

CAR (regno: string, model: string, year: int)

ACCIDENT (report-no: int, date: date,

location:String) OWNS (driver-id-no: string,

regno: string)

PARTICIPATED (driver-id-no: string, regno: string, report-no: int, damage-amount: int)

i) Create the above tables by properly specifying the primary keys and the foreign keys

ii) Enter atleast five tuples for each relation.

iii) Demonstrate how you

a) Update thedamageamountforthecarwithspecific regno in the

accident with report no 12 to 25000.

b) Add a new accident to the database.

iv) Find total number of people who owned cars that were involved in accidents in 2002

v) Find the number of accidents in which cars belonging to a specific model were

involved Answers

1. create table person (driverid varchar (5), name varchar (20) NOT NULL, address
varchar (20), primary key(driverid));
desc person;

Colum Data Leng Precisi Sca Primary Nullab Defa Comme


Table n Type th on le Key le ult nt
PERS DRIVER
Varchar2 5 - - 1 - - -
ON ID
NAME Varchar2 20 - - - - - -
ADDRE
Varchar2 20 - - - - -
SS

Create table car(regno varchar(9),model varchar(15)NOT NULL, year integer NOT NULL ,primary key(regno));

Desc car;

1 |Page – NRUPATHUNGA UNIVERS ITY


Tabl Colu Data Leng Precisi Scal Primary Nullab Defau Comme
e mn Type th on e Key le lt nt
REGN
CAR Varchar2 9 - - 1 - - -
O
MODEL Varchar2 15 - - - - - -
YEAR Number - - 0 - - - -

create table accident (reportno integer, accdate date NOT NULL, location varchar(20) NOT
NULL, primary key(reportno));

desc accident;

Colum Data Leng Precisi Sca Primary Nulla Defa Comm


Table n Type th on le Key ble ult ent
ACCIDE REPORT
Number - - 0 1 - - -
NT NO
ACCDAT
Date 7 - - - - - -
E
LOCATI
Varchar2 20 - - - - - -
ON

create table owns (driverid varchar (5), regno varchar (9), primary key (driverid, regno),
foreign key(driverid)references person(driverid), foreign key(regno)references car(regno));

desc owns;

Tabl Colum Data Leng Precisi Scal Primary Nullab Defau Comme
e n Type th on e Key le lt nt
OWN DRIVER
Varchar2 5 - - 1 - - -
S ID
REGNO Varchar2 9 - - 2 - - -

create table participated (driverid varchar (5), regno varchar(9), reportno integer,
damageamt integer, primary key(driverid,regno,reportno),foreign key(driverid)references
person(driverid),foreign key(regno)references car(regno),foreign key(reportno)references
accident(reportno));

desc participated;

Data Leng Precis Sca Primary Nulla Defa Comm


Table Colum n Type th ion le Key ble ult ent
PARTICIP DRIVERI
Varchar2 5 - - 1 - - -
ATED D
REGNO Varchar2 9 - - 2 - - -
REPORT
Number - - 0 3 - - -
NO
DAMAGE
Number - - 0 - - -
AMT

1. insert into person values('10','vimal','sanjaynagar');


insert into person values('11','rajesh','malleswaram');
insert into person values('12','kamal','rajajinagar');

2 |Page – NRUPATHUNGA UNIVERS ITY


insert into person values('13','sumanth','yelahanka');
insert into person values ('14','vikranth','richmond town');

select * from person;

DRIVERID NAME ADDRESS


10 vimal sanjaynagar
11 rajesh malleswaram
12 kamal rajajinagar
13 sumanth yelahanka
14 vikranth richmond town

insert into car values ('KA04M6699','LANCER',1998);


insert into car values ('KA02N1356','MARUTHI800',2000);
insert into car values ('KA04Q2344','GETZ',2007);
insert into car values ('KA02P9956','INDICA VX',2003);
insert into car values ('KA03L7456','FORD ICON',2005);

select * from car;

REGNO MODEL YE AR
KA04M6699 LANCER 1998
KA02N1356 MARUTHI800 2000
KA04Q2344 GETZ 2007
KA02P9956 INDICA VX 2003
KA03L7456 FORD ICON 2005

insert into accident values (12,'01-jan-2001','residency road');


insert into accident values (250,'31-dec-2002','M G road');
insert into accident values (333,'02-oct-2003','richmond road');
insert into accident values (15700,'01-jun-2007','double road');
insert into accident values (25000,'21-sep-2013','tumkur road');

select * from accident;

REPORTNO ACCD ATE LOC ATION


12 01-JAN-01 residency road
250 31-DEC-02 M G road
333 02-OCT-03 richmond road
15700 01-JUN-07 double road
25000 21-SEP-13 tumkur road
25208 29-OCT-08 magadi road

insert into owns values('10','KA04M6699');


insert into owns values('11','KA02P9956');
insert into owns values('12','KA03L7456');
insert into owns values('13','KA04Q2344');
insert into owns values('14','KA02N1356');

select * from owns;

3 |Page – NRUPATHUNGA UNIVERS ITY


DRIVERID REGNO
10 KA04M6699
11 KA02P9956
12 KA03L7456
13 KA04Q2344
14 KA02N1356

insert into participated values('10','KA04M6699',12,10000);


insert into participated values('11','KA02P9956',250,10000);
insert into participated values('12','KA03L7456',333,10000);
insert into participated values('13','KA04Q2344',15700,10000);
insert into participated values('14','KA02N1356',25000,10000);

select * from participated;

DRIVERID REGNO REPORTNO DAM AGE AMT


10 KA04M6699 12 10000
11 KA02P9956 250 10000
12 KA03L7456 333 15000
13 KA04Q2344 15700 10000
14 KA02N1356 25000 10000
13 KA04Q2344 25208 6000

2. update participated set damageamt=15000 where regno='KA03L7456' and reportno


between 12 and 25000;

select * from participated;

DRIVERID REGNO REPORTNO DAM AGE AMT


10 KA04M6699 12 10000
11 KA02P9956 250 10000
12 KA03L7456 333 15000
13 KA04Q2344 15700 10000
14 KA02N1356 25000 10000
13 KA04Q2344 25208 6000

a. insert into accident values (25208,'29-oct-2008','magadi road');

select * from accident;

REPORTNO ACCD ATE LOC ATION


12 01-JAN-01 residency road
250 31-DEC-02 M G road
333 02-OCT-03 richmond road
15700 01-JUN-07 double road
25000 21-SEP-13 tumkur road
25208 29-OCT-08 magadi road

4 |Page – NRUPATHUNGA UNIVERS ITY


b. insert into participated values('13','KA04Q2344',25208,6000);
select * from participated;

DRIVERID REGNO REPORTNO DAM AGE AMT


10 KA04M6699 12 10000
11 KA02P9956 250 10000
12 KA03L7456 333 15000
13 KA04Q2344 15700 10000
14 KA02N1356 25000 10000
13 KA04Q2344 25208 6000

3. select count (*) from accident where accdate between '1-jan-2002' and '31-dec-
2002';

COUNT(*)
1

4. select count (*) from car C, participated P where c.regno = p.regno and
model='FORD ICON';
COUNT(*)
1

*****************************

2.Considerthefollowingdatabaseofstudents enrollment in courses and

booksadopted for eachcourse.

STUDENT(regno: string, name:string,major:strong,

bdate: date) COURSE(course-no: int cname: string,

dept: string)

ENROLL(reg-no: string, course-no:int,sem:int,

marks: int) BOOK-ADOPTION(course-no: int, sem:

int, book-isbn: int)

TEXT(book-isbn: int, book-title: string, publisher: string, author: string)

i) Create the above tables by properly specifying the primary keys and the foreign keys

ii) Enter atleast five tuples for each relation.

5 |Page – NRUPATHUNGA UNIVERS ITY


iii) Demonstratehowyouaddanewtextbook tothedatabaseandmakethis

bookbe adopted by somedepartment.

iv) Produce a list of text books (include Course-no, book-isbn, book-title)

in the alphabetical order for courses offered by the ‘Compute Science’

department that use more than twobooks.

v) List any department that has all its adopted books published by a specific publisher.

Answers

1. create table student2(regno varchar(10),name varchar(20),major varchar(10),bdate


date,primary key(regno));

desc student2;

C o lu D a ta Le ng Pre cis i Sca P rima r y N u lla D e fa o mm


T a b le mn T yp e th on le Key b le u lt e nt
STUDE REGN Varchar2 10 - - 1 - - -
NT2 O
NAME Varchar2 20 - - - - -

MAJOR Varchar2 10 - - - - -

BDATE Date 7 - - - - -

create table course1(courseno int,cname varchar(15),dept varchar(10),primary

key(courseno)); desc course1;

C o lu m D a ta Le ng Pre cis i Sca P rima r y N u lla D e fa C o mm


T a b le n T yp e th on le Key b le u lt e nt
COURS COURSE
E1 NO Number - - 0 - - -

CNAME Varchar2 15 - - -
- -
DEPT Varchar2 10 - - -
- -

create table enroll(regno varchar(10),courseno int,sem int not null,marks int,primary


key(regno,courseno),foreign key(regno)references student2(regno),foreign
key(courseno)references course1(courseno));

Tabl C o lu m D a ta Le ng Pre cis i Sca P rima r y N u lla D e fa C o mm


e n T yp e th on le Key b le u lt e nt
ENRO REGNO Varchar2 10 - - 1 - - -
LL
COURSE Number - - 0 2 - - -
NO
SEM Number - - 0 - - - -

6 |Page – NRUPATHUNGA UNIVERS ITY


MARKS Number - - 0 - - -

create table textbook1(isbn int,title varchar(20),publisher varchar(20),author


varchar(20),primary key(isbn));

desc textbook1;

C o lu m D a ta Le ng Pre cis i Sca P rima r y N u lla D e fa C o mm


T a b le n T yp e th on le Key b le u lt e nt
TEXTBO
ISBN Number - - 0 - - -
OK1

TITLE Varchar2 20 - -
- - -
PUBLIS
Varchar2 20 - -
HER

AUTHOR Varchar2 20 - - - - -

- - -

create table bookadoption1(courseno int,sem int,isbn int,primary key(courseno,sem),foreign


key(courseno)references course1(courseno),foreign key(isbn)references textbook1(isbn));

desc bookadoption1;

C o lu m D a ta Len P re c is Sca P rima r y N u lla D e fa C o mm


T a b le n T yp e g th io n le Key b le u lt e nt
BOOKADOP COURS
TION1 ENO Number - 0 1 - - -

SEM Number - - 0 2 - - -

ISBN Number - - 0 - - -

2. insert into student2 values('10bsc1001','amrutha','physics','02-jun-

1992');

insert into student2 values('10bsc1002','veena','chemistry','18-sep-1992');

insert into student2 values('11bsc2003','mona','compsc','17-oct-1993');

insert into student2 values('11bsc2005','seema','maths','05-may-1993');

insert into student2 values('12bsc3001','usha','compsc','03-feb-1994');

select * from student2;

R E GN O N AM E M A J OR B D AT E
10bsc1001 amrutha physics 02-JUN-92
10bsc1002 veena chemistry 18-SEP-92
11bsc2003 mona compsc 17-OCT-93
11bsc2005 seema maths 05-MAY-93

7 |Page – NRUPATHUNGA UNIVERS ITY


12bsc3001 usha compsc 03-FEB-94

8 |Page – NRUPATHUNGA UNIVERS ITY


insert into course1 values(10,'modern_physics','physics');

insert into course1 values(16,'computer_net','compsc');

insert into course1 values(22,'algebra','maths');

insert into course1 values(17,'data_struct','compsc');

insert into course1 values(12,'optics','physics');

select * from course1;

C OU R SE N O C N AM E DEPT

10 modern_physics physics
16 computer_net compsc

22 algebra maths
17 data_struct compsc

12 optics physics
23 c++ compsc

insert into enroll values('10bsc1001',10,2,70);


insert into enroll
values('10bsc1002',16,1,80); insert into
enroll values('11bsc2003',22,4,94); insert
into enroll values('11bsc2005',17,3,65);
insert into enroll
values('12bsc3001',12,5,77); select * from
enroll;
R E GN O C OU R SE N O SEM M AR K S
10bsc1001 10 2 70
11bsc2003 22 4 94
11bsc2005 17 3 65

12bsc3001 12 5 77
10bsc1002 16 1 80

insert into textbook1 values(7001,'modern_physics','BPB','halliday');

insert into textbook1 values(6050,'computer_net','mcgrawhill','stallings');

insert into textbook1 values(6900,'algebra','nootan','atre_jayaram');

insert into textbook1 values(6255,'optics','subhas','rajaram');

insert into textbook1 values(6179,'data_struct','subhas','chitra ravi');

select * from textbook1;

9 |Page – NRUPA THUNGA UNIVERS ITY


IS B N T IT L E P U B L ISH ER A U TH OR
7001 modern_physics BPB halliday
6050 computer_net mcgrawhill stallings
6900 algebra nootan atre_jayaram
6179 data_struct subhas chitra ravi
6255 Optics subhas rajaram

8005 c++ subhas M.A.rama

insert into bookadoption1 values(10,2,7001);

insert into bookadoption1 values(16,1,6050);

insert into bookadoption1 values(22,4,6900);

insert into bookadoption1 values(17,3,6179);

insert into bookadoption1 values(12,5,6255);

select * from bookadoption1;

C OU R SE N O SEM IS B N
10 2 7001
16 1 6050
22 4 6900

17 3 6179
12 5 6255
23 3 8005

Insert into textbook1 values(8005,'c++','subhas','M.A.rama');

select * from textbook1;

IS B N T IT L E P U B L ISH ER A U TH OR
7001 modern_physics BPB halliday
6050 computer_net mcgrawhill stallings

6900 algebra nootan atre_jayaram


6179 data_struct subhas chitra ravi
6255 Optics subhas rajaram
8005 c++ subhas M.A.rama

insert into course1 values(23,'c++','compsc');

select * from course1;

C OU R SE N O C N AM E DEPT
10 modern_physics physics
16 computer_net compsc

10 | P a g e – N R U P A T H U N G A U N I V E R S I T Y
22 algebra maths
17 data_struct compsc
12 optics physics
23 c++ compsc

insert into bookadoption1 values(23,3,8005);

select * from bookadoption1;

C OU R SE N O SEM IS B N
10 2 7001
16 1 6050
22 4 6900

17 3 6179
12 5 6255
23 3 8005

3. create or replace view csbooks as(select c.dept,c.courseno,t.title,t.isbn from course1


c,bookadoption1 ba,textbook1 t where c.courseno=ba.courseno and ba.isbn=t.isbn and
dept='compsc')order by t.title;

select * from csbooks;

DEPT C OU R SE N O T IT L E IS B N
Compsc 23 c++ 8005
Compsc 16 computer_net 6050
Compsc 17 data_struct 6179

4. selectcourseno,isbn,titlefromcsbookswheredeptin(select deptfromcsbooks groupby


dept having count(*)>2);

C OU R SE N O IS B N T IT L E
23 8005 c++
16 6050 computer_net
17 6179 data_struct

5. select c.dept,c.cname from course1 c,bookadoption1 ba,textbook1 t where t.isbn=ba.isbn and


ba.courseno=c.courseno and t.publisher='mcgrawhill';

DEPT C N AM E
Compsc computer_net

***********************

11 | P a g e – N R U P A T H U N G A U N I V E R S I T Y
3.
Consider the following database for BANK.
BRANCH(branch-name: string, branch-city: string, assets:
ACCOUNT(accno: int, branch-name: string, balance: real)
DEPOSITOR(customer-name: string, accno: int)
CUSTOMER(customer-name: string, customer-street: string, customer-city: string)
LOAN(loan-no: int, branch-name: string, amount: real)

BORROWER(customer-name: string, loan-no: int)

i) Create the above tables by properly specifying the primary keys and the foreign
keys.
ii) Enter atleast five tuples for each relation
iii) Find all the customers who have atleast two accounts at the main branch.
iv) Find all the customers who have an account at all the branches located in a specific
city
v) Demonstrate how you delete all Customer tuples at every branch located in a
specific city.

ANSWER

1.
create table branch(branchname varchar(20),branchcity varchar(20),assets
decimal(12,4),primary key(branchname));

desc branch;

Tabl Data Leng Precisi Sca Primary Nulla Defa Comm


e Colum n Type th on le Key ble ult ent
BRAN BRANCHN
Varchar2 20 - - 1 - - -
CH AME
BRANCHCI
Varchar2 20 - - - - -
TY

ASSETS Number - 12 4 - - -

create table account(accno int,branchname varchar(20),balance decimal(12,4),primary


key(accno),foreign key(branchname) references branch(branchname));

desc account;

Data Leng Precisi Sca Primary Nulla Defa Comm


Table Colum n Type th on le Key ble ult ent
ACCOU
ACCNO Number - - 0 1 - - -
NT
BRANCHN
Varchar2 20 - - - - -
AME
BALANCE Number - 12 4 - - -

create table depositor(custname varchar(20),accno int,foreign key(accno) references


account(accno));
12 | P a g e – N R U P A T H U N G A U N I V E R S I T Y
desc depositor;

Colum Data Leng Precisi Sca Primary Nulla Defa Comm


Table n Type th on le Key ble ult ent
DEPOSIT CUSTNA
Varchar2 20 - - - - -
OR ME
ACCNO Number - - 0 - - -

create table customer(custname varchar(20),street varchar(20), city varchar(20));

desc customer;

Colum Data Leng Precisi Sca Primary Nulla Defa Comm


Table n Type th on le Key ble ult ent
CUSTO CUSTNA
Varchar2 20 - - - - -
MER ME

STREET Varchar2 20 - - - - -

CITY Varchar2 20 - - - - -

create table loan(loanno int primary key, branchname varchar(20),amount


decimal(12,4),foreign key(branchname) references branch(branchname));

desc loan;

Tab Data Leng Precisi Sca Primary Nulla Defa Comm


le Colum n Type th on le Key ble ult ent
LOA
LOANNO Number - - 0 1 - - -
N
BRANCHN
Varchar2 20 - - - - -
AME

AMOUNT Number - 12 4 - - -

create table borrower(custname varchar(20),loanno int primary key,foreign key(loanno)


references loan(loanno));
desc borrower;

Colum Data Leng Precisi Sca Primary Nulla Defa Comm


Table n Type th on le Key ble ult ent
BORRO CUSTNA
Varchar2 20 - - - - -
WER ME
LOANN
Number - - 0 - - -
O

2.

insert into branch values('Malleswaram','Bangalore',960000);


insert into branch values('Rajajinagar','Bangalore',1500000);
insert into branch values('Jayanagar','Bangalore',1750000);
insert into branch values('Andheri','Mumbai',1250000);
insert into branch values('Annanagar','Chennai',1000000);

13 | P a g e – N R U P A T H U N G A U N I V E R S I T Y
select * from branch;

BR ANCHN AME BR ANCHCITY ASSETS


Malleswaram Bangalore 960000
Rajajinagar Bangalore 1500000
Jayanagar Bangalore 1750000
Andheri Mumbai 1250000
Annanagar Chennai 1000000

insert into account values(10702,'Andheri',11000);


insert into account values(31001,'Annanagar',15200);
insert into account values(50020,'Jayanagar',17515);
insert into account values(50105,'Malleswaram',12500);
insert into account values(57008,'Rajajinagar',12345);

select * from account;

ACCNO BR ANCHN AME BAL ANCE


10702 Andheri 11000
31001 Annanagar 15200
50020 Jayanagar 17515
50105 Malleswaram 12500
57008 Rajajinagar 12345

insert into depositor values('Akshatha',10702);


insert into depositor values('Poornima',31001);
insert into depositor values('Kavitha',50020);
insert into depositor values('Sneha',50105);
insert into depositor values('Shruthi',57008);

select * from depositor;

CUSTN AME ACCNO


Akshatha 10702
Poornima 31001
Kavitha 50020
Sneha 50105
Shruthi 57008

insert into customer values('Akshatha','Shivaji St','Mumbai');


insert into customer values('Kavitha','Rajaram St','Bangalore');
insert into customer values('Poornima','Chetty St','Chennai');
insert into customer values('Sneha','Sastry St','Bangalore');
insert into customer values('Shwetha','Ramakrishna St','Delhi');

select * from customer;

14 | P a g e – N R U P A T H U N G A U N I V E R S I T Y
CUSTN AME STREET CITY
Akshatha Shivaji St Mumbai
Kavitha Rajaram St Bangalore
Poornima Chetty St Chennai
Sneha Sastry St Bangalore
Shwetha Ramakrishna St Delhi

insert into loan values(123,'Malleswaram',100000);


insert into loan values(456,'Rajajinagar',50000);
insert into loan values(789,'Jayanagar',75000);
insert into loan values(101,'Andheri',150000);
insert into loan values(356,'Annanagar',50000);

select * from loan;

LOANNO BR ANCHN AME AM OUNT


123 Malleswaram 100000
456 Rajajinagar 50000
789 Jayanagar 75000
101 Andheri 150000
356 Annanagar 50000

insert into borrower values('Akshatha',101);


insert into borrower values('Rajesh',123);
insert into borrower values('Bhaskar',356);
insert into borrower values('Shruthi',456);
insert into borrower values('Srikanth',789);

select * from borrower;

CUSTN AME LOANNO


Akshatha 101
Rajesh 123
Bhaskar 356
Shruthi 456
Srikanth 789

3.
create view twoacc as(select b.branchname, a.accno, d.custname from branch b, account a,
depositor d where a.accno=d.accno and a.branchname=b.branchname and
b.branchname='Andheri');

select * from twoacc ;

BR ANCHN AME ACCNO CUSTN AME


Andheri 10702 Akshatha

4.
select d.custname, a.accno, b.branchname, b.branchcity from depositor d, account a,
branch b where b.branchcity='Bangalore' and d.accno=a.accno and
a.branchname=b.branchname;

15 | P a g e – N R U P A T H U N G A U N I V E R S I T Y
CUSTN AME ACCNO BR ANCHN AME BR ANCHCITY
Kavitha 50020 Jayanagar Bangalore
Sneha 50105 Malleswaram Bangalore
Shruthi 57008 Rajajinagar Bangalore

5.
delete from customer where city in(select branchcity from branch where
branchcity='Bangalore');

select * from customer;

CUSTN AME STREET CITY


Akshatha Shivaji St Mumbai
Poornima Chetty St Chennai
Shwetha Ramakrishna St Delhi

16 | P a g e – N R U P A T H U N G A U N I V E R S I T Y
4. Create the following attributes
Branch_loan(bwname string,loanno string,loanamt number(6)
Customer_loan(cname string,loanno string);
2. Insert at least 3 rows in each table.
3. Demonstrate various types of Outer join operations on the tables.

create table branch_loan(bname varchar(20),loanno varchar(10),loan3 amt


decimal(6));
desc branch_loan;
Colu Data Leng Precisi Sca Primary Nulla Defa Comm
Table mn Type th on le Key ble ult ent
BRANCH_L
BNAME Varchar2 20 - - - - -
OAN
LOANN
Varchar2 10 - - - - -
O
LOANA
Number - 6 0 - - -
MT
create table customer_loan(cname varchar(20),loanno varchar(10));
desc customer_loan;
Colu Data Leng Precis Sca Primary Nulla Defa Comm
Table mn Type th ion le Key ble ult ent
CUSTOMER_ CNAM
Varchar2 20 - - - - -
LOAN E
LOAN
Varchar2 10 - - - - -
NO
2. insert into branch_loan values('x','l-170',3000);
insert into branch_loan values('y','l-210',5000);
insert into branch_loan values('z','l-240',8000);
select * from branch_loan;
BN AME LOANNO LOAN AMT
x l-170 3000
y l-210 5000
z l-240 8000
insert into customer_loan values('a','l-170');

17 | P a g e – N R U P A T H U N G A U N I V E R S I T Y
insert into customer_loan values('b','l-150');
insert into customer_loan values('c','l-240');
select * from customer_loan;
CN AME LOANNO
a l-170
b l-150
a l-170
c l-240
3. a. select b1.bname,b1.loanno,b1.loanamt,c1.cname,c1.loanno from branch_loan b1
LEFT OUTER JOIN customer_loan c1 on b1.loanno=c1.loanno;
BN AME LOANNO LOAN AMT CN AME LOANNO
x l-170 3000 a l-170
x l-170 3000 a l-170
z l-240 8000 c l-240
y l-210 5000 - -
b. select b1.bname,b1.loanno,b1.loanamt,c1.cname,c1.loanno from branch_loan b1
RIGHT OUTER JOIN customer_loan c1 on b1.loanno=c1.loanno;
BN AME LOANNO LOAN AMT CN AME LOANNO
x l-170 3000 a l-170
x l-170 3000 a l-170
z l-240 8000 c l-240
- - - b l-150
c. select b1.bname,b1.loanno,b1.loanamt,c1.cname,c1.loanno from branch_loan b1
FULL OUTER JOIN customer_loan c1 on b1.loanno=c1.loanno;
BN AME LOANNO LOAN AMT CN AME LOANNO
x l-170 3000 a l-170
x l-170 3000 a l-170
z l-240 8000 c l-240
y l-210 5000 - -
- - - b l-150

5. Sales person database


(1) Create the following tables:
Salesperson(sid int, sname varchar(20), age int, salary number(10)); Customer(cid int,cname
varchar(20, city varchar(20), industrytypevarchar(1));
Orders(num int, order_date datetime, cust_id int, salesperson_id int, amtint);
(ii) Insert atleast 5 rows in each table.
(iii) Find the names of all salespeople that have an order with a specific customer.
(iv) Find the names of all salespeople that do not have any order with that specific customer.
(v) Find the of salespeople that have 2 or more orders.

create database SalesOrdersdb;


use SalesOrdersdb;

(1) Create the following tables:Salesperson(sid int, sname varchar(20),age int, salary
number(10));
(2) Customer(cid int,cname varchar(20, city varchar(20), industrytype varchar(1));
(3) Orders(num int, order_date datetime, cust_id int, salesperson_id int, amt int);

18 | P a g e – N R U P A T H U N G A U N I V E R S I T Y
Query 1:

create table salesperson(sid int primary key, sname varchar(20), age int, salary int);

desc salesperson;

Colu Data Leng Precisi Sca Primary Nulla Defa Comm


Table mn Type th on le Key ble ult ent
SALESPER
SID Number - - 0 1 - - -
SON
SNAM
Varchar2 20 - - - - -
E

AGE Number - - 0 - - -

SALAR
Number - - 0 - - -
Y

create table customer3(cid int primary key, cname varchar(20), city varchar(20), industrytype
varchar(1));
desc customer3;

Data Leng Precis Sca Primary Nulla Defa Comm


Table Colum n Type th ion le Key ble ult ent
CUSTOM
CID Number - - 0 1 - - -
ER3

CNAME Varchar2 20 - - - - -

CITY Varchar2 20 - - - - -

INDUSTRY
Varchar2 1 - - - - -
TYPE

create table orders(onum int primary key, order_date date,cust_id int, foreign key(cust_id)
references customer3(cid),salesperson_id int, foreign key(salesperson_id) references
salesperson(sid),atm int);
desc orders;

Data Leng Precis Sca Primary Nulla Defa Comm


Table Colum n Type th ion le Key ble ult ent
CUSTOM
CID Number - - 0 1 - - -
ER3
CNAME Varchar2 20 - - - - -

CITY Varchar2 20 - - - - -
INDUSTRY
Varchar2 1 - - - - -
TYPE

19 | P a g e – N R U P A T H U N G A U N I V E R S I T Y
(ii) Insert atleast 5 rows in each table.

insert into salesperson values(1,'Vinay',40,40000);


insert into salesperson values(2,'Mohan',34,35000);
insert into salesperson values(3,'Amit',32,30000);
insert into salesperson values(4,'Joseph',42,45000);
insert into salesperson values(5,'Pramod',38,36000);
select * from salesperson;

SID SN AME AGE SAL ARY

1 Vinay 40 40000

2 Mohan 34 35000

3 Amit 32 30000

4 Joseph 42 45000

5 Pramod 38 36000

insert into customer3 values(4,'Samsonic','Bangalore','J');


insert into customer3 values(6,'Panasung','Delhi','J');
insert into customer3 values(7,'Samony','Mumbai','B');
insert into customer3 values(9,'Orange','Bangalore','B');
insert into customer3 values(11,'Sigma','Chennai','J');
select*from customer3;

CID CN AME CITY INDUSTRYTYPE

4 Samsonic Bangalore J

6 Panasung Delhi J

7 Samony Mumbai B

9 Orange Bangalore B

11 Sigma Chennai J

20 | P a g e – N R U P A T H U N G A U N I V E R S I T Y
insert into orders values(10,'08-Feb-96',4,1,540);
insert into orders values(20,'01-Mar-99',6,2,1800);
insert into orders values(30,'07-Jan-95',7,3,460);
insert into orders values(40,'01-Sep-98',9,4,2400);
insert into orders values(50,'02-May-98',11,5,600);
select*from orders;

ONUM ORDER_ D ATE CUST_ID S ALE SPERS ON_ID ATM

10 08-FEB-96 4 1 540

20 01-MAR-99 6 2 1800

30 07-JAN-95 7 3 460

40 01-SEP-98 9 4 2400

50 02-MAY-98 11 5 600

(iii) Find the names of all salespeople that have an order with a specific customer.

select salesperson.sname from salesperson where sid=(select orders.salesperson_id from


orders, customer3 where orders.cust_id=customer3.cid and customer3.cname='Samsonic');

SN AME
Vinay

(iv) Find the names of all salespeople that do not have any order with that specific customer.

select salesperson.sname from salesperson where sid not in(select orders.salesperson_id from
orders, customer3 where orders.cust_id=customer3.cid and customer3.cname='Samsonic');

SN AME
Mohan
Amit
Joseph
Pramod

(v) Find the of salespeople that have 2 or more orders.


select sname from orders,salesperson where orders.salesperson_id=salesperson.sid group by
sname, salesperson_id having count(salesperson_id)>1;

21 | P a g e – N R U P A T H U N G A U N I V E R S I T Y
no data found.

6. DATE FUNCTIONS

QUERY 1: TO FIND SYSTEM DATE.


select sysdate from dual;

==

SYSDATE
30-NOV-22
1 rows returned in 0.45
seconds

QUERY 2: TO FIND NEXT DAY.

select sysdate,NEXT_DAY(sysdate,'sunday') "NEXT_DAY" from dual;

SYSDATE NEXT_DAY

30-NOV-22 04-DEC-22

QUERY 3: TO ADD MONTHS.


select sysdate,ADD_MONTHS(sysdate,4) "ADD_MONTHS" fromdual;
SYSDATE ADD_MONTHS

30-NOV-22 31-MAR-23

1 rows returned in 0.00 seconds

QUERY 4: TO FIND LAST DAY.


select sysdate,last_day(sysdate)"last_day" from dual;

SYSDATE Last_day

01-DEC-22 31-DEC-22

select sysdate,last_day('14-feb-22')"last_day" from dual;


SYSDATE Last_day

01-DEC-22 28-FEB-22

1 rows returned in 0.00


seconds

22 | P a g e – N R U P A T H U N G A U N I V E R S I T Y
QUERY 5: TO FIND MONTHS_BETWEEN.
select round(months_between('31-dec-19','30-jul-18'))fromdual;
ROUND(MONTHS_BETWEEN('31 -DEC-19','30-JUL-18'))

17

1 rows returned in 0.00 seconds

select months_between('31-dec-20','31-jan-22')from dual;


MONTHS_BETWEEN('31 -DEC-20','31-JAN-22')

-13

1 rows returned in 0.00 seconds

QUERY 6: TO FIND LEAST.


select least('31-dec-20','31-jan-22')from dual;
LEAST('31 -DEC-20','31-JAN-22')

31-dec-20

1 rows returned in 0.00 seconds

QUERY 7:TO FIND GREATEST.


select greatest('31-dec-20','31-jan-22')from dual;
GREATEST('31-DEC-20','31 -JAN-22')

31-jan-22

1 rows returned in 0.00 seconds

QUERY 8: ROUND FUNCTION.


Select sysdate ,round(sysdate,'month') "round_month"from dual;
SYSDATE Round_month

05-DEC-22 01-DEC-22

1 rows returned in 0.19 seconds

23 | P a g e – N R U P A T H U N G A U N I V E R S I T Y
Select sysdate ,round(sysdate,'year') "round_year" fromdual;

SYSDATE Round_year

05-DEC-22 01-JAN-23

1 rows returned in 0.00 seconds

QUERY 9: TRUNC FUNCTION .


Select sysdate-90 ,trunc(sysdate-90,'year') "trunc_year"from dual;
SYSDATE-90 Trunc_year

06-SEP-22 01-JAN-22

1 rows returned in 0.02 seconds

Select sysdate+20 ,trunc(sysdate+20,'month')"trunc_month" from dual;

SYSDATE+20 TRUNC_MONTH

25-DEC-22 01-DEC-22

1 rows returned in 0.11 seconds

Conversion function
QUERY 1:TO _CHAR
select sysdate,To_char(sysdate,'DD.MM.YYYY') fromdual;

TO_CHAR(SYSDATE,'DD
SYSDATE .MM.YYYY')

05-DEC-22 05.12.2022

1 rows returned in 0.00seconds

select sysdate,To_char(sysdate,'DD') from dual;

SYSDATE TO_CHAR(SYSDATE,'DD')

05-DEC-22 05

1 rows returned in 0.00seconds

select sysdate,To_char(sysdate,'MM ,YEAR') fromdual;


SYSDATE TO_CHAR(SYSDATE,'MM,YEAR')

24 | P a g e – N R U P A T H U N G A U N I V E R S I T Y
05-DEC-22 12 ,TWENTY TWENTY-TWO

1 rows returned in 0.00seconds

QUERY 2: TO_DATE.
select To_date('2003/12/13','YYYY/MM/DD') fromdual;
TO_DATE('2003/12/13','YYYY/MM /DD')

13-DEC-03

select To_date('082407','MMYYDD') from dual;

TO_DATE('082407','MMYYDD')

07-AUG-24

7. String functions

create table student_db1(id number(20),firstname


varchar(10),lastname varchar(10),email varchar(15));
desc student_db2;
Object TABLE Objec STUDENT_DB1
Type t
Colu Data Len Preci Sca Primar Nulla Def Com
Table mn Typ gth sion le y Key ble a men
e ult t
STUDENT
ID Number - 20 0 - - -
_DB2
FIRST Varcha
10 - - - - -
NAME r2
LASTN Varcha
10 - - - - -
AME r2
Varcha
EMAIL 15 - - - - -
r2

1-4

insert into student_db2 values('u24an21s0','ucchas','ADIKARI','[email protected]');insert into


student_db2 values('u25an42s1','pavani','ACHAR','[email protected]'); insert into student_db2
values('u26an21s3','piu','MANDAL','[email protected]'); insert into student_db2
values('u27an21s6','santhosh','PATIL','[email protected]');

select*from student_db2;

25 | P a g e – N R U P A T H U N G A U N I V E R S I T Y
FIRSTNAM LASTNAM
ID E E EMAIL
u24a
ucchas ADIKARI [email protected]
n
21s0
u25an42 [email protected]
pavani ACHAR
s1 om
u26an21 [email protected]
piu MANDAL
s3 om
u27an21 [email protected]
santhosh PATIL
s6 om

Using concatenation(||) function

select firstname,lastname,firstname||lastname from student_db2;

FIRSTNAME LASTNAME FIRSTNAME||LASTNAME


ucchas ADIKARI ucchasADIKARI
pavani ACHAR pavaniACHAR
piu MANDAL piuMANDAL
santhosh PATIL santhoshPATIL

Using lpad function


select id,lpad('u24an21s0',12,'0')as leftpadding from student_db2 where firstname='ucchas';

ID LEFTPADDING
u24an21s0 000u24an21s0

Using rpad function


select firstname,rpad('pavani',7,'M')as rightpadding from student_db2 wherelastname='ACHAR';

FIRSTNAME RIGHTPADDING
pavani pavaniM

Using ltrim function


select email,ltrim('[email protected]','nm')as lefttrim from student_db2 wherefirstname='piu';

EMAIL LEFTTRIM
[email protected] @gmail.com
26 | P a g e – N R U P A T H U N G A U N I V E R S I T Y
Using rtrim function
select id,rtrim('u27an21s6','s6')as righttrim from student_db2 wherelastname='PATIL';

ID RIGHTTRIM
u27an21s6 u27an21

Using lower function


select lastname,lower('PATIL')as lowerstring from student_db2 whereid='u27an21s6';

LASTNAME LOWERSTRING
PATIL patil

Using upper function


select firstname,upper('ucchas')as upperstring from student_db2 wherelastname='ADIKARI';
FIRSTNAME UPPERSTRING
ucchas UCCHAS

Using Inticap function


select id,initcap('u24an21s0')as initcap from student_db2 wherefirstname='ucchas';

ID INITCAP
u24an21s0 U24an21s0

Using length function


select email,length('[email protected]')as stringlength from student_db2 wherefirstname='pavani';

EMAIL STRINGLENGTH
[email protected] 12

Using substr function


select email,substr('[email protected]',3,10)as substring from student_db2 wherelastname='PATIL';
EMAIL SUBSTRING
[email protected] @gmail.com

Using instr function


select firstname,instr('santhosh','hosh')as instr from student_db2 whereid='u27an21s6';

FIRSTNAME INSTR
santhosh 5
27 | P a g e – N R U P A T H U N G A U N I V E R S I T Y
PL/SQL

1. Write a PL/SQL program to find the largest of three numbers

declare
a number; b number; c number;begin
a:=&a;
b:=&b;
c:=&c;
if (a>b and a>c) then dbms_output.put_line(‘aislargest’
||a);
elsif (b>a and b>c) then
dbms_output.put_line(‘bislargest’ ||b);else

dbms_output.put_line(‘c is the largest’||c);end


if;

end;

2. Write a PL/SQL program to generate reverse for given number

declare
nnumber(4):=&n;
s number(4) := 0;
r number(4);
begin
while n > 0
loop
r:= mod(n,10);
s:=(s*10)+r;
n:=trunc(n/10);
end loop;
dbms_output.put_line(‘the reverse number is’);
dbms_output.put_line(s);
end;

3. Write a PL/SQLProgramto find thefactorialofagivennumber

declare
i number(4) :=1;

28 | P a g e – N R U P A T H U N G A U N I V E R S I T Y
nnumber(4):=&n;
f number(4) :=1;
begin
for i in 1..n
loop
f:=f*i;
end loop;
dbms_output.put_line(‘factorial of anumberis’||f);
end;

4. Write a PL/SQL program to check whether given number is prime or not

declare
num number;
i number:= 1;
c number:=0;
begin
num := #
for i in 1..num loop
if ((mod(num,i))=0) then
c:=c+1;
end if;
end loop;
if (c>2) then
dbms_output.put_line(num|| ‘not prime’);
else
dbms_output.put_line(num ||‘is prime’);
end if;
end;

5. Writea PL/SQLprogram to generate Fibonacci seriesupto N

declare
a number(3) := 1;
b number(3) := 1;
c number(3);
n number(3);
begin

29 | P a g e – N R U P A T H U N G A U N I V E R S I T Y
n:=&n;
dbms_output.put_line(‘the Fibonacci series is:’)
while a<=n
loop
dbms_output.put_line(a);
c:=a+b;
a:=b;
b:=c;
end loop;
end;

6. Write a PL/SQL program for inserting a row into employee table

Createtableemployee (emp_idnumber(5) primarykey,emp_namevarchar2(30), Emp_dept


varchar2(10),emp_salary varchar2(8));

Declare
begin
Insert into employee values(10,’chitra’,’hr’,40000);
End;

7. Writeapl/sqlprogramtohandleapredefinedexception

declare
n number(4);
d number(4);
begin
n:=&n;
d:=n/0;
exception
when zero_divide
then
dbms_output.put_line(‘divide by error exception is caught’);
end;

8. Write a pl/sql program for creating a procedure for calculating sum of two numbers.

Create or replace procedure sum(n1 in number,n2 in number) is Total number(6);


Begin
Total:= n1+n2;
Dbms_output.put_line(‘the sum is ‘||total);

30 | P a g e – N R U P A T H U N G A U N I V E R S I T Y
End;

Begin
Sum(10,20);
End;

Output:
The sum is : 30

9. Writeaprocedure to check thegiven yearis leapyearornot

Create orreplace procedure leapyear(y in number) is


Begin
If ymod 4 = 0 and y mod 100<>0 or y mod 400 =0 then Dbms_output.put_line (‘the given year is leap
year’);
else
Dbms_output.put_line(‘the given year is not leap year’);
End if;
End;

Begin
Leapyear(2012);
end;

31 | P a g e – N R U P A T H U N G A U N I V E R S I T Y
PL/SQL – Function Declaring

1. Declare function get_sal for an employee with specific employee code

i) Create a table with name emply1 with empcode,name and salary


ii) Insert 5 tuples into the emp table

ANSWER:

create table emply(empcode number, name varchar(10),salary number);

insert into emply values(10,'Raj',153000);


insert into emply values(11,'Singh',230000);
insert into emply values(12,'Madhu',52000);
insert into emply values(13,'Suverna',65200);
insert into emply values(14,'Likith',56400);

desc emply;

select * from emply;

EMPCODE NAME SAL ARY


10 Raj 153200
11 Singh 230000
12 Madhu 52000
13 Suverna 65200
14 Likith 56400

declare
empid emply.empcode%type;
sal emply.salary%type;
function get_sal(empno in emply.empcode%type) return number as salary number;
begin
select salary into sal from emply where emply.empcode=empno;
return(sal);
end;
begin
empid:=10;
sal:=get_sal(empid);
dbms_output.put_line('Sal of emp is id '||empid||' is '||sal);
end;

Output:

Sal of emp is id 10 is 153000

Statement processed.

32 | P a g e – N R U P A T H U N G A U N I V E R S I T Y
2. Write a procedure in pl sql to increase the salary of an employee table

i) Create the emp table with emcode name and salary


ii) Insert 5 tuples into the emp table

ANSWER:

create table emply(empcode number,name varchar(10),salary number);

desc emply;

insert into emply values(10,'Raj',153000);


insert into emply values(11,'Singh',230000);
insert into emply values(12,'Madhu',52000);
insert into emply values(13,'Suverna',65200);
insert into emply values(14,'Likith',56400);

select * from emply;

EMPCODE NAME SAL ARY


10 Raj 15300
11 Singh 230000
12 Madhu 52000
13 Suverna 65200
14 Likith 56400

create procedure Sal_hike121(eno in emply.empcode%type,increase in


emply.salary%type)as sal emply.salary%type;
new_sal emply.salary%type;
begin
select salary into sal from emply where empcode=eno;
new_sal:=sal +increase;
update emply set salary =new_sal where empcode=eno;
dbms_output.put_line('Table updated with new salary '||new_sal);
end;

OUTPUT:

Procedure created.

begin
Sal_hike121(10,200);
end;

33 | P a g e – N R U P A T H U N G A U N I V E R S I T Y
OUTPUT:

Table updated with new salary 153200

Statement processed.

EMPCODE NAME SAL ARY


10 Raj 153200
11 Singh 230000
12 Madhu 52000
13 Suverna 65200
14 Likith 56400

34 | P a g e – N R U P A T H U N G A U N I V E R S I T Y

You might also like