Você está na página 1de 26

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;
Com ment
-

Table
STUDENT

Column
SNUM SNAME MAJOR SLEVEL AGE

Dat a T ype
Number Varchar2 Varchar2 Varchar2 Number

Lengt h
10 5 7 -

Precisi on
4 -

Scale
0 -

Primar y Key
-

Nullabl e

Def ault
-

SQL>insert into student('3','SUHAS','CS','FR','20'); SQL>select * from student;


SNUM
3 14 9 2 5 6 7 10 12

SNAME
SUHAS RAJESH VINUTHA VARSHA SHRUTHI VISHAK VISHAL AMIT ATUL

MAJOR
CS CS IS CS CS CS EC EC EC

SLEVEL
FR JR JR JR SO JR SR SR JR

AGE
20 20 22 20 20 20 21 20 20

SQL>desc enrolled;
Table
ENROLLED

Column
SNUM CNAME

Dat a T ype
Number Varchar2

Lengt h
10

Precisi on
-

Scale
-

Primar y Key
-

Nullabl e

Def ault
-

Comment
-

SQL>INSERT INTO ENROLLED VALUES(08,'SE');

SQL>SELECT * FROM ENROLLED;


Department of Computer Science & Engineering Page 2

Database Applications Laboratory Manual

SNUM
3 4 5 9 8 4 3 7 9 2 2 8 2 2 6 9

CNAME
DS LAB DS LAB DS LAB DBMS LAB DS LAB EC DBMS LAB EC SE DBMS LAB DS LAB SE DBMS C SHARP DBMS LAB EC

SQL>desc class;
Tabl e
CLAS S

Colum n
NAME MEETSA T ROOM D

Dat a Type
Varchar2 Varchar2 Varchar2 Number

Lengt h
10 5 7 -

Precisi o n
-

Scal e
-

Primar y Key
-

Nullabl e

Def au lt
-

Comme nt
-

SQL>INSERT INTO CLASS VALUES('SE','9AM','R138',555); SQL>SELECT * FROM CLASS;


NAME
DBMS SS DS LAB DBMS EC DBMS DBMS SE

MEETS AT
11AM 9AM 11AM 1PM 10AM 11AM 10AM 9AM

ROOM
R128 R128 R138 R128 R138 R128 R128 R138

D
333 111 444 555 222 333 555 555

Department of Computer Science & Engineering

Page 3

Database Applications Laboratory Manual

SQL>desc faculty;
Table
FACULT Y

Colum n
FID FNAME DEPTID

Dat a Type
Number Varchar2 Number

Lengt h
10 -

Precisi o n
-

Scal e
-

Primar y Key
-

Nullabl e

Def au lt
-

Comme nt
-

SQL>select * from faculty;


FID
333 555 222 111 444

FNAME
HARSHITH HARSH SNEHA VANDANA HARSHA

DEPTID
2 3 1 1 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 c where 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.name group by s.snum, sname, c.meetsat having count(*)>1;

Department of Computer Science & Engineering

Page 4

Database Applications Laboratory Manual

SNAME
VARSHA

iv)

SQL>select fname from faculty f,class c where f.fid=c.d group by fid, fname having count(distinct c.room) = (select count(distinct room) from class);

FNAME
HARSH

v)

SQL>select f.fname from faculty f,class c,enrolled e where f.fid=c.d and c.name=e.cname group by f.fid, f.fname having count (*)<5;

FNAME
HARSH SNEHA HARSHITH

Department of Computer Science & Engineering

Page 5

Database Applications Laboratory Manual

Exercise 2:
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);
Department of Computer Science & Engineering Page 6

Database Applications Laboratory Manual

Table created. SQL>create table employees(eid number,ename varchar(10),salary number); Table created. SQL>desc flights;
Co mm ent
-

Table
FLIGHTS

Column
NO SOURCE DEST DISTANCE DEPARTS ARRIVES PRICE

Dat a T ype
Number Varchar2 Varchar2 Number Timestamp(6) Timestamp(6) Number

Lengt h
10 10 11 11 -

Precisi on
10

Scale
6 6 2

