Lab Manual DBMS

67
Database Laboratory 13MCA27 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. TOCE DEPARTMENT OF MCA Page 1

description

it is use full for mca students under vtu university. it is the lab programs of dbms subject

Transcript of Lab Manual DBMS

Database Laboratory 13MCA27

Database Laboratory 13MCA27

Exercise-1Notown 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.

ER Diagram

Relational Schema

Create Table Query

1) SQL>create table musician(

M_id smallint primary key,

M_name varchar(15) not NULL,

telephone varchar(10),

Addr varchar(20) not NULL);Table Created.

2) SQL> create table instrument(

I_id smallint primary key,

I_name varchar(15) not NULL,

M_key varchar(10) not NULL);Table Created.

3) SQL> create table play(

M_id smallint not NULL references musician,

I_id smallint not NULL references instrument, primary key(M_id,I_id));

Table Created.4) SQL> create table album(

A_id smallint primary key,

A_title varchar(15) not NULL,

format varchar(5),

producer smallint not NULL references musician(M_id), copyright date);Table Created.

5) SQL> create table song(

A_id smallint not NULL references album,

S_no smallint not NULL,

stitle varchar(15) not NULL,

I_id smallint not NULL references instrument, primary key(A_id,S_no));Table Created.

6) SQL> create table perfomed(

M_id smallint not NULL references musician,

A_id smallint not NULL,

S_no smallint not NULL,

primary key(M_id,A_id,S_no),

foreign key(A_id,S_no) references song);Table Created.

Insert Into

1) SQL> insert into musician values(1,sowjanya,9916564999,vamanjoor);1 Row Created.

2) SQL> insert into instrument values(11,saxharn,b-flat);1 Row Created.

3) SQL> insert into play values(1,11);1 Row Created.

4) SQL> insert into album values(111,home again,cd,5,23-nov-04);1 Row Created.

5) SQL> insert into song values(111,2,hit me off,11);1 Row Created.

6) SQL> insert into performed values(1,111,1);1 Row Created.

Tables

MISICIAN Table

INSTRUMENT Table

PLAY Table

ALBUM Table

SONG Table

PERFOMED Table

Queries

a) List musician name, title of the song which he has played, the album in which song has occulted.

SQL> select m.M_name,s.stitle,a.A_title

from musician m,song s,album a,perfomed p1 where s.A_id=p1.A_id

and s.S_no=p1.S_no

and s.A_id=a.A_id

and m.M_id=p1.M_id

order by A_title;

b) List the details of songs which are performed by more than 3 musicians.

SQL> select A_id,S_no,stitle from song

where (A_id,S_no) in (select A_id,S_no

from perfomed group by S_no,A_id

having count(M_id)>3);

c) List the different instruments played by the musicians and the average number of musicians who play the instrument.

SQL> select distinct(i.I_name),count(p.M_id) as Total_Musician,

count(p.M_id)/(select count(*) from musician) as Average_Musician

from instrument i,play p

where i.I_id=p.I_id

group by I_name;

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.

SQL> select A_title from album where

producer in ( select distinct(M_id) from play

where I_id in ( select I_id from instrument

where I_name in ('guitar','fluat'))

and

A_id in ( select A_id from song group by A_id

having count(A_id)>(select avg(count(A_id)) from song

group by A_id))); e) List the details of musicians who can play all the instruments present.

SQL> select * from musician where

M_id in ( select M_id from play group by M_id

having count(I_id)=

(select count(I_id) from instrument));

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

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

Relational Schema

Create Table Query

1) SQL> create table professor(

p_id number primary key,

p_name varchar2(15),

p_age number(3),

rank number(3),

speciality varchar2(20));Table Created.

2) SQL> create table project(

proj_no number primary key,

s_name varchar2(15),

startdate date,

enddate date,

budget number(10));Table Created.

3) SQL> create table graduate(

usn number primary key,

stu_name varchar2(15),

stu_age number (3),

degree varchar2(10));Table Created.4) SQL> create table projleader(

lead_id number primary key,

proj_no number references project(proj_no),

4 p_id number references professor(p_id));Table Created.

5) SQL> create table work_prof(

p_id number references professor(p_id),

proj_no number references project(proj_no),

lead_id number references projleader(lead_id));Table Created.

6) SQL> create table work_stu(

usn number,

proj_no number,

lead_id number,

primary key(usn,proj_no,lead_id),

foreign key(usn)references graduate(usn) on delete cascade,

foreign key(proj_no)references project(proj_no) on delete cascade,

foreign key(lead_id)references projleader(lead_id) on delete cascade);Table Created.

