0% found this document useful (0 votes)
45 views23 pages

RSR Manual 3

1. Retrieve details of all books in the library including book id, title, publisher name, authors, number of copies in each branch. 2. Get details of borrowers who have borrowed more than 3 books between January 2017 to June 2017. 3. Delete a specific book from the BOOK table and update other tables to reflect this deletion.

Uploaded by

jeevenmr02
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views23 pages

RSR Manual 3

1. Retrieve details of all books in the library including book id, title, publisher name, authors, number of copies in each branch. 2. Get details of borrowers who have borrowed more than 3 books between January 2017 to June 2017. 3. Delete a specific book from the BOOK table and update other tables to reflect this deletion.

Uploaded by

jeevenmr02
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

A.

Consider the following schema for a Library Database:


BOOK (Book_id, Title, Publisher_Name, Pub_Year)
BOOK_AUTHORS (Book_id, Author_Name) PUBLISHER (Name, Address, Phone)
BOOK_COPIES (Book_id, Branch_id, No-of_Copies)
BOOK_LENDING (Book_id, Branch_id, Card_No, Date_Out, Due_Date)
LIBRARY_BRANCH (Branch_id, Branch_Name, Address)

Write SQL queries to


1. Retrieve details of all books in the library – id, title, name of publisher, authors, number of copies in
each branch, etc.
2. Get the particulars of borrowers who have borrowed more than 3 books, but from Jan 2017 to Jun
2017
3. Delete a book in BOOK table. Update the contents of other tables to reflect this data manipulation
operation.
4. Partition the BOOK table based on year of publication. Demonstrate its working with a simple query.
5. Create a view of all books and its number of copies that are currently available in the Library.

Schema Diagram
Entity-Relationship Diagram

Table Creation
create table publisher( pname varchar(10) ,
address varchar(10),
phoneno int,
primary key(pname));

create table book( bookid int primary key,


tittle varchar(10),
pub_name varchar(10),
puyear int,
foreign key(pub_name) references publisher(pname)on delete cascade);

create table bookauthor( bid int,


authorname varchar(10),
foreign key(bid)references book(bookid)on delete cascade);

create table librarybranch( branchid varchar(10) primary key,


branchname varchar(10),
address varchar(10));

create table bookcopies( boid int,bchid varchar(10), noofcopy int,


primary key(boid,bchid),
foreign key(boid) references book(bookid)on delete cascade,
foreign key(bchid) references librarybranch(branchid)on delete cascade);

create table booklend( booid int,


branid varchar(10), cardno int,
dateout date, outdate date,
primary key(booid,branid,cardno),
foreign key(branid)references librarybranch(branchid)on delete cascade,
foreign key(booid)references book(bookid)on delete cascade);

Insertion of values into the tables

insert into publisher values ('Mcgraw','bangalore',9900100);


insert into publisher values ('Pearson','new delhi',9900101);
insert into publisher values ('Random', 'hyderabad',9900102);
insert into publisher values ('Hachett', 'chenai',9900103);
insert into publisher values ('Planeta', 'bangalore',9900104);

insert into book values (1,'dbms' , 'Mcgraw',2017);


insert into book values (2,'adbms', 'Mcgraw',2016);
insert into book values (3,'cn', 'Pearson',2016);
insert into book values (4,'cg', 'Planeta',2015);
insert into book values (5,'os', 'Pearson',2016);

insert into bookauthor values (1,'Navathe');


insert into bookauthor values (2,'Navathe');
insert into bookauthor values (3,'Tenenbaum');
insert into bookauthor values (4,'Krishnan');
insert into bookauthor values (5,'Galvin');
select * from bookauthor;

insert into librarybranch values (1000,'Jayanagar','Bangalore');


insert into librarybranch values (1001,'Whitefield','bangalore');
insert into librarybranch values (1002,'Hebbala','Mysore');
insert into librarybranch values (1003,'Vijayapura','Ckikkamaglur');
insert into librarybranch values (1004,'Manipal','Udupi');

select * from librarybranch;

insert into bookcopies values(1,1000,10);


insert into bookcopies values(1, 1001,15);
insert into bookcopies values(2,1002,50);
insert into bookcopies values(2, 1003,25);
insert into bookcopies values( 3, 1004,17);
insert into bookcopies values(5, 1000,10);
insert into bookcopies values( 4, 1001,30);

select * from bookcopies;


insert into booklend values (1, 1000, 501, '01-jan-17','01-jun-17');
insert into booklend values (3, 1004, 501,'11-jan-17','11-mar-17');
insert into booklend values (2, 1003, 501, '21-feb-17','21-apr-17');
insert into booklend values (4, 1001, 501, '15-mar-17','15-jul-17');
insert into booklend values (1, 1001, 504, '12-apr-17','12-may-17');

