DBMS Lab Programs
DBMS Lab Programs
IV Sem
e) List all books that belong to “CS” department and are written by author “Navathe”.
Table created.
1 row created.
SQL> /
Enter value for accno: 1002
Enter value for title: Database System
Enter value for author: Navathe
Enter value for dept: CS
Enter value for purchasedate: 1-Jan-1998
Enter value for price: 300
old 2: (&accno,'&title','&author','&dept','&purchasedate',&price)
new 2: (1002,'Database System','Navathe','CS','1-Jan-1998',300)
1 row created.
SQL> /
Enter value for accno: 1003
Enter value for title: Discrete Maths
Enter value for author: SC Gupta
Enter value for dept: Maths
Enter value for purchasedate: 1-Nov-2003
Enter value for price: 510
old 2: (&accno,'&title','&author','&dept','&purchasedate',&price)
new 2: (1003,'Discrete Maths','SC Gupta','Maths','1-Nov-2003',510)
1 row created.
SQL> /
Enter value for accno: 1004
Enter value for title: M.Statistics
Enter value for author: SC Gupta
Enter value for dept: Stats
Enter value for purchasedate: 1-Apr-2021
Enter value for price: 999
old 2: (&accno,'&title','&author','&dept','&purchasedate',&price)
new 2: (1004,'M.Statistics','SC Gupta','Stats','1-Apr-2021',999)
1 row created.
SQL> /
Enter value for accno: 1005
Enter value for title: C
Enter value for author: Bala
Enter value for dept: CS
Enter value for purchasedate: 1-Jan-2020
Enter value for price: 700
old 2: (&accno,'&title','&author','&dept','&purchasedate',&price)
new 2: (1005,'C','Bala','CS','1-Jan-2020',700)
1 row created.
Table created.
1 row created.
SQL> /
Enter value for accno: 1005
Enter value for borrower: Suman
old 1: insert into IssuedBooks values(&accno,'&borrower')
new 1: insert into IssuedBooks values(1005,'Suman')
1 row created.
ACCNO BORROWER
---------- ----------
1001 Rahul
1005 Suman
0 rows deleted.
1 row updated.
e) List all books that belong to “CS” department and are written by author
“Navathe”.
g) List all books which have a price less than 500 or purchased between
“01/01/1999” and “01/01/2004"
SQL> commit;
Commit complete.
2. Create a database having three tables to store the details of students of Computer
Department in your college.
1.Personal information about Student (College roll number, Name of student, Date of
birth, Address, Marks(rounded off to whole number) in percentage at 10 + 2, Phone
number). College Roll Number is the primary key
2.Paper Details (Paper code, Name of the Paper). Paper Code is the Primary Key
3.Student’s Academic and Attendance details (College roll number, Paper Code,
Attendance, Marks in home examination). College Roll No is a Foreign key & PaperCode is
a Foreign Key.
College Roll Number is a foreign key and paper code is also a foreign key.
a) Identify primary and foreign keys. Create the tables and insert at least 5
records in each table.
1 row created.
SQL> /
Enter value for rollno: 5002
Enter value for name: DEF
Enter value for dob: 2-Feb-2001
Enter value for address: Warangal
Enter value for marks_plus2: 74
Enter value for phno: 8877665544
old 1: insert into StudentInformation
values(&RollNo,'&name','&dob','&address',&marks_plus2,&phno)
new 1: insert into StudentInformation values(5002,'DEF','2-Feb-
2001','Warangal',74,8877665544)
1 row created.
SQL> /
Enter value for rollno: 5003
Enter value for name: xyz
Enter value for dob: 4-Mar-2002
Enter value for address: Khammam
Enter value for marks_plus2: 75
Enter value for phno: 7894561234
old 1: insert into StudentInformation
values(&RollNo,'&name','&dob','&address',&marks_plus2,&phno)
new 1: insert into StudentInformation values(5003,'xyz','4-Mar-
2002','Khammam',75,7894561234)
1 row created.
SQL> /
Enter value for rollno: 5004
Enter value for name: pqr
Enter value for dob: 5-Apr-2001
Enter value for address: Kurnool
Enter value for marks_plus2: 54
Enter value for phno: 8965478654
old 1: insert into StudentInformation
values(&RollNo,'&name','&dob','&address',&marks_plus2,&phno)
new 1: insert into StudentInformation values(5004,'pqr','5-Apr-
2001','Kurnool',54,8965478654)
1 row created.
SQL> /
Enter value for rollno: 5005
Enter value for name: rahul
Enter value for dob: 6-Jun-2001
Enter value for address: Anantapur
Enter value for marks_plus2: 87
Enter value for phno: 8965478965
old 1: insert into StudentInformation
values(&RollNo,'&name','&dob','&address',&marks_plus2,&phno)
new 1: insert into StudentInformation values(5005,'rahul','6-Jun-
2001','Anantapur',87,8965478965)
1 row created.
1 row created.
SQL> /
Enter value for papercode: Paper2
Enter value for papername: Prog with C++
old 1: insert into PaperDetails values('&papercode','&papername')
new 1: insert into PaperDetails values('Paper2','Prog with C++')
1 row created.
SQL> /
Enter value for papercode: Paper3
Enter value for papername: DataStructures
old 1: insert into PaperDetails values('&papercode','&papername')
new 1: insert into PaperDetails values('Paper3','DataStructures')
1 row created.
SQL> /
Enter value for papercode: Paper4
Enter value for papername: DBMS
old 1: insert into PaperDetails values('&papercode','&papername')
new 1: insert into PaperDetails values('Paper4','DBMS')
1 row created.
SQL> /
Enter value for papercode: Paper5
Enter value for papername: Prog with Java
old 1: insert into PaperDetails values('&papercode','&papername')
new 1: insert into PaperDetails values('Paper5','Prog with Java')
1 row created.
PAPERC PAPERNAME
------ ---------------
Paper1 Prog with C
Paper2 Prog with C++
Paper3 DataStructures
Paper4 DBMS
Paper5 Prog with Java
SQL> /
Enter value for rollno: 5001
Enter value for papercode: Paper1
Enter value for attendance: 60
Enter value for marks: 80
old 1: insert into AcadAttendance values(&RollNo,'&PaperCode',&attendance,&marks)
new 1: insert into AcadAttendance values(5001,'Paper1',60,80)
1 row created.
SQL> /
Enter value for rollno: 5002
Enter value for papercode: Paper2
Enter value for attendance: 78
Enter value for marks: 90
old 1: insert into AcadAttendance values(&RollNo,'&PaperCode',&attendance,&marks)
new 1: insert into AcadAttendance values(5002,'Paper2',78,90)
1 row created.
b) Design a query that will return the records (from the second table) along with the name
of student from the first table, related to students who have more than 75% attendance
and more than 60% marks in paper2.
c) List all students who live in “Warangal” and have marks greater than 60 in paper1.
d) Find the total attendance and total marks obtained by each student.
select rollno
from AcadAttendance
where papercode='paper2' and marks=(select max(marks) from AcadAttendance where
papercode='paper2');
3. Create the following tables and answer the queries given below:
a) Identify primary and foreign keys. Create the tables and insert at least 5
records in each table.
------------------------------------------------------------------------------
(i) Customer (CustID, email, Name, Phone, ReferrerID)
In Customer table, CustID is the primary key & ReferrerId is a foreign key
1 row created.
SQL> /
Enter value for custid: C2
Enter value for email: def@gmail.com
Enter value for name: DEF
Enter value for phone: 7788990099
Enter value for referrerid: C1
old 1: insert into Customer values('&CustId','&email','&name',&phone,'&ReferrerId')
new 1: insert into Customer values('C2','def@gmail.com','DEF',7788990099,'C1')
1 row created.
----------------------------------------------------------------------------------------------
(ii) BicycleModel(ModelNo, Manufacturer, Style)
1 row created.
SQL> /
Enter value for modelno: 2021
Enter value for manufacturer: Hero
Enter value for style: ElectrBike
old 1: insert into BicycleModel values(&ModelNo,'&Manufacturer','&style')
new 1: insert into BicycleModel values(2021,'Hero','ElectrBike')
1 row created.
-------------------------------------------------------------------------------------------
(iii) Bicycle (BicycleID, DatePurchased, Color, CustID, ModelNo)
In Bicycle table, BicycleID is the primary key , CustId is a foreign key & ModelNo is also
foreign key
1 row created.
SQL> /
Enter value for bicycleid: 20
Enter value for datapurchased: 6-dec-2020
Enter value for color: black
Enter value for custid: C2
Enter value for modelno: 2020
old 1: insert into Bicycle values(&BicycleId,'&DataPurchased','&color','&CustId',&ModelNo)
new 1: insert into Bicycle values(20,'6-dec-2020','black','C2',2020)
1 row created.
------------------------------------------------------------------------------------------------------
(iv)
Service (StartDate, BicycleID, EndDate)
SQL> /
Enter value for startdate: 1-jan-2021
Enter value for bicycleid: 20
Enter value for enddate: 15-jan-2021
old 1: insert into Service values('&startdate',&BicycleId,'&enddate')
new 1: insert into Service values('1-jan-2021',20,'15-jan-2021')
1 row created.
b) List all the customers who have the bicycles manufactured by manufacturer “Honda”.
select modelno
from BicycleModel
where manufacturer='Honda'; // Find modelno for the manufacturer Honda
select custid
from Bicycle
where modelno=(select modelno from BicycleModel where manufacturer='Honda'); //
find custid for Honda
select name
from Customer
where custid=(select custid from Bicycle where modelno=(select modelno from
BicycleModel where manufacturer='Honda'));
c) List the bicycles purchased by the customers who have been referred by Customer “C1”.
Customer table
Bicycle table
select ModelNo
from Bicycle // Finds the modelnos which are red color
where color='red';
MODELNO
---------
2021
select manufacturer
from BicycleModel
where ModelNo=(select ModelNo from Bicycle where color='red');
MANUFACTUR
----------
Hero
select BicycleId
from Service;
BICYCLEID
----------
21
20
select ModelNo
from Bicycle
where BicycleId = (select BicycleId from Service); //Not suggested for multiple-rows
select ModelNo
from Bicycle
where BicycleId in (select BicycleId from Service); // when multiple-rows exist, then make
use of "in".
------------------------------------------------------------
declare
x number;
y number;
z number;
begin
x:=&x;
y:=&y;
z:=addnum(x,y);
dbms_output.put_line('Sum of Two number='||z);
end;
/
Output:
6. Write a PL/SQL program to Handle Exceptions.
declare
employee emp%rowtype;
employeeno emp.empno%type;
begin
employeeno:=&employeeno;
select * into employee from emp where empno=employeeno;
dbms_output.put_line('employee number is '||employee.empno);
dbms_output.put_line('employee name is '||employee.ename);
dbms_output.put_line('employee job is '||employee.job);
dbms_output.put_line('employee sal is '||employee.sal);
Exception
when NO_DATA_FOUND then
dbms_output.put_line('No such employee found');
end;
/