Insert Into

1) SQL> insert into professor values(1,'Soniya',29,2,'UNIX');1 Row Created.

2) SQL> insert into graduate values(1301,'Jay',22,'MCA');1 Row Created.

3) SQL> insert into project values(101,'SQRTPL','24-oct-2006','20-nov-2010',100000);1 Row Created.

4) SQL> insert into projleader values(501,101,1);1 Row Created.

5) SQL> insert into work_prof values(1,101,501);1 Row Created.

6) SQL> insert into work_stu values(1301,101,501);1 Row Created.

TablesPROFESSOR Table GRADUATE Table

PROJECT Table

PROJLEADER Table

WORK_PROF Table

WORK_STU Table

Queries

a) Retrieve the names of all professors who do not have an ongoing project of more than 1

lakh.SQL> select distinct(prof.p_name) from

professor prof, project proj, work_prof wp

where prof.p_id=wp.p_id

and proj.proj_no=wp.proj_no

and proj.budget select prof.p_name, stu.stu_name, proj.s_name from

professor prof, project proj, work_stu ws, projleader pl,

graduate stu

where stu.usn=ws.usn

and proj.proj_no=ws.proj_no

and pl.lead_id=ws.lead_id

and prof.p_id=pl.p_id

order by prof.p_name;

c) List the professors and sum of the budget of their projects started after 2005 but ended in 2010.

SQL> select pf.p_name,sum(pj.budget) as Total_budget from work_prof wp, professor pf, project pj

where pf.p_id=wp.p_id and pj.proj_no=wp.proj_no and

EXTRACT(YEAR FROM startdate)>2005 and EXTRACT(YEAR FROM enddate)=2010

group by pf.p_name;

d) List the names of professors who has a total worth of project greater than the average budget of projects sanctioned.

SQL> select prof.p_name, proj.budget from

professor prof, project proj, work_prof wp where

proj.proj_no=wp.proj_no and

prof.p_id=wp.p_id and

proj.budget>(select avg(budget)from project);

e) List the professors who work on all the projects.

SQL> select p_name from professor where p_id in

(select p_id from work_prof group by p_id having count(p_id)=

(select count(proj_no) from project)); Exercise: 3A 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.ER Diagram

Relational Schema

Create Table Query

1) SQL> create table bank( bank_code varchar2(10) primary key, bank_name varchar2(20), bank_address varchar2(25), phone number(10));Table Created.

2) SQL> create table branch( branch_name varchar2(20) primary key, branch_address varchar2(25), phone number(10), bank_code varchar2(10) references bank(bank_code));Table Created.

3) SQL> create table customer( ssn number(9) primary key, cust_name varchar2(25), cust_address varchar2(25), cust_phone number(10), age number(2) check(age >18));Table Created.4) SQL> create table account( account_no number(10) primary key, account_type varchar2(10), account_access varchar(10), amount number(10,2), branch_name varchar2(20) references branch(branch_name));Table Created.

5) SQL> create table depositor( account_no number(10) references account(account_no), ssn number(9) references customer(ssn));Table Created.6) SQL> create table loan( loan_no number(10) primary key, duration number(10), interest_rate number(10), loan_amount number(10,2), branch_name varchar2(20) references branch(branch_name));Table Created.

7) SQL> create table borrower( ssn number(10) references customer(ssn), loan_no number(10) references loan(loan_no));Table Created.

Insert Into

1) SQL> insert into bank values ('SBI','StateBankOfIndia','CorporationCircle,Delhi',86342189);1 Row Created.

2) SQL> insert into branch values ('SBIJayanagar','JayaNagar,Bangalore',78945612,'SBI');1 Row Created.

3) SQL> insert into customer values(852147963,'Vasanth','Jayanagar',85223147,35);1 Row Created.

4) SQL> insert into account values(456456,'savings','single',50000,'SBIT.Nagar');1 Row Created.

5) SQL> insert into depositor values (456456,357869142);1 Row Created.

6) SQL> insert into loan values(123456,20,10,1000000,'SBIJayanagar');1 Row Created.

7) SQL> insert into borrower values(852147987,123459);1 Row Created.

Tables BANK Table BRANCH Table

CUSTOMER Table

ACCOUNT Table

DEPOSITOR Table

LOAN Table

BORROWER Table

Queries

a) List the details of customers who have joint account and also have at least one loan.SQL> select * from customer where ssn in (select ssn from depositor where account_no in (select account_no from account where account_access = 'joint') intersect select ssn from borrower); b) List the details of the branch which has given maximum loan.