Primar y Key
-

Nullabl e

Def ault
-

SQL>insert into flights values(100,'BANGALURU','FRANKFURT',90000,TIMESTAMP'201209-09 01:00:00',TIMESTAMP'2012 09 10 18:00:00',50000); SQL>select* from flights;

NO
100 112 102 111 101 110

SOURCE
BANGALURU BANGALURU BANGALURU BANGALURU BANGALURU BANGALURU

DEST
FRANKFURT NEW DELHI DUBAI MUMBAI NEW YORK FRANKFURT

DIST ANCE
90000 1000 30000 1500 90000 90000

DEP ARTS
09-SEP-12 01.00.00.000000 AM 11-SEP-12 09.00.00.000000 AM 13-SEP-12 05.00.00.000000 AM 11-SEP-12 10.00.00.000000 AM 12-SEP-12 02.00.00.000000 PM 14-SEP-12 02.00.00.000000 AM

ARRI VES
10-SEP-12 06.00.00.000000 PM 11-SEP-12 12.00.00.000000 PM 13-SEP-12 11.00.00.000000 AM 11-SEP-12 01.00.00.000000 PM 13-SEP-12 05.00.00.000000 PM 15-SEP-12 07.00.00.000000 PM

PRICE
50000 6000 15000 6200 60000 40000

Department of Computer Science & Engineering

Page 7

Database Applications Laboratory Manual

SQL>desc aircraft;
AIRCRAFT AID ANAME CRUISINGRANGE Number Varchar2 Number 10 -

SQL>insert into AIRCRAFT values(812,'BOEING',800); SQL>select* from aircraft;


AI D
999 998 888 812

AN AME
FLIGHT FLIGHT BOEING BOEING

CRUI SINGRANGE
11000 21000 900 800

SQL>desc certified;
Table
CERTIFI ED

Colum n
EID AID

Dat a Type
Number Number

Lengt h
-

Precisi on
-

Scal e
-

Primar y Key
-

Nullab le

Def au lt
-

Comme nt
-

SQL>insert into CERTIFIED values(113,812); SQL>select* from certified;


EID
111 112 111 112 113 111 111 113

AI D
888 998 812 999 812 999 998 888

Department of Computer Science & Engineering

Page 8

Database Applications Laboratory Manual

SQL>desc employees;
EMPLOYEES EID ENAME SALARY Number Varchar2 Number 10 -

SQL>insert into employees values (223,'KIRAN',15000); SQL>select* from employees;


EID
113 222 333 112 111 223 334

ENAME
ANJU CHITRA RAVI RAJ RAJESH KIRAN RAMESH

SAL ARY
35000 10000 25000 85000 90000 15000 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)
AI D
999 998

AN AME
FLIGHT 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
111

MAX( A. CRUISINGR ANGE)


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;
AN AME
998 999

AVG( SAL ARY)


87500 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'; ENAME
RAJESH 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');
AI D
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 varchar(10),bdate date); Table created.

