Program1
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, Programme_id, No-of_Copies)
BOOK_LENDING (Book_id, Programme_id, Card_No, Date_Out, Due_Date)
LIBRARY_PROGRAMME (Programme_id, Programme_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 Programme, 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.
Entity-Relationship Diagram:
v
Schema Diagram:
v
Table Creation:
create table PUBLISHER_dbms (Name varchar(15),
Address varchar(15),Phone varchar(10), Primary key
(Name));
create table BOOK_dbms (Book_id
varchar(4),Title
varchar(10),Publisher_name varchar(15),
Pub_year int, primary key (Book_id),
Foreign key(Publisher_name) references
PUBLISHER_dbms (Name) on deletecascade);
create table BOOK_AUTHORS_dbms(Book_id varchar(4),
Author_name varchar(10), primary key (Book_id),
Foreign key(Book_id) references BOOK_dbms (Book_id) on delete cascade);
create table LIBRARY_PROGRAMME_dbms(programme_id varchar(4),
programme_name varchar(15), Address varchar(15), primary key
(programme_id));
create table BOOK_LENDING_dbms(Book_id varchar(4),
programme_id varchar(4), Card_no int, Date_out
datetime,Due_date datetime,
primary key (Book_id, programme_id, Card_no),
Foreign key(Book_id) references BOOK_dbms (Book_id) on delete cascade,
Foreign key(programme_id) references LIBRARY_PROGRAMME_dbms (programme_id) on
delete cascade);
create table BOOK_COPIES_dbms(Book_id varchar(4),
programme_id varchar(4),
No_of_copies int,
primary key (Book_id, programme_id),
Foreign key(Book_id) references BOOK_dbms (Book_id) on delete cascade,
Foreign key(programme_id) references LIBRARY_PROGRAMME_dbms (programme_id) on
delete cascade);
v
To view created tables:
SELECT * FROM PUBLISHER_dbms;
SELECT * FROM BOOK_dbms;
SELECT * FROM BOOK_AUTHORS_dbms;
SELECT * FROM LIBRARY_PROGRAMME_dbms;
SELECT * FROM BOOK_LENDING_dbms;
SELECT * FROM BOOK_COPIES_dbms;
Inserting values:
insert into PUBLISHER_dbms values ('TMH', 'Mangalore','9876543897');
insert into PUBLISHER_dbms values ('Prism', 'chennai','8756444324');
insert into PUBLISHER_dbms values ('Himalaya', 'Kolkata','9876556785');
insert into PUBLISHER_dbms values ('Pearson','Delhi','9878987675');
insert into PUBLISHER_dbms values ('Elsevier','Bangalore','7659876785');
insert into BOOK_dbms values ('b1','DBMS', 'TMH',2015);
insert into BOOK_dbms values ('b2','DMS', 'Prism',2016);
insert into BOOK_dbms values ('b3','CN', 'Himalaya',2015);
insert into BOOK_dbms values ('b4','AI', 'Pearson',2013);
insert into BOOK_dbms values ('b5','OS', 'Elsevier',2017);
insert into BOOK_AUTHORS_dbms values ('b1','navathe');
insert into BOOK_AUTHORS_dbms values ('b2','dsc');
insert into BOOK_AUTHORS_dbms values ('b3','david');
insert into BOOK_AUTHORS_dbms values ('b4','stuart');
insert into BOOK_AUTHORS_dbms values ('b5','galvin');
insert into LIBRARY_PROGRAMME_dbms values ('bh1','Book Corner', 'Bangalore');
insert into LIBRARY_PROGRAMME_dbms values ('bh2','Book Point', 'Mangalore');
insert into LIBRARY_PROGRAMME_dbms values ('bh3','Book Cafe', 'Mumbai');
insert into BOOK_COPIES_dbms values ('b1','bh1',10);
insert into BOOK_COPIES_dbms values ('b1','bh2',15);
insert into BOOK_COPIES_dbms values ('b2','bh2',30);
insert into BOOK_COPIES_dbms values ('b2','bh3',28);
insert into BOOK_COPIES_dbms values ('b3','bh1',35);
insert into BOOK_COPIES_dbms values ('b3','bh2',22);
insert into BOOK_COPIES_dbms values ('b4','bh1',8);
i
insert into BOOK_COPIES_dbms values ('b5','bh3',17);
(Note: For Mysqluse Date Format: ’YYYY-MM-DD’. Eg: ’2017-01-02’.)
insert into BOOK_LENDING_dbms values ('b1','bh1',1,'02-jan-17','10-jan-17');
insert into BOOK_LENDING_dbms values ('b2','bh2',2,'11-jan-17','11-mar-17');
insert into BOOK_LENDING_dbms values ('b3','bh1',1,'21-feb-17','21-apr-17');
insert into BOOK_LENDING_dbms values ('b5','bh3',1,'15-mar-17','15-jul-17');
insert into BOOK_LENDING_dbms values ('b3','bh2',4,'12-apr-17','12-may-17');
insert into BOOK_LENDING_dbms values ('b4','bh1',1,'21-feb-17','21-apr-17');
Queries:
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 Jun2017.
3.Delete a book in BOOK table. Update the contents of other tables to reflect this
data manipulation operation.
(Note: records corresponds to Book_id =’b4’ is also deleted from BOOK_AUTHORS,
LIBRARY_BRANCH, BOOK_COPIES and BOOK_LENDING tables because of the use
of ‘on delete CASCADE’ constraint on foreign keys).
x
4. Partition the BOOK table based on year of publication. Demonstrate its working
with a simplequery.
5. Create a view of all books and its number of copies that are currently available
in the Library.