Database Laboratory Manual
Dept. of MCA, RNSIT
Bangalore-98
Eduoncloud.com Page 1
Instructions for the Exercises:
1. Draw ER diagram based on given scenario with various Constraints.
2. Create Relational Database Schema based on the above scenario using Mapping Rules.
3. Perform the given queries using any RDBMS Environment.
4. Suitable tuples have to be entered so that queries are executed correctly
5. The results of the queries may be displayed directly
Exercise: 1
Notown Records has decided to store information about musicians who perform on its
albums. Each musician that records at Notown has an SSN, a name, an address, and a phone
number. No musician has more than one phone. Each instrument used in songs recorded at
Notown has a unique identification number, a name (e.g., guitar, synthesizer, flute) and a
musical key (e.g., C, B-flat, E-flat). Each album recorded on the Notown label has a unique
identification number, a title, a copyright date and a format (e.g., CD or MC). Each song
recorded at Notown has a title and an author. Each musician may play several instruments, and
a given instrument may be played by several musicians. Each album has a number of songs on
it, but no song may appear on more than one album. Each song is performed by one or more
musicians, and a musician may perform a number of songs. Each album has exactly one musician who acts as its producer. A musician may produce several albums.
Queries
a) List musician name, title of the song which he has played, the album in which song has
occulted.
b) List the details of songs which are performed by more than 3 musicians.
c) List the different instruments played by the musicians and the average number of musicians
who play the instrument.
d) Retrieve album title produced by the producer who plays guitar as well as flute and has
produced no of songs greater than the average songs produced by all producers. e) List the details of musicians who can play all the instruments present.
Database Laboratory Manual
Dept. of MCA, RNSIT
Bangalore-98
Eduoncloud.com Page 2
ER diagram
Author
Database Laboratory Manual
Dept. of MCA, RNSIT
Bangalore-98
Eduoncloud.com Page 3
Relational schema:
MUSICIAN
Ssn Name Addrs Phone
PLAYS
Id Ssn
INSTRUMENT
Id Iname M_key
PERFORMS
Ssn Title
SONG
Title Author Aid
ALBUM
Aid Title C_date Format Producer_ssn
Database Laboratory Manual
Dept. of MCA, RNSIT
Bangalore-98
Eduoncloud.com Page 4
Table creation:
create table musician
(ssn number(3) primary key,
name varchar(30),
addr varchar(30),
phone number(10) unique
);
create table instrument
(id number(4) primary key,
iname varchar(30),
mkey varchar(10));
create table plays
(ssn number(3) references musician(ssn),
id number(4) references instrument(id),
primary key(ssn,id));
create table songs
(title varchar (10) primary key,
author varchar(20),
aid number(4) references album(aid));
create table performs
(title varchar(10) references songs(title),
ssn number(4) references musician(ssn),
primary key(title,ssn));
create table album
(aid number(4) primary key,
atitle varchar(30) unique,
format varchar(5),
rdate date,
ssn number(3) references musician(ssn));
Table Description:
desc musician
Name Null? Type
----------------------------------------- -------- ---------------------
SSN NOT NULL NUMBER(3)
NAME VARCHAR2(30)
ADDR VARCHAR2(30)
PHONE NUMBER(10)
Database Laboratory Manual
Dept. of MCA, RNSIT
Bangalore-98
Eduoncloud.com Page 5
desc instrument
Name Null? Type
----------------------------------------- -------- ---------------------
ID NOT NULL NUMBER(4)
INAME VARCHAR2(30)
MKEY VARCHAR2(10)
desc plays
Name Null? Type
----------------------------------------- -------- ---------------------
SSN NOT NULL NUMBER(3)
ID NOT NULL NUMBER(4)
desc songs
Name Null? Type
----------------------------------------- -------- ---------------------
TITLE NOT NULL VARCHAR2(10)
AUTHOR VARCHAR2(20)
AID NUMBER(4)
desc album
Name Null? Type
----------------------------------------- -------- ---------------------
AID NOT NULL NUMBER(4)
ATITLE VARCHAR2(30)
FORMAT VARCHAR2(5)
RDATE DATE
SSN NUMBER(3)
desc performs
Name Null? Type
----------------------------------------- -------- ---------------------
TITLE NOT NULL VARCHAR2(10)
SSN NOT NULL NUMBER(4)
Values inside Tables: select * from musician;
SSN NAME ADDR PHONE
---------- ------------- ------------- ----------------
123 rahman bangalore 9999999999
124 ilayaraja chennai 8888888888
125 pritam mumbai 7777777777
126 harris hyderbad 9999998888
127 jessi cochin 9844498444
Database Laboratory Manual
Dept. of MCA, RNSIT
Bangalore-98
Eduoncloud.com Page 6
select * from instrument;
ID INAME MKEY
---------- ------------------------------ ----------
1 guitar high
2 flute flat
3 violin low
4 drums rock
5 piano high
6 tabala low
select * from plays;
SSN ID
---------- ----------
123 1
123 2
124 4
123 4
126 6
127 5
124 3
123 3
123 5
123 6
select * from songs;
TITLE AUTHOR AID
---------- -------------------- ----------
jaadu kiran 102
sri sai krishna 101
janam hari 101
sanam ram 104
stotra charan 103
select * from album;
AID ATITLE FORMA RDATE SSN
---------- ------------------------------ ----- --------- ----------
101 abc cd 12-MAR-99 123
102 xyz mp3 12-JUN-99 123
103 mno mp4 15-MAR-99 124
104 pqr tape 19-DEC-90 125
105 aaa cd 10-JAN-10 126
106 bbb cd 10-MAR-99 123
107 ccc tape 12-APR-93 123
Database Laboratory Manual
Dept. of MCA, RNSIT
Bangalore-98
Eduoncloud.com Page 7
select * from performs;
TITLE SSN
---------- ----------
jaadu 123
jaadu 124
sri sai 125
sanam 126
stotra 127
Queries:
query 1:
select m.name , s.title , a.aid from musician m, performs p, songs s, album a
where m.ssn=p.ssn and p.title =s.title and s.aid=a.aid;
NAME TITLE AID
------------------------------ ---------- ----------
rahman jaadu 102
ilayaraja jaadu 102
pritam sri sai 101
harris sanam 104
jessi stotra 103
query2:
select title, author from songs where title=(select title from
performs p group by title having count(*)>1);
TITLE AUTHOR
---------- --------------------
jaadu kiran
query 3:
select iname, (select count(ssn) from plays where id=i.id group by id)
as No_of_musicians from instrument I;
INAME NO_OF_MUSICIANS
------------------------------ ---------------
guitar 1
flute 1
violin 2
drums 2
piano 2
tabala 2
query 4:
select atitle from album where ssn in
(select m.ssn from musician m where m.ssn
in (select ssn from instrument i, plays p where
p.id=i.id and iname in ('guitar','flute'))) and
ssn=(select ssn from album group by ssn having
count(ssn)= (select count(distinct ssn) from album));
Database Laboratory Manual
Dept. of MCA, RNSIT
Bangalore-98
Eduoncloud.com Page 8
ATITLE
-------
abc
xyz
www
sss
OR
select atitle from album where ssn=(select m.ssn from musician m where m.ssn=
(select distinct ssn from (select * from instrument i ,plays p where
p.id=i.id) where iname in ('guitar','flute')))
and ssn=(select ssn from album group by ssn having count(ssn)= (select
count(distinct ssn) from album))
ATITLE
--------------------------
abc
xyz
sss
www
query 5:
select (select name from musician where ssn=p.ssn)
as musician from plays p group by ssn having
count(id)=(select count(i.id) from instrument i);
MUSICIAN
-----------
Rahman
Database Laboratory Manual
Dept. of MCA, RNSIT
Bangalore-98
Eduoncloud.com Page 9
Exercise: 2
Professors have a PROFID, a name, an age, a rank, and a research specialty. Projects have a project
number, a sponsor name (e.g. UGC/AICTE/...), a starting date, an ending date, and a budget. Graduate
students have an USN, a name, an age, and a degree program (e.g. MCA/ MPhil/BE/ME ..). Each
project is managed exactly by one professor (known as the project's principal investigator). Each project
is worked on by one or more professors (known as the project's co-investigators). Professors can
manage/work on multiple projects. Each project is worked on by one or more graduate students (known
as the project's research assistants). Graduate students can work on multiple projects. Each professor can
supervise many students. A student who is working on a project can be supervised by only one
professor.
Queries
a) Retrieve the names of all professors who do not have an ongoing project of more than 1
lakh.
b) Retrieve the names of all graduate students along with their professors under whom they
work and project sponsor.
c) List the professors and sum of the budget of their projects started after 2005 but ended in
2010.
d) List the names of professors who has a total worth of project greater than the average budget
of projects sanctioned
e) List the professors who work on all the projects.
Database Laboratory Manual
Dept. of MCA, RNSIT
Bangalore-98
Eduoncloud.com Page 10
ER DIAGRAM
Database Laboratory Manual
Dept. of MCA, RNSIT
Bangalore-98
Eduoncloud.com Page 11
RELATIONAL SCHEMA
PROFESSOR
PROFID NAME AGE RANK RESEARCH
PROJECT
PNO SPONSOR SDATE BUDGET EDATE PROFID
STUDENT
USN SNAME AGE DEGREE PROFID
P_WORKSON
PROFID PNO
S_WORKSON
USN PNO
Database Laboratory Manual
Dept. of MCA, RNSIT
Bangalore-98
Eduoncloud.com Page 12
Table creation:
create table professor
(profid number(3) primary key,
name varchar(20),
age number(3),
rank number(3),
research varchar(20));
create table projects
(pno number(3) primary key,
sponsor varchar(10),
sdate date,
edate date,
budget number(5),
p_investigator number(3) references professor(profid));
create table student
(usn number(5) primary key,
sname varchar(20),
age number(3),
degree varchar(10),
profid number(3) references professor(profid));
create table pworkson
(profid number(3) references professor(profid),
pno number(3) references projects(pno),
primary key(profid,pno))
create table sworkson
(usn number(5) references student(usn),
pno number(3) references projects(pno),
primary key(usn,pno));
Table Description:
desc professor
Name Null? Type
----------------------------------------- -------- ---------------------
PROFID NOT NULL NUMBER(3)
NAME VARCHAR2(20)
AGE NUMBER(3)
Database Laboratory Manual
Dept. of MCA, RNSIT
Bangalore-98
Eduoncloud.com Page 13
RANK NUMBER(3)
RESEARCH VARCHAR2(20)
desc projects
Name Null? Type
----------------------------------------- -------- ---------------------
PNO NOT NULL NUMBER(3)
SPONSOR VARCHAR2(10)
SDATE DATE
EDATE DATE
BUDGET NUMBER(5)
P_INVESTIGATOR NUMBER(3)
desc student
Name Null? Type
----------------------------------------- -------- ---------------------
USN NOT NULL NUMBER(5)
SNAME VARCHAR2(20)
AGE NUMBER(3)
DEGREE VARCHAR2(10)
PROFID NUMBER(3)
desc pworkson
Name Null? Type
----------------------------------------- -------- ---------------------
PROFID NOT NULL NUMBER(3)
PNO NOT NULL NUMBER(3)
desc sworkson
Name Null? Type
----------------------------------------- -------- ---------------------
USN NOT NULL NUMBER(5)
PNO NOT NULL NUMBER(3)
Values inside Tables:
select * from professor;
PROFID NAME AGE RANK RESEARCH
---------- -------------------- ---------- ---------- -------------------
1 vishwanath 35 1 sp
2 hemanth 35 1 os
3 raghu 31 1 unix
4 kumar 28 1 oops
5 basavaraju 28 1 oomd
select * from professor;
PROFID NAME AGE RANK RESEARCH
Database Laboratory Manual
Dept. of MCA, RNSIT
Bangalore-98
Eduoncloud.com Page 14
---------- -------------------- ---------- ---------- -------------------
1 vishwanath 35 1 sp
2 hemanth 35 1 os
3 raghu 31 1 unix
4 kumar 28 1 oops
5 basavaraju 28 1 oomd
select * from projects;
PNO SPONSOR SDATE EDATE BUDGET P_INVESTIGATOR
---------- ---------- --------- --------- ---------- --------------
111 vtu 12-JAN-12 12-JAN-14 10000 1
222 govt 18-MAR-13 18-MAR-15 40000 2
333 vtu 22-AUG-14 22-AUG-16 60000 3
444 central 20-APR-14 20-APR-15 75000 4
555 central 25-FEB-12 25-FEB-14 90000 5
select * from student
USN SNAME AGE DEGREE PROFID
---------- -------------------- ---------- ---------- ----------
123 shashi 22 mca 1
124 rajath 21 mca 2
125 harish 21 be 3
126 ram 24 msc 4
127 kiran 22 mca 5
select * from pworkson;
PROFID PNO
---------- ----------
1 111
2 222
3 333
4 444
5 555
2 111
2 333
2 444
2 555
select * from sworkson;
USN PNO
---------- ----------
123 111
124 222
125 333
126 444
127 555
Database Laboratory Manual
Dept. of MCA, RNSIT
Bangalore-98
Eduoncloud.com Page 15
Queries:
Query 1:
select name from professor p , pworkson pw , projects pj
where p.profid = pw.profid and pw.pno=pj.pno and budget <100000;
NAME
--------------------
hemanth
raghu
kumar
basavaraju
query 2:
select sname,name as "professor", sponsor from professor p ,
student s , sworkson sw , projects pr
where p.profid=s.profid and s.usn=sw.usn and sw.pno=pr.pno
SNAME professor SPONSOR
-------------------- -------------------- ----------
shashi vishwanath vtu
rajath hemanth govt
harish raghu vtu
ram kumar central
kiran basavaraju central
Queries3:
select name, (select sum(budget) from projects where
p_investigator=p.profid group by
p_investigator) as "total budget" from professor p , projects
where profid=p_investigator and sdate like '%12' and edate like '%14'
NAME total budget
-------------------- ------------
vishwanath 120000
basavaraju 90000
query4:
select name from professor ,projects where profid= p_investigator
and budget>(select avg(budget) from projects)
NAME
----------------
vishwanath
basavaraju
Database Laboratory Manual
Dept. of MCA, RNSIT
Bangalore-98
Eduoncloud.com Page 16
query5:
select name from professor where profid=
(select p.profid from pworkson p group by p.profid
having count(p.profid)=(select count(pno) from projects))
NAME
------------------
hemanth
Exercise: 3
A bank has many branches and a large number of customers. Bank is identified by its code.
Other details like name, address and phone for each bank are also stored. Each branch is
identified by its bank. Branch has name, address and phone. A customer can open different
kinds of accounts with the branches. An account can belong to more than one customer.
Customers are identified by their SSN, name, address and phone number. Age is used as a
factor to check whether customer is a major. There are different types of loans, each identified
by a loan number. A customer can take more than one type of loan and a loan can be given to
more than one customer. Loans have a duration and interest rate. Make suitable assumptions
and use them in showing maximum and minimum cardinality ratios.
Queries:
a) List the details of customers who have joint account and also have at least one loan.
b) List the details of the branch which has given maximum loan.
c) List the details of saving accounts opened in the SBI branches located at Bangalore.
d) List the name of branch along with its bank name and total amount of loan given by it. e) Retrieve the names of customers who have accounts in all the branches located in a specific city
Database Laboratory Manual
Dept. of MCA, RNSIT
Bangalore-98
Eduoncloud.com Page 17
ER diagram:
Database Laboratory Manual
Dept. of MCA, RNSIT
Bangalore-98
Eduoncloud.com Page 18
Relational schema
Table Creation:
create table bank
(code number(3) primary key,
name varchar(20),
addr varchar(30),
phone number(10));
Database Laboratory Manual
Dept. of MCA, RNSIT
Bangalore-98
Eduoncloud.com Page 19
create table customer
(ssn number(3) primary key,
cname varchar(15),
addr varchar(20),
phone number(10))
create table loan
(lno number(3) primary key,
duration number(2),
interest number(4,2)
bname varchar(20),
code number(3),
foreign key(bname,code) references branch(bname,code));
create table branch
(bname varchar(20),
code number(3),
addr varchar(20),
phone number(10),
primary key(bname,code),
foreign key(code) references bank(code));
create table account
( acno number(3) primary key,
ac_type varchar(10),
bname varchar(20),
code number(3),
foreign key(bname,code) references branch(bname,code));
create table custaccount
(ssn number(3) references customer(ssn),
acno number(3) references account(acno),
primary key(ssn,acno))
create table custloan
(ssn number(3) references customer(ssn),
lno number(3) references loan(lno),
primary key(ssn,lno));
Describe tables:
desc bank
Name Null? Type
----------------------------------------- -------- ---------------------
CODE NOT NULL NUMBER(3)
NAME VARCHAR2(20)
ADDR VARCHAR2(30)
PHONE NUMBER(10)
Database Laboratory Manual
Dept. of MCA, RNSIT
Bangalore-98
Eduoncloud.com Page 20
desc branch
Name Null? Type
----------------------------------------- -------- ---------------------
BNAME NOT NULL VARCHAR2(20)
CODE NOT NULL NUMBER(3)
ADDR VARCHAR2(20)
PHONE NUMBER(10)
desc customer
Name Null? Type
----------------------------------------- -------- ---------------------
SSN NOT NULL NUMBER(3)
CNAME VARCHAR2(15)
ADDR VARCHAR2(20)
PHONE NUMBER(10)
desc account
Name Null? Type
----------------------------------------- -------- ---------------------
ACNO NOT NULL NUMBER(3)
AC_TYPE VARCHAR2(10)
BNAME VARCHAR2(20)
CODE NUMBER(3)
desc custloan
Name Null? Type
----------------------------------------- -------- ---------------------
SSN NOT NULL NUMBER(3)
LNO NOT NULL NUMBER(3)
desc custaccount
Name Null? Type
----------------------------------------- -------- ---------------------
SSN NOT NULL NUMBER(3)
ACNO NOT NULL NUMBER(3)
desc loan
Name Null? Type
----------------------------------------- -------- ---------------------
LNO NOT NULL NUMBER(3)
DURATION NUMBER(2)
INTEREST NUMBER(4,2)
AMOUNT NUMBER(10)
BNAME VARCHAR2(20)
CODE NUMBER(3)
Values in Tables:
select * from bank;
Database Laboratory Manual
Dept. of MCA, RNSIT
Bangalore-98
Eduoncloud.com Page 21
CODE NAME ADDR PHONE
---------- -------------------- ------------------------------ ----------
1 sbm bangalore 99999999
2 sbi mysore 777777777
3 canara mangalore 98989988
4 corp banagalore 987889778
5 axis bangalore 987667766
select * from branch;
BNAME CODE ADDR PHONE
-------------------- ---------- -------------------- ----------
rns 3 bangalore 876376372
rrnagar 1 bangalore 985431323
jayanagar 2 bangalore 985243242
vijaynagar 4 bangalore 965454252
rajajinagar 5 bangalore 8776673782
rns2 3 mysore 8676767423
banashankari 4 bangalore 667342434
mysore1 2 mysore 764783678
rns5 3 mysore 878765466
select * from customer;
SSN CNAME ADDR PHONE
---------- --------------- -------------------- ----------
111 vishwanath bangalore 987567898
222 raghu mysore 786989878
333 nandan delhi 978695433
444 hemanth bangalore 914265623
555 basavaraj mysore 9535626726
select * from loan;
LNO DURATION INTEREST AMOUNT BNAME CODE
---------- ---------- ---------- ---------- -------------------- -----
123 5 10.5 50000 rrnagar 1
124 8 7.5 40000 rajajinagar 5
125 12 8 60000 vijaynagar 4
126 3 9 75000 rrnagar 1
127 14 12 100000 rns 3
128 5 8 10000 rrnagar 1
select * from custaccount;
SSN ACNO
---------- ----------
222 106
222 107
Database Laboratory Manual
Dept. of MCA, RNSIT
Bangalore-98
Eduoncloud.com Page 22
555 105
111 101
111 102
222 103
333 104
444 105
select * from custaccount;
SSN ACNO
---------- ----------
222 106
222 107
555 105
111 101
111 102
222 103
333 104
444 105
select * from custloan;
SSN LNO
---------- ----------
333 125
444 125
555 127
555 126
111 123
222 124
select * from account;
ACNO AC_TYPE BNAME CODE
---------- ---------- -------------------- ----------
101 sb rns 3
102 fd rrnagar 1
103 sb rajajinagar 5
104 rd vijaynagar 4
105 sb jayanagar 2
106 sb rns2 3
107 sb rns5 3
Database Laboratory Manual
Dept. of MCA, RNSIT
Bangalore-98
Eduoncloud.com Page 23
Queries:
1) select * from customer where ssn in(select c.ssn from custaccount c , custaccount
d where c.ssn<>d.ssn and c.acno=d.acno) and ssn in(select distinct ssn from
custloan)
SSN CNAME ADDR PHONE
---------- --------------- -------------------- ----------
444 hemanth bangalore 914265623
555 basavaraj mysore 9535626726
2) select * from branch where (bname,code) in (select bname,code from
loan group by (bname,code) having sum(amount)>max(amount)); BNAME CODE ADDR PHONE
-------------------- ---------- -------------------- ----------
rrnagar 1 bangalore 985431323
3) select * from account where ac_type='sb' and (bname,code) in (select
bname,code from branch where addr='bangalore' and code in (select code from
bank where name='sbi'))
ACNO AC_TYPE BNAME CODE
----- ---------- -------------------- ----------
105 sb jayanagar 2
4) select bname, code, sum(amount) from loan group by (bname,code);
BNAME CODE SUM(AMOUNT)
-------------------- ---------- -----------
rns 3 100000
rrnagar 1 135000
vijaynagar 4 60000
rajajinagar 5 40000
Database Laboratory Manual
Dept. of MCA, RNSIT
Bangalore-98
Eduoncloud.com Page 24
5) select cname from customer where ssn in ( select ssn from custaccount where acno in
(select acno from account where (bname,code) in (select bname , code from
branch where addr='mysore' group by (bname,code))));
CNAME
-------
raghu
Exercise: 4
Patients are identified by an SSN, and their names, addresses, and ages must be recorded.
Doctors are identified by an SSN. For each doctor, the name, specialty, and years of experience
must be recorded. Each pharmaceutical company is identified by name; it has an address and
one phone number. For each drug, the trade name and formula must be recorded. Each drug is
sold by a given pharmaceutical company, and the trade name identifies a drug uniquely from
among the products of that company. Each pharmacy has a name, address, and phone number.
Each patient is checked up by some doctor. Every doctor has at least one patient. Each
pharmacy sells several drugs and has a price for each. A drug could be sold at several
pharmacies, and the price could vary from one pharmacy to another. Doctors prescribe drugs for
patients. A doctor could prescribe one or more drugs for several patients, and a patient could
obtain prescriptions from several doctors. Each prescription has a date and a quantity associated
with it. Pharmaceutical companies have long-term contracts with pharmacies. A pharmaceutical
company can contract with several pharmacies, and a pharmacy can contract with several
pharmaceutical companies. For each contract, you have to store a start date, an end date, supervisor and the text of the contract.
Queries:
a) List the details of patients who are 20 years old and have been checked by eye-specialist.
b) List the details of doctors who have given the prescription to more than 20 patients in year
2013.
c) List the details of pharmaceutical companies who supply drug to more than 10 pharmacies in
the same city where company is located.
d) List the details of drug supplied by only one pharmaceutical company. e) List the details of drug supplied by all pharmaceutical companies.
Database Laboratory Manual
Dept. of MCA, RNSIT
Bangalore-98
Eduoncloud.com Page 25
ER & Relation Schema:
Database Laboratory Manual
Dept. of MCA, RNSIT
Bangalore-98
Eduoncloud.com Page 26
Database Laboratory Manual
Dept. of MCA, RNSIT
Bangalore-98
Eduoncloud.com Page 27
Create table patient (ssn number(3) primary key, Name varchar(20), Addr varchar(30), Age number(3)); Create table doctor (ssn number(3) primary key, Name varchar(20), Addr varchar(30), Speciality varchar(30), Experience number(3)); Create table pharma_company (name varchar(20) primary key, Addr varchar(20), Phone number(10)); Create table pharmacy (pname varchar(20) primary key, Addr varchar(30), Phone number(10)); Create table drug (tname varchar(20) primary key, Formula varchar(20)); Create table docpatient (pssn number(3) references patient, Dssc number(3) references doctor, Primary key(pssn,dssn)); create table pcomp_drug(tname varchar(20) references drug,
name varchar(20) references PHARMA_COMPANY,
primary key(tname,name));
create table pharma_contract
(name varchar(20) references pharma_company,
phname varchar(20) references PHARMACY,
sdate date,
edate date,
supervisor varchar(30),
text varchar(30));
create table pharmacy_drug
(phname varchar(20) references PHARMACY,
tname varchar(2) references drug,
primary key(phname,tname));
Database Laboratory Manual
Dept. of MCA, RNSIT
Bangalore-98
Eduoncloud.com Page 28
Table created.
create table prescription
(pssn number(3) references patient,
dssn number(3) references doctor,
tname varchar(2) references drug,
qty number(3),
pdate date);
select * from patient;
SSN NAME ADDR AGE
---------- -------------------- -------------------- ----------
1 harish bangalore 28
2 raj mysore 55
3 thomas delhi 55
4 kishan bangalore 34
5 ram mangalore 76
select * from doctor;
DSSN NAME SPECIALITY EXPERIENCE
---------- -------------------- -------------------- ----------
123 nagesh ortho 8
124 raghu eye 8
125 hemanth surgeon 12
126 vishwanath physician 15
127 kumar skin 5
select * from drug;
TRNAME FORMULA
-------------------- ----------
saridon abc
anacin xyz
crocin www
dolopar ggg
cpm qqq
select * from pharmacy;
PHNAME ADDR PHONE
-------------------- -------------------- ----------
medplus rr nagar 87766565
apollo bangalore 90987655
sai mysore 987668877
maruthi bangalore 967565697
ganesh bangalore 89876566
select * from pharma_company;
Database Laboratory Manual
Dept. of MCA, RNSIT
Bangalore-98
Eduoncloud.com Page 29
NAME ADDR PHONE
-------------------- -------------------- ----------
cipla bangalore 97765545
ranbaxy delhi 986568799
drreddy bangalore 977654689
himalaya bangalore 985754687
johnson delhi 966555678
select * from prescription;
PSSN DSSN TNAME QTY PDATE
---------- ---------- -------------------- ---------- ---------
1 123 saridon 5 12-MAR-12
2 124 crocin 8 15-MAR-13
3 125 cpm 8 10-JUL-14
4 126 dolopar 12 10-MAY-13
5 127 crocin 7 16-JAN-13
select * from pcomp_drug;
TNAME NAME
-------------------- --------------------
saridon cipla
anacin cipla
crocin himalaya
dolopar ranbaxy
select * from pharmacy_drug;
PHNAME TNAME
-------------------- --------------------
medplus dolopar
apollo crocin
apollo saridon
sai crocin
ganesh cpm
select * from pharma_contract;
NAME PHNAME SDATE EDATE SUPERVISOR TEXT
---------- ------------ ------ --------- --------- ----------------------
cipla medplus 12-JAN-77 12-MAR-16 abc ssss
ranbaxy apollo 12-JUN-14 12-JUN-17 aaa rrr
Database Laboratory Manual
Dept. of MCA, RNSIT
Bangalore-98
Eduoncloud.com Page 30
himalaya sai 12-JUL-12 12-JUN-16 fas eeee
drreddy ganesh 12-MAY-13 22-MAR-14 fgf ttt
select * from docpatient;
DSSN PSSN
---------- ----------
123 1
123 2
124 1
125 3
126 4
127 5
6 rows selected.
Queries:
1) select * from patient where age=28 and ssn in
(select pssn from docpatient where dssn in
(select dssn from doctor where SPECIALITY='ortho'))
SSN NAME ADDR AGE
----- -------------------- -------------------- ----------
1 harish bangalore 28
2)select * from doctor where dssn in (select dssn from prescription
where pdate like '%13' group by dssn having count(pssn)>=2);
DSSN NAME SPECIALITY EXPERIENCE
----- -------------------- -------------------- ----------
124 raghu eye 8
3)select * from pharma_company where name in
(select name from pharma_contract where phname in
( select phname from pharmacy where pharma_company.addr=pharmacy.addr) group by
name
having count(phname)>=2);
NAME ADDR PHONE
-------------------- -------------------- ----------
drreddy bangalore 977654689
4)select * from drug where trname in (select tname from pcomp_drug where
name='cipla');
Database Laboratory Manual
Dept. of MCA, RNSIT
Bangalore-98
Eduoncloud.com Page 31
TRNAME FORMULA
-------------------- ---------
anacin xyz
saridon abc
5) select * from drug where trname in (select distinct tname from pcomp_drug);
TRNAME FORMULA
-------------------- --------
anacin xyz
crocin www
dolopar ggg
saridon abc
Exercise; 5
Data requirements of movie industry are captured. Each movie is identified by title and
year of release. Each movie has length in minutes and classified under one genres (like action,
horror etc.). Each movie has a plot outline. Production companies are identified by name and
each has an address. A production company produces one or more movies. Actors are identified
by id. Other details like name and date of birth of actors are also stored. Each actor acts in one
or more movies. Each actor has a role in movie. Directors are identified by id. Other details like
name and date of birth of directors are also stored. Each director directs one or more movies.
Each movie has one or more actors and one or more directors and is produced by a production
company.
Queries:
a) List the details of horror movies released in 2012 and directed by more than 2 directors.
b) List the details of actors who acted in movies having same titles but released before 2000
and after 2010.
c) List the details of production companies producing maximum movies.
d) List the details of movies where director and actor have same date of birth.
e) Retrieve the names of directors directed all the movies produced by any one production company.
Database Laboratory Manual
Dept. of MCA, RNSIT
Bangalore-98
Eduoncloud.com Page 32
ER and Relationa schema
Database Laboratory Manual
Dept. of MCA, RNSIT
Bangalore-98
Eduoncloud.com Page 33
CREATION OF TABLES :
create table production
(pc_name varchar2(15) primary key,
pc_address varchar2(15));
create table movie
(m_title varchar2(15) ,
m_length number(3),
Database Laboratory Manual
Dept. of MCA, RNSIT
Bangalore-98
Eduoncloud.com Page 34
m_yor number(4),
m_genres varchar2(10),
outline varchar2(15),
m_pcname references production ,
primary key(m_title,m_yor));
create table actors
(a_id number(5) primary key,
a_name varchar2(15),
a_dob date);
create table directors
(d_id number(5) number(5) primary key,
d_name varchar2(15),
d_dob date);
create table moviedirectors
(m_title varchar(15) ,
m_yor number(4) ,
foreign key (m_title,m_yor) references movie,
d_id number(5) references directors,
primary key(m_title,m_yor,md_id));
create table movieactors
(m_title varchar(15),
m_yor number(4),
ma_id number(5) references actors,
role varchar2(15),
foreign key(m_title, m_yor) references movie,
primary key(m_title,m_yor,ma_id));
desc movie;
Name Null? Type
----------------------------------------- -------- ---------------------------
M_TITLE NOT NULL VARCHAR2(15)
M_LENGTH NUMBER(3)
M_YOR NOT NULL NUMBER(4)
M_GENRES VARCHAR2(10)
OUTLINE VARCHAR2(15)
M_PCNAME VARCHAR2(15)
desc actors;
Name Null? Type
----------------------------------------- -------- ---------------------------
A_ID NOT NULL NUMBER(5)
A_NAME VARCHAR2(15)
A_DOB DATE
Database Laboratory Manual
Dept. of MCA, RNSIT
Bangalore-98
Eduoncloud.com Page 35
desc directors;
Name Null? Type
----------------------------------------- -------- ---------------------------
D_ID NOT NULL NUMBER(5)
D_NAME VARCHAR2(15)
D_DOB DATE
desc production;
Name Null? Type
----------------------------------------- -------- ---------------------------
PC_NAME NOT NULL VARCHAR2(15)
PC_ADDRESS VARCHAR2(15)
desc moviedirectors;
Name Null? Type
----------------------------------------- -------- ---------------------------
M_TITLE NOT NULL VARCHAR2(15)
M_YOR NOT NULL NUMBER(4)
MD_ID NOT NULL NUMBER(5)
desc movieactors;
Name Null? Type
----------------------------------------- -------- ---------------------------
M_TITLE NOT NULL VARCHAR2(15)
M_YOR NOT NULL NUMBER(4)
A_ID NOT NULL NUMBER(5)
ROLE VARCHAR2(15)
select * from production;
PC_NAME PC_ADDRESS
--------------- ---------------
avm chennai
yashraj bangalore
sun bangalore
rockline bangalore
ram mumbai
select * from movie;
M_TITLE M_LENGTH M_YOR M_GENRES OUTLINE M_PCNAME
--------------- ---------- ---------- ---------- --------------- --------------
-
ddlj 2 1996 romantic love avm
srikrishna 3 2010 myth devotion sun
krish 2 2014 animated cartoon yashraj
chennaexp 2 2013 drama comedy rockline
robo 3 2012 comedy comedy ram
ddlj 2 2012 romantic love avm
sur 2 2012 romantic love avm
select * from actors;
Database Laboratory Manual
Dept. of MCA, RNSIT
Bangalore-98
Eduoncloud.com Page 36
A_ID A_NAME A_DOB
---------- --------------- ---------
111 amitabh 10-MAR-55
222 shahrukh 15-MAR-77
333 hrithik 15-JAN-99
444 akshay 13-APR-75
555 amir 15-JAN-76
select * from directors;
D_ID D_NAME D_DOB
---------- --------------- ---------
1 abc 12-MAR-77
2 xyz 22-MAR-88
3 mno 15-JAN-99
4 pqr 15-JUL-89
5 jkl 25-FEB-85
select * from moviedirectors;
M_TITLE M_YOR MD_ID
--------------- ---------- ----------
ddlj 1996 1
srikrishna 2010 2
krish 2014 3
chennaexp 2013 4
robo 2012 5
select * from movieactors
M_TITLE M_YOR A_ID ROLE
--------------- ---------- ---------- ---------------
ddlj 1996 111 hero
krish 2014 333 hero
chennaexp 2013 222 hero
robo 2012 444 hero
srikrishna 2010 555 hero
ddlj 2012 111 hero
query1:
select * from movie where m_yor='2012'and m_genres='horror' and m_title in
(select m_title from moviedirectors group by m_title having count(*)>=2);
M_TITLE M_LENGTH M_YOR M_GENRES OUTLINE M_PCNAME
--------------- ---------- ---------- ---------- --------------- ------------
robo 3 2012 horror comedy ram
Database Laboratory Manual
Dept. of MCA, RNSIT
Bangalore-98
Eduoncloud.com Page 37
query2:
select * from actors where A_ID in (select m.A_ID from movieactors m,
movieactors n where m.m_title=n.m_title and m.m_yor<2000 and n.m_yor < 2010);
A_ID A_NAME A_DOB
----- --------------- ---------
111 amitabh 10-MAR-55
query3:
select * from production where PC_NAME in
(select M_PCNAME from movie group by m_pcname having count(m_pcname)
>=(select max(count(m_pcname)) from movie group by m_pcname));
PC_NAME PC_ADDRESS
--------------- --------------
avm Chennai
query4:
select * from movie where m_title in
(select m_title from moviedirectors where m_title in
(select m_title from movieactors where a_id in
(select a_id from actors a, directors d where a.a_dob=d.d_dob)));
M_TITLE M_LENGTH M_YOR M_GENRES OUTLINE M_PCNAME
--------------- ---------- ---------- ---------- --------------- ---------
krish 2 2014 animated cartoon yashraj
query5:
select d_name from directors where d_id in
(select md_id from moviedirectors where (m_title,m_yor) in
(select m_title,m_yor from movie where M_PCNAME='avm'));
D_NAME
----------
abc