Library DB
Library DB
Entity-Relationship Diagram
Author_Name
Book_id Title
Pub_Year M N
Has
Published-by
N No_of_copies
Branch_id
Publisher_Name
M M N
1 Book_Copies In Library_Branch
Branch_Name
Address
Publisher
N Address
Date_out
Book_Lending
Phone
Card_No
Due_date
N
Card
Book
Book_Authors
Book_i Author_nam
d e
Publisher
Book_Copies
Book_Lending
Library_Branch
Table Creation
---------------------------------------------------------------------------------------------------------
CREATE TABLE BOOK
(
BOOK_ID INT,
TITLE VARCHAR (20),
PUB_YEAR VARCHAR(20),
PRIMARY KEY(BOOK_ID),
PUBLISHER_NAME VARCHAR (20) REFERENCES PUBLISHER (NAME)
);
----------------------------------------------------------
-------------------------------------------------
----------------------------------------------------------------------------------------------------------------
CREATE TABLE LIBRARY_BRANCH
(
BRANCH_ID INT,
BRANCH_NAME VARCHAR (20),
ADDRESS VARCHAR (30),
PRIMARY KEY(BRANCH_ID)
);
-------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------
CREATE TABLE BOOK_LENDING
(
DATE_OUT DATE,
DUE_DATE DATE,
BOOK_ID INT REFERENCES BOOK (BOOK_ID) ON DELETE CASCADE,
BRANCH_ID INT REFERENCES LIBRARY_BRANCH (BRANCH_ID) ,
CARD_NO INT REFERENCES CARD (CARD_NO),
PRIMARY KEY (BOOK_ID, BRANCH_ID, CARD_NO)
);
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 Jun 2017.
SELECT CARD_NO
FROM BOOK_LENDING
WHERE DATE_OUT BETWEEN ’01-JAN-2017’ AND ’01-JUL-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.
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.