1. Retrieve details of all books in the library – id, title, name of publisher, authors, number of copies in
each branch, etc.

select b.bookid,b.tittle,b.pub_name, a.authorname,c.noofcopy, l.branchid


from book b, bookauthor a, bookcopies c, librarybranch l
where b.bookid=a.bid and b.bookid=c.boid and l.branchid=c.bchid;

2. Get the particulars of borrowers who have borrowed more than 3 books, but from Jan 2017 to Jun
2017.

select cardno
from booklend
where dateout between '01-jan-2017' and '01-jul-2017'
group by cardno
having count(*)>3;
4. Partition the BOOK table based on year of publication. Demonstrate its working with a simple
query.

create view v1 AS
select puyear
from book
order by puyear;

5. Create a view of all books and its number of copies that are currently available in the Library.

create view v2 AS
select b.bookid, b.tittle,c.noofcopy
from book b, bookcopies c, librarybranch l
where b.bookid=c.boid and c.bchid=l.branchid;
3. Delete a book in BOOK table. Update the contents of other tables to reflect this data manipulation
operation (Execute this query at last)

delete from book


where bookid=3;
B. Consider the following schema for Order Database:

SALESMAN (Salesman_id, Name, City, Commission)


CUSTOMER (Customer_id, Cust_Name, City, Grade, Salesman_id)
ORDERS (Ord_No, Purchase_Amt, Ord_Date, Customer_id, Salesman_id)

Write SQL queries to


1. Count the customers with grades above Bangalore’s average.
2. Find the name and numbers of all salesmen who had more than one customer.
3. List all salesmen and indicate those who have and don’t have customers in their cities (Use UNION
operation.)
4. Create a view that finds the salesman who has the customer with the highest order of a day.
5. Demonstrate the DELETE operation by removing salesman with id 1000. All his orders must also be
deleted.

Schema Diagram

Entity-Relationship Diagram
Creation of Tables

create table salesman(


salesman_id varchar(5),
name varchar(10),
city varchar(10),
commission varchar(5),
primary key(salesman_id));

create table customer(


customer_id varchar(5),
cust_name varchar(10),
city varchar(10),
grade int,
primary key(customer_id),
salesman_id references salesman(salesman_id) on delete SET NULL);

create table orders(


ord_no varchar(5),
purchase_amt int,
ord_date date,
primary key(ord_no),
customer_id references customer(customer_id) on delete cascade,
salesman_id references salesman(salesman_id) on delete cascade);
insert into salesman values('s1000','John','Banglore','25%');
insert into salesman values('s1001','Ravi','Banglore','20%');
insert into salesman values('s1002','Kumar','Mysore','15%');
insert into salesman values('s1003','Smith','Delhi','30%');
insert into salesman values('s1004','Harsha','Hyderabad','15%');

insert into customer values('cust1','Preethi','Banglore',100,'s1000');


insert into customer values('cust2','Vivek','Manglore',300,'s1000');
insert into customer values('cust3','Bhaskar','Chennai',400,'s1001');
insert into customer values('cust4','Chethan','Banglore',200,'s1001');
insert into customer values('cust5','Mamatha','Banglore',400,'s1003');

insert into orders values('ord01',5000,'04-may-17','cust1','s1000');


insert into orders values('ord02',450,'20-jan-17','cust1','s1000');
insert into orders values('ord03',1000,'24-feb-17','cust3','s1001');
insert into orders values('ord04',3500,'13-apr-17','cust4','s1002');
insert into orders values('ord05',550,'09-mar-17','cust2','s1001');
insert into orders values('ord06',4000,'09-mar-17','cust2','s1001');
1. Count the customers with grades above Bangalore’s average.
4. Create a view that finds the salesman who has the customer with the highest order of a day.
5. Demonstrate the DELETE operation by removing salesman with id 1000. All his orders must also be
deleted.

select grade,count(customer_id)
from customer
group by grade
having grade>(select avg(grade)
from customer
where city='Banglore');

2. Find the name and numbers of all salesmen who had more than one customer.

select salesman_id,name
from salesman
where salesman_id IN
(select salesman_id
from customer
group by salesman_id
having count(*)>1);
3. List all salesmen and indicate those who have and don’t have customers in their cities (Use UNION
operation.)

select s.salesman_id,s.name,c.cust_name,c.city
from salesman s,customer c
where s.city=c.city
UNION
select s.salesman_id,s.name,'no match',c.city
from salesman s,customer c
where s.city<>c.city and s.salesman_id=c.salesman_id;