SQL> select * from branch where branch_name in (select branch_name from (select branch_name, sum(loan_amount) l_amt from loan group by branch_name) where l_amt in (select max(sum(loan_amount)) from loan group by branch_name)); c) List the details of saving accounts opened in the SBI branches located at Bangalore.

SQL> select * from account where account_type = 'savings' and branch_name in (select branch_name from branch where branch_address like '%Bangalore%' and branch_name like 'SBI%');

d) List the name of branch along with its bank name and total amount of loan given by it.

SQL> select b.bank_name, br.branch_name, l.loan_amt from bank b, branch br ,(select branch_name, sum(loan_amount) loan_amt from loan group by branch_name) l where b.bank_code = br.bank_code and br.branch_name = l.branch_name;

e) Retrieve the names of customers who have accounts in all the branches located in a specific city.

SQL> select cust_name from customer where ssn in (select distinct ssn from depositor d where not exists ((select branch_name from branch where branch_address like '%Bangalore%') minus (select branch_name from account acc, depositor dep

where acc.account_no = dep.account_no and dep.ssn = d.ssn)));

Exercise: 4Patients 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.ER Diagram

Relational Schema

Create Table Query

1) SQL> Create table Doctor( d_ssn varchar2(5) primary key, d_name varchar2(15),

d_spec varchar2(20), d_expr number);Table Created.

2) SQL> Create table Patient( p_ssn varchar2(5) primary key, p_name varchar2(15),

p_addr varchar(15), p_age number, d_ssn varchar2(20) references doctor(d_ssn));Table Created.3) SQL> Create table prescription( presc_id number primary key, d_ssn varchar2(5) references doctor(d_ssn), p_snn varchar2(5) references patient(p_ssn),

drug_name varchar2(20),

presc_date date);

Table Created.

4) SQL> Create table Pharma_company( phar_id varchar2(5) primary key, phar_name varchar2(10), phar_city varchar2(10),

phar_phone number);

Table Created.

5) SQL> Create table Drug( drug_id varchar2(5) primary key,

drug_name varchar2(15), trade_name varchar2(10), formula varchar2(5), price number(5, 2),

phar_id varchar2(5) references pharma_company(phar_id), expiry_date date);

Table Created.

6) SQL> Create table Pharmacy( py_id varchar2(5) primary key, py_name varchar2(20), py_city varchar2(10),

py_phone number);Table Created.

7) SQL> Create table selling_drug( drug_id varchar2(5) references drug(drug_id), py_id varchar2(5) references pharmacy(py_id));Table Created.

8) SQL> Create table making_drug(

phar_id varchar2(5) references pharma_company(phar_id),

drug_id varchar2(5) references drug(drug_id),

py_id varchar2(5) references pharmacy(py_id));

Table Created.

9) SQL> Create table contract( phar_id varchar2(5) references pharma_company(phar_id), py_id varchar2(5) references pharmacy(py_id), start_date date, end_date date,

supervisor varchar2(10),

description varchar2(20));Table Created.

Insert Into

1) SQL> insert into doctor values('D001','Prashanth','Eye Specialist',5);1 Row Created.

2) SQL> insert into patient values('P001','Raja','Bangalore',20,'D001');1 Row Created.

3) SQL> insert into prescription values(1,'D001','P001','ionoiz-m','01-oct-14');1 Row Created.

4) SQL> insert into Pharma_company values('PH01','HULtd','Mumbai',8981500);1 Row Created.

5) SQL> Insert into drug values ('11', 'ionoiz-m','Steriods', 'd1+d2', 70, 'PH01', '15-aug-2011');1 Row Created.

6) SQL> Insert into pharmacy values('py01', 'Barani Chemist', 'Bangalore', 3652479);1 Row Created.

7) SQL> Insert into selling_drug values('11', 'py01');1 Row Created.

8) SQL> Insert into making_drug values('PH01','11','py01');1 Row Created.

9) SQL> Insert into contract values('PH01', 'py01', '23-apr-14', '20-aug-16', 'Sangeeta','Contract for 2 y');1 Row Created.

TablesDOCTOR Table

PATIENT Table

PRESCRIPTION Table

PHARMA_COMPANY Table

DRUG Table

PHARMACY Table

SELLING_DRUG Table

MAKING_DRUG Table

CONTRACT Table

Queries

