dbms
TRANSCRIPT
Database Applications Laboratory Manual
Exercise 1:
Consider the following relations:
Student (snum: integer, sname: string, major: string, level: string, age: integer)
Class (name: string, meets at: string, room: string, d: integer)
Enrolled (snum: integer, cname: string)
Faculty (fid: integer, fname: string, deptid: integer)
The meaning of these relations is straightforward; for example, Enrolled has one record per student-class pair such that the student is enrolled in the class. Level is a two character code with 4 different values (example: Junior: JR etc)
Write the following queries in SQL. No duplicates should be printed in any of the answers.
i. Find the names of all juniors (level = JR) who are enrolled in a class taught by Prof. Harshith
ii. Find the names of all classes that either meet in room R128 or have five or more Students enrolled.
iii. Find the names of all students who are enrolled in two classes that meet at the same time.
iv. Find the names of faculty members who teach in every room in which some class is taught.
v. Find the names of faculty members for whom the combined enrollment of the courses that they teach is less than five.
Department of Computer Science & Engineering Page 1
Database Applications Laboratory Manual
SQL>desc student;
Table Column Data Type Length Precision Scale Primary Key Nullable DefaultComment
STUDENT SNUM Number - 4 0 - - -
SNAME Varchar2 10 - - - - -
MAJOR Varchar2 5 - - - - -
SLEVEL Varchar2 7 - - - - -
AGE Number - - - - - -
SQL>insert into student('3','SUHAS','CS','FR','20');
SQL>select * from student;
SNUM SNAME MAJOR SLEVEL AGE
3 SUHAS CS FR 20
14 RAJESH CS JR 20
9 VINUTHA IS JR 22
2 VARSHA CS JR 20
5 SHRUTHI CS SO 20
6 VISHAK CS JR 20
7 VISHAL EC SR 21
10 AMIT EC SR 20
12 ATUL EC JR 20
SQL>desc enrolled;
TableColum
nData Type
Length
Precision
Scale
Primary Key
Nullable
Default
Comment
ENROLLED
SNUM Number - - - - - -
CNAME Varchar2 10 - - - - -
SQL>INSERT INTO ENROLLED VALUES(08,'SE');
SQL>SELECT * FROM ENROLLED;
Department of Computer Science & Engineering Page 2
Database Applications Laboratory Manual
SNUM CNAME
3 DS LAB
4 DS LAB
5 DS LAB
9 DBMS LAB
8 DS LAB
4 EC
3 DBMS LAB
7 EC
9 SE
2 DBMS LAB
2 DS LAB
8 SE
2 DBMS
2 C SHARP
6 DBMS LAB
9 EC
SQL>desc class;
Table
Column
Data Type
Length
Precision
Scale
Primary Key
Nullable
Default
Comment
CLASS
NAME Varchar2 10 - - - - -
MEETSAT
Varchar2 5 - - - - -
ROOM Varchar2 7 - - - - -
D Number - - - - - -
SQL>INSERT INTO CLASS VALUES('SE','9AM','R138',555);
SQL>SELECT * FROM CLASS;
NAME MEETSAT ROOM D
DBMS 11AM R128 333
SS 9AM R128 111
DS LAB 11AM R138 444
DBMS 1PM R128 555
EC 10AM R138 222
DBMS 11AM R128 333
DBMS 10AM R128 555
SE 9AM R138 555
Department of Computer Science & Engineering Page 3
Database Applications Laboratory Manual
SQL>desc faculty;
TableColum
nData Type
Length
Precision
Scale
Primary Key
Nullable
Default
Comment
FACULTY
FID Number - - - - - -
FNAME Varchar2 10 - - - - -
DEPTID Number - - - - - -
SQL>select * from faculty;
FID FNAME DEPTID
333 HARSHITH 2
555 HARSH 3
222 SNEHA 1
111 VANDANA 1
444 HARSHA 2
i) SQL>select distinct sname from student s,enrolled e,class c,faculty f
where s.slevel='JR' and s.snum=e.snum and e.cname=c.name and c.d=f.fid and f.fname='HARSHITH';
SNAME
VARSHA
ii) SQL>select name from class cwhere c.room='R128'UNION (select cname from enrolled group by cname having count(*)>=5);
DBMS
DS LAB
SS
iii) SQL>select sname from student s,enrolled e,class c where s.snum=e.snum and e.cname=c.namegroup by s.snum, sname, c.meetsathaving count(*)>1;
SNAME
VARSHA
Department of Computer Science & Engineering Page 4
Database Applications Laboratory Manual
iv) SQL>select fname from faculty f,class c where f.fid=c.dgroup by fid, fnamehaving count(distinct c.room) = (select count(distinct room) from class);
FNAME
HARSH
v) SQL>select f.fname from faculty f,class c,enrolled ewhere f.fid=c.d and c.name=e.cnamegroup by f.fid, f.fname having count (*)<5;
FNAME
HARSH
SNEHA
HARSHITH
Exercise 2:
Department of Computer Science & Engineering Page 5
Database Applications Laboratory Manual
The following relations keep track of airline flight information:
Flights (no: integer, from: string, to: string, distance: integer, Departs: time, arrives: time, price: real)
Aircraft (aid: integer, aname: string, cruisingrange: integer)
Certified (eid: integer, aid: integer)
Employees (eid: integer, ename: string, salary: integer)
Note that the Employees relation describes pilots and other kinds of employees as well; Every pilot certified for some aircraft, and only pilots are certified to fly.
Write each of the following queries in SQL.
i) Find the names of aircraft such that all pilots certified to operate them have salaries more than Rs.80, 000.
ii) For each pilot who is certified for more than three aircrafts, find the eid and the maximum cruising range of the aircraft for which she or he is certified.
iii) Find the names of pilots whose salary is less than the price of the cheapest route from Bengaluru to Frankfurt.
iv) For all aircraft with cruisingrange over 1000 Kms, .find the name of the aircraft and the average salary of all pilots certified for this aircraft.
v) Find the names of pilots certified for some Boeing aircraft.
vi) Find the aids of all aircraft that can be used on routes from Bengaluru to New Delhi.
SQL>create table flights(no number, source varchar(10),dest varchar(10),distance number, departs timestamp, arrives timestamp, price decimal(10,2));
Table created.
SQL>create table aircraft(aid number,aname varchar(10),cruisingrange number);
Table created.
SQL>create table certified(eid number,aid number);
Table created.
SQL>create table employees(eid number,ename varchar(10),salary number);
Department of Computer Science & Engineering Page 6
Database Applications Laboratory Manual
Table created.
SQL>desc flights;
Table Column Data Type Length Precision Scale Primary Key Nullable Default
Commen
t
FLIGHTS NO Number - - - - - -
SOURCE Varchar2 10 - - - - -
DEST Varchar2 10 - - - - -
DISTANCE Number - - - - - -
DEPARTS Timestamp(6) 11 - 6 - - -
ARRIVES Timestamp(6) 11 - 6 - - -
PRICE Number - 10 2 - - -
SQL>insert into flights values(100,'BANGALURU','FRANKFURT',90000,TIMESTAMP'2012-09-09 01:00:00',TIMESTAMP'2012 09 10 18:00:00',50000);
SQL>select* from flights;
NO SOURCE DEST DISTANCE DEPARTS ARRIVES PRICE
100 BANGALURU FRANKFURT 9000009-SEP-12 01.00.00.000000 AM
10-SEP-12 06.00.00.000000 PM
50000
112 BANGALURU NEW DELHI 100011-SEP-12 09.00.00.000000 AM
11-SEP-12 12.00.00.000000 PM
6000
102 BANGALURU DUBAI 3000013-SEP-12 05.00.00.000000 AM
13-SEP-12 11.00.00.000000 AM
15000
111 BANGALURU MUMBAI 150011-SEP-12 10.00.00.000000 AM
11-SEP-12 01.00.00.000000 PM
6200
101 BANGALURU NEW YORK 9000012-SEP-12 02.00.00.000000 PM
13-SEP-12 05.00.00.000000 PM
60000
110 BANGALURU FRANKFURT 9000014-SEP-12 02.00.00.000000 AM
15-SEP-12 07.00.00.000000 PM
40000
SQL>desc aircraft;
AIRCRAFT AID Number - -
- -
- -
Department of Computer Science & Engineering Page 7
Database Applications Laboratory Manual
ANAME Varchar2 10-
- -
- -
CRUISINGRANGE Number - -
- -
- -
SQL>insert into AIRCRAFT values(812,'BOEING',800);
SQL>select* from aircraft;
AID ANAME CRUISINGRANGE
999 FLIGHT 11000
998 FLIGHT 21000
888 BOEING 900
812 BOEING 800
SQL>desc certified;
TableColum
nData Type
Length
Precision
Scale
Primary Key
Nullable
Default
Comment
CERTIFIED
EID Number - - - - - -
AID Number - - - - - -
SQL>insert into CERTIFIED values(113,812);
SQL>select* from certified;
EID AID
111 888
112 998
111 812
112 999
113 812
111 999
111 998
113 888
SQL>desc employees;
Department of Computer Science & Engineering Page 8
Database Applications Laboratory Manual
EMPLOYEES EID Number - - -
- -
-
ENAME Varchar210
- -
- -
-
SALARY Number - - -
- -
-
SQL>insert into employees values (223,'KIRAN',15000);
SQL>select* from employees;
EID ENAME SALARY
113 ANJU 35000
222 CHITRA 10000
333 RAVI 25000
112 RAJ 85000
111 RAJESH 90000
223 KIRAN 15000
334 RAMESH 30000
i) SQL> select a.aid, a.aname from aircraft a, certified c, employees e
where a.aid=c.aid and c.eid=e.eid and e.salary>80000
group by a.aid,a.aname
having count(*)=(select count(*) from employees e where e.salary>80000)
AID ANAME
999 FLIGHT
998 FLIGHT
ii) SQL>Select c.eid, MAX(a.cruisingrange)from certified c, aircraft a
where c.aid=a.aid
group by c.eid
having count(*)>3;
EID MAX(A.CRUISINGRANGE)
111 21000
iii) SQL> SELECT distinct E.ENAME FROM EMPLOYEES E,certified c
Department of Computer Science & Engineering Page 9
Database Applications Laboratory Manual
WHERE e.eid=c.eid and E.SALARY < ( SELECT MIN (F.PRICE)FROM FLIGHTS F
WHERE F.SOURCE = 'BANGALURU' AND F.DEST = 'FRANKFURT');
ENAME
ANJU
iv) SQL> create table temp(aname varchar(20),salary number);
SQL>insert into temp(aname,salary) select a.aid, e.salary
From AIRCRAFT A,CERTIFIED C,EMPLOYEES E
WHERE A.CRUISINGRANGE>1000 AND A.AID=C.AID AND C.EID=E.EID ;
SQL>select aname,avg(salary) from temp group by aname;
ANAME AVG(SALARY)
998 87500
999 87500
v) SQL>SELECT DISTINCT E.ENAME FROM EMPLOYEES E, CERTIFIED C, AIRCRAFT A
WHERE E.EID = C.EID AND C.AID = A.AID AND A.ANAME = 'BOEING';
ENAMERAJESH
ANJU
vi) SQL>SELECT A.AID FROM AIRCRAFT A
WHERE A.CRUISINGRANGE > (SELECT MIN (F.DISTANCE)
FROM FLIGHTS F
WHERE F.SOURCE = 'BANGALURU'AND F.DEST = 'NEW DELHI');
AID
999
998
Department of Computer Science & Engineering Page 10
Database Applications Laboratory Manual
Exercise 3:
Consider the following database of student enrollment in courses & books adopted for each course. STUDENT (regno: string, name: string, major: string, bdate:date)
COURSE (course #:int, cname:string, dept:string)
ENROLL ( regno:string, course#:int, sem:int, marks:int)
BOOK _ ADOPTION (course# :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 at least five tuples for each relation.
iii. Demonstrate how you add a new text book to the database and make this book be adopted by
some department.
iv. Produce a list of text books (include Course #, Book-ISBN, Book-title) in the alphabetical
order for courses offered by the ‘CS’ department that use more than two books.
v. List any department that has all its adopted books published by a specific publisher.
vi. Generate suitable reports.
vii. Create suitable front end for querying and displaying the results.
1. Create the above tables by properly specifying the primary keys and the foreign keys.
SQL> create table students (regno varchar(10)primary key,name varchar(10),major varchar(10),bdate date);
Table created.
SQL>create table courses(course_id int primary key,cname varchar(10),dept varchar(20));
Table created.
SQL>create table enrolls(regno varchar(10),course_id int,se mint,marks int,primary key(regno)references students(regno),foreign key(course_id)references courses(course_id));
Department of Computer Science & Engineering Page 11
Database Applications Laboratory Manual
Table created.
SQL>create table texts(book_isbn int primary key,book_title varchar(15),publisher varchar(10),author varchar(15));
Table created.
SQL>create table book_adoptions(course_id int,sem int,book_isbn int,primary key(course_id,sem),foreign key(course_id)references courses(course_id),foreign key(book_isbn)references texts(book_isbn));
Table created.
2. Enter at least five tuples for each relation
SQL>insert into students values (‘®no”,’&name’,’&major’,’&date’);
SQL>insert into COURSES values (‘&course_id”,’&cname’,’&dept’);
SQL>insert into text values (‘&book_isbn’,’&book_title’,’&publisher’,’&author’);
SQL>insert into book_adoption values (‘&course_id’,’&sem’,’&book_isbn’);
SQL>select * from STUDENTS;
REGNO NAME MAJOR BDATE
123 akhilaa data stru 01-DEC-09
567 sinchana ada 01-DEC-09
345 anusha maths 01-DEC-09
234 arnitha data base 01-DEC-09
456 sneha microproce 01-DEC-09
Department of Computer Science & Engineering Page 12
Database Applications Laboratory Manual
Select * from courses;
COURSE# CNAME DEPT
5 Be tce
1 Be ise
2 Be CS
3 Be ece
4 Be CS
Select * from enroll;
Department of Computer Science & Engineering Page 13
REGNO COURSE# SEM MARKS
234 2 3 21
123 1 2 24
345 3 7 25
456 4 1 19
567 5 5 23
Database Applications Laboratory Manual
Select * from text;
BOOK_ISBN BOOK_TITLE PUBLISHER AUTHOR
765 Ada nandi padhma red
543 microproc mcgrachill douglashal
876 engee math prism dsc
123 let us c pearson yashwant
987 data struc nandi padhma red
654 data base pearson navathe
Select * from book_adoption;
Department of Computer Science & Engineering Page 14
Database Applications Laboratory Manual
3. Demonstrate how you add a new text book to the database and make this book be
adopted by some department.
SQL>insert into text values ('444','analog','pearson','rajesh');
Select * from text;
BOOK_ISBN BOOK_TITLE PUBLISHER AUTHOR
765 Ada nandi padhma red
543 microproc mcgrachill douglashal
876 engee math prism dsc
123 let us c pearson yashwant
987 data struc nandi padhma red
Department of Computer Science & Engineering Page 15
COURSE# SEM BOOK_ISBN
4 2 123
2 2 123
4 2 123
5 5 543
3 7 765
1 2 987
2 3 876
4 1 654
Database Applications Laboratory Manual
654 data base pearson navathe
444 Analog Pearson rajesh
SQL>insert into book_adoption values(3,4,444);
4. Produce a list of text books (include Course #, Book-ISBN, Book-title) in the alphabetical order for courses offered by the ‘CS’ department that use more than two books.
select distinct c.course#,t.book_isbn,t.book_title
from text t,book_adoption b,course c
where c.dept='CS' and c.course#=b.course# and b.book_isbn=t.book_isbn
Department of Computer Science & Engineering Page 16
COURSE# SEM BOOK_ISBN
4 2 123
2 2 123
4 2 123
5 5 543
3 7 765
1 2 987
2 3 876
4 1 654
3 4 444
Database Applications Laboratory Manual
and c.couse# in (select course# from book_adoption b group by course# having count(*)>2)
order by
t.book_title;
5. List any department that has all its adopted books published by a specific publisher.
COURSE# BOOK_ISBN BOOK_TITLE CNAME
765 ada nandi padhma red
543 microproc mcgrachill douglashal
876 engee math prism dsc
123 let us c pearson yashwant
Exercise 4:
The following tables are maintained by a book dealer.
AUTHOR (author-id:int, name:string, city:string, country:string)
PUBLISHER (publisher-id:int, name:string, city:string, country:string)
CATALOG (book-id:int, title:string, author-id:int, publisher-id:int, category-id:int, year:int,
price:int)
CATEGORY (category-id:int, description:string)
ORDER-DETAILS (order-no:int, book-id:int, quantity:int)
i. Create the above tables by properly specifying the primary keys and the foreign keys.
ii. Enter at least five tuples for each relation.
Department of Computer Science & Engineering Page 17
Database Applications Laboratory Manual
iii. Give the details of the authors who have 2 or more books in the catalog and the price of the
books is greater than the average price of the books in the catalog and the year of publication is
after 2000.
iv. Find the author of the book which has maximum sales.
v. Demonstrate how you increase the price of books published by a specific publisher by 10%.
vi. Generate suitable reports.
vii. Create suitable front end for querying and displaying the results.
1. Create the above tables by properly specifying the primary keys and the foreign keys.
Create table authors(aid int primary key,name varchar(15),city varchar(15),country varchar(15));
Table created.
Create table publishers(pi dint primary key,name varchar(15),city varchar(15),country varchar(15));
Table created.
Create table categorys(cid int primary key,description varchar(15));
Table created.
Create table catalogs(bid int primary key,title varchar(15),aid int,pi dint,cid int,year int,price int
Foreign key(aid)references authors(aid),
Foreign key(pid)referencespublishers(pid),
Foreign key(cid)references categorys(cid));
Table created.
Create table order_details(orderno int,bid int,qty int,primary key(orderno,bid),foreign key(bid) references catalogs(bid));
Table created.
Department of Computer Science & Engineering Page 18
Database Applications Laboratory Manual
2. Enter at least five tuples for each relation.
SQL>desc authors;
Table Column Data Type Length
AUTHOR1 AID Number -
NAME Varchar2 15
CITY Varchar2 15
COUNTRY Varchar2 15
SQL>insert into authors values(‘&aid’,’&name’,’&city’,’&country’);
SQL>select * from authors;
AID NAME CITY COUNTRY
5 SHIVAAM KENGARI INDIA
2 ASHOKA HOSUR INDIA
1 THILAK BANGALORE INDIA
3 RAVI CHENNAI INDIA
SQL>desc publishers;
Table Column Data Type Length
Department of Computer Science & Engineering Page 19
Database Applications Laboratory Manual
PUBLISHER PID Number -
NAME Varchar2 15
CITY Varchar2 15
COUNTRY Varchar2 15
SQL>insert into publishers values(‘&pid’,’&name’,’&city’,’&country’);
SQL>select * from publishers;
SQL>insert into categorys values(‘&cid’,’&description’);
SQL>select * from categorys;
SQL>insert into catalogs values(‘&bid’, ‘&title’, ‘&aid’, ‘&pid’, ‘&cid’, ‘&year’,’&price’);
SQL>select * from catalogs;
SQL>insert into order_details values(‘&orderno’,’&bid’,’&qty’);
SQL>select * from order_details;
3.Give the details of the authors who have 2 or more books in the catalog and the price of the
books is greater than the average price of the books in the catalog and the year of publication
is after 2000.
SQL>select distinct a.aid,a.name,a.city,a.country from authors a, catalogs c where a.aid=c.aid
and c.price>(select avg(price) from catalogs) and c.year >2000 and a.aid=(select aid from
catalogs group by aid having count(*)>=2);
AID CITY COUNTRY
Department of Computer Science & Engineering Page 20
Database Applications Laboratory Manual
5 KENGARI INDIA
2 HOSUR INDIA
4. Find the author of the book which has maximum sales.
SQL>select name, qty from order_details o,authors a,catalogs c where o.bid=c.bid and
c.aid=a.aid and qty in(select max(qty) from order_details);
NAME QTY
ASHOKA 5
5. Demonstrate how you increase the price of books published by a specific publisher by 10%.
SQL>Update catalogs set price=price*0.1+price where pid in(select pid from publisher where name=’pearson’);
Two rows updated
Department of Computer Science & Engineering Page 21
Database Applications Laboratory Manual
Exercise 5:
Consider the following database for a banking enterprise.
BRANCH (branch-name: string, branch-city: string, assets: real)
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-number: int, branch-name: string, amount: real)
BORROWER (customer-name: string, loan-number: int)
(i) Create the above tables by properly specifying the primary keys and 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 account tuples at every branch located in a specific city.
Department of Computer Science & Engineering Page 22
Database Applications Laboratory Manual
(vi) Generation of suitable reports.
(vii) Create suitable front end for querying and displaying the results.
Create table branch(bname varchar(20) primary key, bcity varchar(20),assets real);
Table created.
Create table account(accno int primary key, bname varchar(20),balance real,foreign key (bname) references branch(bname));
Table created.
Create table customer(cname varchar(20) primary key, cstreet varchar(20),city varchar(20));
Table created.
create table depositor(cname varchar(20), accno int,primary key(cname,accno),foreign key (cname) references customer(cname),foreign key (accno) references account(accno));
Table created.
Create table loan(lnumber int primary key, bname varchar(20),amount real,foreign key (bname) references branch(bname));
Table created.
Create table borrower(cname varchar(20),lnumber int,foreign key (cname) references customer(cname),foreign key(lnumber) references loan(lnumber));
Table created.
insert into branch values('jayanagar','bangalore',1500)
select * from branch
BNAME BCITYASSET
S
jayanagar bangalore 1500
Department of Computer Science & Engineering Page 23
Database Applications Laboratory Manual
majestic bangalore 2600
jubleehillshyderabad
4000
panjagutta
hyderabad
3500
krpuram chennai 1020
insert into account values(5600,'krpuram',25000)
Select * from account
ACCNO BNAME BALANCE
5467 jubleehills 16000
5060 majestic 10000
5065 jayanagar 20000
5468 jubleehills 20000
5600 krpuram 25000
5490 panjagutta 15000
5111 jayanagar 15000
insert into customer values('naveen','market road','bangalore')
select * from customer
CNAME CSTREET CITY
sudhakar udayanagar road bangalore
naveen market road bangalore
Department of Computer Science & Engineering Page 24
Database Applications Laboratory Manual
naveen j bagalur road chennai
pradeep pujagutta road hyderabad
dilip band box road bomanahalli
insert into depositor values('naveen j',5600)
select * from depositor
CNAME ACCNO
dilip 5467
dilip 5468
naveen 5111
naveen j 5600
pradeep 5490
sudhakar 5060
sudhakar 5065
insert into loan values(10,'majestic',150000)
select * from loan
ACCNO BNAME BALANCE
15 jubleehills 1000000
10 majestic 150000
18 jayanagar 150000
16 krpuram 200000
Department of Computer Science & Engineering Page 25
Database Applications Laboratory Manual
insert into borrower values('sudhakar',10)
select * from borrower
CNAME LNUMBER
dilip 15
naveen 15
naveen j 16
sudhakar 10
3. SELECT d.CNAME FROM DEPOSITOR D,ACCOUNT A,CUSTOMER C, branch b
WHERE D.ACCNO=A.ACCNO AND C.CNAME=D.CNAME AND A.BNAME=B.BNAME
AND A.BNAME IN (SELECT BNAME FROM BRANCH WHERE BCITY='bangalore')
GROUP BY d.CNAME
HAVING COUNT(DISTINCT B.BNAME)>=2;
CNAME
sudhakar
4. Select cname from customer c where not exists(select bname from branch where
city=’bangalore’ minus select bname from depositor d,account a where d.accno=a.accno and
d.cname=c.cname) and exists (select bname from branch where city=’bangalore’);
bNAME
mejestic
Department of Computer Science & Engineering Page 26
Database Applications Laboratory Manual
5. Delete from account where bname in (select bname from branch where city=’chennai’);
Two rows deleted
Department of Computer Science & Engineering Page 27