Library Database

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 20

LIBRARY DATABASE

Consider the following schema for 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)
create table publisher
(
name varchar(10),
address varchar(10),
phone int,
primary key(name)
);
create table book
(
book_id varchar(5),
title varchar(20),
publisher_name varchar(10),
publisher_year int,
primary key(book_id),
foreign key(publisher_name) references publisher(name)
on delete cascade
);
create table book_authors
(
book_id varchar(5),
author_name varchar(15),
primary key(book_id),
foreign key(book_id) references book(book_id)
on delete cascade
);
create table library_branch
(
branch_id varchar(5),
branch_name varchar(10),
address varchar(15),
primary key(branch_id),
);
create table book_copies
(
book_id varchar(5),
branch_id varchar(5),
no_of_copies int,
primary key(book_id,branch_id),
foreign key(book_id) references book(book_id) on delete
cascade,
foreign key(branch_id) references
library_branch(branch_id) on delete cascade
);
create table book_lending
(
book_id varchar(5),
branch_id varchar(5),
card_no varchar(5),
date_out date,
due_date date,
primary key(book_id,branch_id,card_no),
foreign key(book_id) references book(book_id) on delete
cascade,
foreign key(branch_id) references
library_branch(branch_id) on delete cascade;
);
• 1. Retrieve details of all books in the library –
id, title, name of publisher, authors, number of
copies in each branch, etc.

select b.book_id, b.title, b.publisher_name,


ba.author_name, bc.branch_id, bc.no_of_copies
from book b,book_authors ba, book_copies bc
where b.book_id = bc.book_id and b.book_id =
ba.book_id;
2. Get the particulars of borrowers who have
borrowed more than 3 books, but from Jan 2017
to Jun 2017.
select distinct card_no
from book_lending b
where (date_out between '01-jan-2017' and '30-
jun-2017')
group by card_no
having count(*)>3;
3. Delete a book in BOOK table. Update the
contents of other tables to reflect this data
manipulation operation.
• delete from book where book_id='112';

4.Partition the BOOK table based on year of


publication. Demonstrate its working with a
simple query.
CREATE TABLE book1(book_id varchar(5),
title varchar(10),
publisher_name varchar(10),
publisher_year int,
PRIMARY KEY(book_id,publisher_year))
PARTITION BY RANGE (publisher_year)
(PARTITION p1 VALUES LESS THAN (2002),
PARTITION p2 VALUES LESS THAN (2010),
PARTITION p3 VALUES LESS THAN (MAXVALUE));
SELECT * FROM BOOK1 PARTITION(P1);
SELECT * FROM BOOK1 PARTITION(P2);
SELECT * FROM BOOK1 PARTITION(P3);
5. Create a view of all books and its number of
copies that are currently available in the Library.

create view available as


(
select book_id,sum(no_of_copies) -
(select count(card_no) from book_lending
where b.book_id = book_id) as avail_copies
from book_copies b group by book_id
);

You might also like