a) List the details of patients who are 20 years old and have been checked by eye specialist.SQL> select p.p_name from patient p, doctor d where d.d_ssn=p.d_ssn and p.p_age=20 and d.d_spec='Eye Specialist';

b) List the details of doctors who have given the prescription to more than 20 patients in year 2013.SQL> select D_name from doctor where d_ssn in ( select d_ssn from prescription where EXTRACT(YEAR FROM presc_date)=2013 having count(presc_id)>2 group by d_ssn);

c) List the details of pharmaceutical companies who supply drug to more than 10 pharmacies in the same city where company is located.SQL> select phar.phar_name from pharma_company phar, pharmacy py, making_drug mk where phar.phar_id=mk.phar_id and py.py_id=mk.py_id and py.py_city=phar.phar_city having count(py.py_id) > 1 group by phar.phar_name;

d) List the details of drug supplied by only one pharmaceutical company.SQL> select * from drug where drug_id in (select drug_id from making_drug having count(phar_id)=1 group by drug_id);

e) List the details of drug supplied by all pharmaceutical companies.

SQL> select * from drug where drug_id in

(select drug_id from making_drug having count(phar_id)=

(select count(phar_id) from pharma_company) group by drug_id);

Exercise: 5Data 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. Others 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.ER Diagram

Relational Schema

Create Table Query

1) SQL> create table production_company( prdn_co_name varchar2(25), address varchar2(25), primary key(prdn_co_name));Table Created.

2) SQL> create table actor( Id varchar2(10) primary key, name varchar2(25), dob date);Table Created.

3) SQL> create table director( Id varchar2(10) primary key, name varchar2(25), dob date);Table Created.

4) SQL> create table movie( title varchar2(25), yr_of_release number(4), length number(5) , classification varchar(10), plot_out_line varchar2(25), prdn_co_name varchar2(25) references production_company(prdn_co_name), primary key(title,yr_of_release));

Table Created.

5) SQL> create table actor_movie_detail( title varchar2(25), yr_of_release number(4), roles varchar2(10), Id varchar2(10) references actor(Id));Table Created.

6) SQL> create table director_movie_detail( title varchar2(25), yr_of_release number(4), Id varchar2(10) references director(Id));Table Created.

Insert Into

1) SQL> insert into production_company values('New Line Cinema','LosAngles, US');1 Row Created.

2) SQL> insert into actor values('A001','JackieChan','07-APR-54');1 Row Created.

3) SQL> insert into director values('D001','Chris Columbus','07-APR-54');1 Row Created.

4) SQL> insert into movie values('RushHour','1998','120','Action','Police Story','New Line Cinema');1 Row Created.

5) SQL> insert into actor_movie_detail values ('RushHour','1998','MainHero','A001');1 Row Created.

6) SQL> insert into director_movie_detail values('RushHour','1998','D001');1 Row Created.

TablesPRODUCTION_COMPANY Table

ACTOR Table

DIRECTOR Table

MOVIE Table

ACTOR_MOVIE_DETAIL Table

DIRECTOR_MOVIE_DETAIL Table

Queries

a) List the details of horror movies released in 2012 and directed by more than 2 directors.SQL> select * from movie m where classification = 'Horror' and yr_of_release='2012' and (title , yr_of_release) in

(select title , yr_of_release from director_movie_detail group by title,yr_of_release having count(*) >2);

b) List the details of actors who acted in movies having same titles but released before 2000 and after 2010.

SQL> select a.* from actor a , (select title,id from actor_movie_detail where yr_of_release < 2000 intersect

select title,id from actor_movie_detail where yr_of_release > 2010) l where a.id =l.id;

c) List the details of production companies producing maximum movies.

SQL> select * from production_company where prdn_co_name in ( select prdn_co_name from (select prdn_co_name, count(prdn_co_name) cnt from movie group by prdn_co_name)

where cnt in (select max(count(prdn_co_name)) from movie group by prdn_co_name));

d) List the details of movies where director and actor have same date of birth.

SQL> select * from movie where (title,yr_of_release) in (

select title, yr_of_release from actor_movie_detail where id in

(select a.id from actor a, director d where a.dob = d.dob)

intersect

select title, yr_of_release from director_movie_detail where id in

(select d.id from actor a, director d where a.dob = d.dob));

e) Retrieve the names of directors directed all the movies produced by any one production company.

SQL> select name from director where id in (select distinct id from director_movie_detail dmd

where not exists( (select title from movie where PRDN_CO_NAME ='RedGiant')

minus

(select title from director_movie_detail where id = dmd.id)));

TOCE DEPARTMENT OF MCA Page 42