4. Create a view that finds the salesman who has the customer with the highest
order of a day.
create view highestorder AS
select s.salesman_id, s.name, o1.ord_no from salesman s, orders o1
where s.salesman_id=o1.salesman_id
and o1.purchase_amt =(
select max( purchase_amt)
from orders o2
where o2.ord_date=o1.ord_date);
5. Demonstrate the DELETE operation by removing salesman with id 1000. All his orders must also be
deleted.

delete from salesman


where salesman_id='s1000';
C .Consider the schema for Movie Database:
ACTOR (Act_id, Act_Name, Act_Gender)
DIRECTOR (Dir_id, Dir_Name, Dir_Phone)
MOVIES (Mov_id, Mov_Title, Mov_Year, Mov_Lang, Dir_id)
MOVIE_CAST (Act_id, Mov_id, Role)
RATING (Mov_id, Rev_Stars)
Write SQL queries to

1. List the titles of all movies directed by ‘Hitchcock’.

2. Find the movie names where one or more actors acted in two or more movies.

3. List all actors who acted in a movie before 2000 and also in a movie after 2015 (use JOIN
operation).
4. Find the title of movies and number of stars for each movie that has at least one rating and
find the highest number of stars that movie received. Sort the result by movie title.
5. Update rating of all movies directed by ‘Steven Spielberg’ to 5.

Schema Diagram

E-R Diagram
Table Creation

create table actor (


act_id varchar(6),
act_name varchar (10),
act_gender char (6),
primary key (act_id));

create table director (


dir_id varchar (6),
dir_name varchar (20),
dir_phone number (10),
primary key (dir_id));

create table movies (


mov_id varchar(6),
mov_title varchar(20),
mov_year number (4),
mov_lang varchar (10),
dir_id varchar (6),
primary key (mov_id),
foreign key (dir_id) references director (dir_id));

create table movie_cast (


act_id varchar(6),
mov_id varchar(6),
role varchar(10),
primary key (act_id, mov_id),
foreign key (act_id) references actor (act_id),
foreign key (mov_id) references movies (mov_id));
create table rating (
mov_id varchar (6),
rev_star varchar (8),
primary key (mov_id),
foreign key (mov_id) references movies (mov_id));
insert into actor values('act1','Shrinidhi','Female');
insert into actor values ('act2','Yash','Male');
insert into actor values ('act3','Punith','Male');
insert into actor values ('act4','Sudeep','Male');
insert into director values ('dir1','P Neel',990011);
insert into director values ('dir2','Hitchcock', 990012);
insert into director values ('dir3','C Nolan', 990013);
insert into director values('dir4','Steven Spielberg', 990014);

insert into movies values ('mov1','KGF-1', 2017, 'Kannada','dir1');


insert into movies values ('mov2','KGF-2', 2022, 'Kannada', 'dir1');
insert into movies values ('mov3','The Ring', 1927, 'English', 'dir2' );
insert into movies values ('mov4','War Horse', 2011, 'English', 'dir4');
insert into movie_cast values ('act1','mov1', 'Lead F');
insert into movie_cast values ('act1', 'mov2', 'Lead F');
insert into movie_cast values ('act3', 'mov3', 'Lead M');
insert into movie_cast values ('act3', 'mov1', 'Guest');
insert into movie_cast values ('act4', 'mov4', 'Lead M');

insert into rating values ('mov1', 4);


insert into rating values ('mov2', 2);
insert into rating values ('mov3', 5);
insert into rating values ('mov4', 4);

1. List the titles of all movies directed by ‘Hitchcock’.

select mov_title
from movies
where dir_id IN (
select dir_id from director
where dir_name = 'Hitchcock' );
2. Find the movie names where one or more actors acted in two or more movies.

select mov_title
from movies m, movie_cast mv
where m.mov_id=mv.mov_id and act_id IN (
select act_id
from movie_cast
group by act_id
having count(act_id)>1)
group by mov_title having count (*)>1;
3. List all actors who acted in a movie before 2000 and also in a movie after 2015 (use JOIN operation).

select act_name, mov_title, mov_year


from actor a
JOIN movie_cast c ON a.act_id=c.act_id
JOIN movies m ON c.mov_id=m.mov_id
where m.mov_year NOT BETWEEN 2000 and 2015;

4. Find the title of movies and number of stars for each movie that has at least one rating and find the
highest number of stars that movie received. Sort the result by movie title.

select m.mov_title,r.rev_stars
from movies m,rating r
where m.mov_id=r.mov_id and r.rev_stars>0
order by m.mov_title;
5. Update rating of all movies directed by ‘Steven Spielberg’ to 5

update rating set rev_stars=5


where mov_id IN
(
select mov_id
from movies where dir_id IN
(
select dir_id from director
where dir_name = 'Steven Spielberg'));

You might also like