(regno

varchar(10)primary

key,name

varchar(10),major

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 (&regno,&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 123 567 345 234 456

NAME akhilaa sinchana anusha arnitha sneha

MAJOR data stru ada maths data base microproce

BDATE 01-DEC-09 01-DEC-09 01-DEC-09 01-DEC-09 01-DEC-09

Department of Computer Science & Engineering

Page 12

Database Applications Laboratory Manual

Select * from courses;

COURSE# 5 1 2 3 4 Be Be Be Be Be

CNAME tce ise CS ece CS

DEPT

Select * from enroll;

REGNO 234 123 345 456 567 2 1 3 4 5

COURSE# 3 2 7 1 5

SEM

MARKS 21 24 25 19 23

Department of Computer Science & Engineering

Page 13

Database Applications Laboratory Manual

Select * from text;

BOOK_ISBN 765 543 876 123 987 654

BOOK_TITLE Ada microproc engee math let us c data struc data base

PUBLISHER nandi mcgrachill prism pearson nandi pearson

AUTHOR padhma red douglashal dsc yashwant padhma red navathe

Select * from book_adoption;

COURSE# 4 2 4 5 3 1 2 4 2 2 2 5 7 2 3 1

SEM

BOOK_ISBN 123 123 123 543 765 987 876 654

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 765 543 876 123 987 654 444 BOOK_TITLE Ada microproc engee math let us c data struc data base Analog PUBLISHER nandi mcgrachill prism pearson nandi pearson Pearson AUTHOR padhma red douglashal dsc yashwant padhma red navathe rajesh

SQL>insert into book_adoption values(3,4,444); COURSE# 4 2 4 5 3 1 2 4 3 2 2 2 5 7 2 3 1 4 SEM BOOK_ISBN 123 123 123 543 765 987 876 654 444

Department of Computer Science & Engineering

Page 15

Database Applications Laboratory Manual

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 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#
765 543 876 123

BOOK_ISBN ada microproc engee math let us c

BOOK_TITLE
nandi mcgrachill prism pearson

CNAME
padhma red douglashal dsc yashwant

Department of Computer Science & Engineering

Page 16

Database Applications Laboratory Manual

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. 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.

Department of Computer Science & Engineering

Page 17

Database Applications Laboratory Manual

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. 2. Enter at least five tuples for each relation. SQL>desc authors;

Table AUTHOR1 AID

Column

Data Type Number Varchar2 Varchar2 Varchar2 15 15 15

Length

NAME CITY COUNTRY

SQL>insert into authors values(&aid,&name,&city,&country); SQL>select * from authors;

Department of Computer Science & Engineering

Page 18

Database Applications Laboratory Manual

AID 5 2 1 3

NAME SHIVAAM ASHOKA THILAK RAVI

CITY KENGARI HOSUR BANGALORE CHENNAI

COUNTRY INDIA INDIA INDIA INDIA

SQL>desc publishers;

Table PUBLISHER PID

Column

Data Type Number Varchar2 Varchar2 Varchar2 15 15 15

Length

NAME CITY COUNTRY

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);

Department of Computer Science & Engineering

Page 19

Database Applications Laboratory Manual

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 5 CITY KENGARI HOSUR
COUNTRY

INDIA 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. Demonstrate how you increase the price of books published by a specific publisher by 10%.

Department of Computer Science & Engineering

Page 20

Database Applications Laboratory Manual

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) (ii) Create the above tables by properly specifying the primary keys and foreign keys. 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.

(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));

Department of Computer Science & Engineering

Page 22

Database Applications Laboratory Manual

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 jayanagar majestic jubleehills panjagutta krpuram BCITY bangalore bangalore hyderabad hyderabad chennai ASSETS 1500 2600 4000 3500 1020

insert into account values(5600,'krpuram',25000) Select * from account ACCNO 5467 5060 5065 BNAME jubleehills majestic jayanagar BALANCE 16000 10000 20000

Department of Computer Science & Engineering

Page 23

Database Applications Laboratory Manual

5468 5600 5490 5111

jubleehills krpuram panjagutta jayanagar

20000 25000 15000 15000

insert into customer values('naveen','market road','bangalore')

select * from customer CNAME sudhakar naveen naveen j pradeep dilip CSTREET udayanagar road market road bagalur road pujagutta road band box road CITY bangalore bangalore chennai hyderabad bomanahalli

insert into depositor values('naveen j',5600) select * from depositor CNAME dilip dilip naveen naveen j ACCNO 5467 5468 5111 5600

Department of Computer Science & Engineering

Page 24

Database Applications Laboratory Manual

pradeep sudhakar sudhakar

5490 5060 5065

insert into loan values(10,'majestic',150000) select * from loan ACCNO 15 10 18 16 BNAME jubleehills majestic jayanagar krpuram BALANCE 1000000 150000 150000 200000

insert into borrower values('sudhakar',10) select * from borrower CNAME dilip naveen naveen j sudhakar LNUMBER 15 15 16 10

Department of Computer Science & Engineering

Page 25

Database Applications Laboratory Manual

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

5. Delete from account where bname in (select bname from branch where city=chennai);

Two rows deleted

Department of Computer Science & Engineering

Page 26

Você também pode gostar