0% found this document useful (0 votes)
4 views

DBA_LAB-1

The document outlines the schema and SQL queries for a Library Database, including tables for books, authors, publishers, and lending records. It provides specific queries to retrieve book details, borrower information, delete a book, partition the book table, and create a view of available books. Additionally, it includes the SQL commands for creating and populating the database tables.
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)
4 views

DBA_LAB-1

The document outlines the schema and SQL queries for a Library Database, including tables for books, authors, publishers, and lending records. It provides specific queries to retrieve book details, borrower information, delete a book, partition the book table, and create a view of available books. Additionally, it includes the SQL commands for creating and populating the database tables.
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/ 5

PROGRAM - 1

Consider the following schema for a Library Database:


BOOK (Book_ID, Title, Pub_Name, Pub_Year)
BOOK_AUTHORS (Book_ID, Author_Name)
PUBLISHER (Pub_ID, Pub_Name, Address, Phone)
BOOK_COPIES (Book_ID, PGM_ID, No_of_Copies)
BOOK_LENDING (Book_ID, PGM_ID, Card_No, Date_Out, Due_Date)
LIBRARY_PROGRAM (PGM_ID, PGM_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 program, 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 the 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.
Solution Queries:
Query to create tables:

 Publisher Table –
CREATE TABLE PUBLISHER(
PUB_ID INT PRIMARY KEY,
PUB_NAME VARCHAR(20) UNIQUE,
ADDRESS VARCHAR(20),
PHONE INT);

 Book Table –
CREATE TABLE BOOK(
BOOK_ID INT PRIMARY KEY,
TITLE VARCHAR(20),
PUB_NAME VARCHAR(20),
FOREIGN KEY (PUB_NAME) REFERENCES PUBLISHER (PUB_NAME) ON DELETE
CASCADE,
PUB_YEAR INT);

 Library Program Table –


CREATE TABLE LIBRARY_PROGRAM(
PGM_ID INT PRIMARY KEY,
PGM_NAME VARCHAR(20),
ADDRESS VARCHAR(30));
 Book Authors Table –
CREATE TABLE BOOK_AUTHORS(
BOOK_ID INT,
AUTHOR_NAME VARCHAR(20),
PRIMARY KEY(BOOK_ID, AUTHOR_NAME),
FOREIGN KEY(BOOK_ID) REFERENCES BOOK(BOOK_ID) ON DELETE CASCADE);

 Book Copies Table –


CREATE TABLE BOOK_COPIES(
BOOK_ID INT,
PGM_ID INT,
NO_OF_COPIES INT,
PRIMARY KEY(BOOK_ID, PGM_ID),
FOREIGN KEY (BOOK_ID) REFERENCES BOOK(BOOK_ID) ON DELETE CASCADE,
FOREIGN KEY (PGM_ID) REFERENCES LIBRARY_PROGRAM(PGM_ID) ON DELETE
CASCADE);

 Book Lending Table –


CREATE TABLE BOOK_LENDING(
BOOK_ID INT,
PGM_ID INT,
CARD_NO INT,
DATE_OUT DATE,
DUE_DATE DATE,
PRIMARY KEY(BOOK_ID,PGM_ID,CARD_NO),
FOREIGN KEY (BOOK_ID) REFERENCES BOOK(BOOK_ID) ON DELETE CASCADE,
FOREIGN KEY (PGM_ID) REFERENCES LIBRARY_PROGRAM(PGM_ID) ON DELETE
CASCADE);

Query to insert values into the table:

 Publisher table –
INSERT INTO PUBLISHER VALUES (501,'KVS','BANGALORE',9535616745);
INSERT INTO PUBLISHER VALUES (502,'WESTLAND','PUNE',8768916745);
INSERT INTO PUBLISHER VALUES (503,'RUPA','BANGALORE',6478989715);
INSERT INTO PUBLISHER VALUES (504,'GANGA','MUMBAI',9876985645);
INSERT INTO PUBLISHER VALUES (505,'HACHETTE','MATTUR',7013458745);

PUB_ID PUB_NAME ADDRESS PHONE


501 KVS BANGALORE 9535616745
502 WESTLAND PUNE 8768916745
503 RUPA BANGALORE 6478989715
504 GANGA MUMBAI 9876985645
505 HACHETTE MATTUR 7013458745
 Book Table –
INSERT INTO BOOK VALUES (001,'MCGRAW-HILL','GANGA',2001);
INSERT INTO BOOK VALUES (002,'MY ARTEMIS','KVS',2004);
INSERT INTO BOOK VALUES (003,'CHEMISTRY VOL 1','WESTLAND',2006);
INSERT INTO BOOK VALUES (004,'UPRISING','RUPA',2018);
INSERT INTO BOOK VALUES (005,'CHEMISTRY VOL 2','WESTLAND',2021);

BOOK_ID TITLE PUBLISHER_NAME PUB_YEAR


1 MCGRAW-HILL GANGA 2001
2 MY ARTEMIS KVS 2004
3 CHEMISTRY VOL 1 WESTLAND 2006
4 UPRISING RUPA 2018
5 CHEMISTRY VOL 2 WESTLAND 2021

 Library Program Table –


INSERT INTO LIBRARY_PROGRAM VALUES (101,'BOOK AXIS','BANGALORE');
INSERT INTO LIBRARY_PROGRAM VALUES (102,'BOOK SQUARE','PUNE');
INSERT INTO LIBRARY_PROGRAM VALUES (103,'CLAUS BOOKS','MUMBAI');
INSERT INTO LIBRARY_PROGRAM VALUES (104,'COMIC CON','PUNE');
INSERT INTO LIBRARY_PROGRAM VALUES (105,'FANDOM','BANGALORE');

PGM_ID PGM_NAME ADDRESS PGM_ID


101 BOOK AXIS BANGALORE 101
102 BOOK SQUARE PUNE 102
103 CLAUS BOOKS MUMBAI 103
104 COMIC CON PUNE 104
105 FANDOM BANGALORE 105

 Book Authors Table –


INSERT INTO BOOK_AUTHORS VALUES (001, 'ASHISH C');
INSERT INTO BOOK_AUTHORS VALUES (002, 'ANEESHA');
INSERT INTO BOOK_AUTHORS VALUES (003, 'ADITYA KUL C');
INSERT INTO BOOK_AUTHORS VALUES (004, 'SAQUIB M');
INSERT INTO BOOK_AUTHORS VALUES (005, 'ARJUN S');

BOOK_ID AUTHOR_NAME
1 ASHISH C
2 ANEESHA
3 ADITYA KUL C
4 SAQUIB M
5 ARJUN S
 Book Copies Table –
INSERT INTO BOOK_COPIES VALUES (001, 102, 40);
INSERT INTO BOOK_COPIES VALUES (002, 101, 18);
INSERT INTO BOOK_COPIES VALUES (003, 104, 53);
INSERT INTO BOOK_COPIES VALUES (004, 103, 4);
INSERT INTO BOOK_COPIES VALUES (005, 105, 20);

BOOK_ID PGM_ID NO_OF_COPIES


1 102 40
2 101 18
3 104 53
4 103 4
5 105 20

 Book Lending Table –


INSERT INTO BOOK_LENDING VALUES (001, 101, 5001, '21-SEP-2021', '19-OCT-2021');
INSERT INTO BOOK_LENDING VALUES (001, 102, 5002, '07-JAN-2017', '18-MAY-2017');
INSERT INTO BOOK_LENDING VALUES (002, 102, 5003, '02-FEB-2017', '22-MAR-2020');
INSERT INTO BOOK_LENDING VALUES (003, 103, 5004, '14-SEP-2016', '08-OCT-2021');
INSERT INTO BOOK_LENDING VALUES (005, 104, 5005, '18-JUN-2020', '14-AUG-2021');
INSERT INTO BOOK_LENDING VALUES (002, 102, 5002, '07-JAN-2017', '18-MAY-2017');
INSERT INTO BOOK_LENDING VALUES (003, 102, 5002, '02-FEB-2017', '22-MAR-2020');
INSERT INTO BOOK_LENDING VALUES (004, 102, 5002, '14-MAR-2017', '08-MAY-2019');

BOOK_ID PGM_ID CARD_NO DATE_OUT DUE_DATE


1 101 5001 21-SEP-21 19-OCT-21
1 102 5002 07-JAN-17 18-MAY-17
2 102 5003 02-FEB-17 22-MAR-20
3 103 5004 14-SEP-16 08-OCT-21
5 104 5005 18-JUN-20 14-AUG-21
2 102 5002 07-JAN-17 18-MAY-17
3 102 5002 02-FEB-17 22-MAR-20
4 102 5002 14-MAR-17 08-MAY-19

Query for given questions:


1) SELECT LP.PGM_NAME, B.BOOK_ID,TITLE, PUB_NAME, AUTHOR_NAME,
NO_OF_COPIES
FROM BOOK B, BOOK_AUTHORS BA, BOOK_COPIES BC, LIBRARY_PROGRAM LP,
PUBLISHER P
WHERE B.BOOK_ID = BA.BOOK_ID AND
BA.BOOK_ID = BC.BOOK_ID AND
BC.PGM_ID = LP.PGM_ID
GROUP BY LP.PGM_NAME, B.BOOK_ID, TITLE,
PUB_NAME, AUTHOR_NAME, NO_OF_COPIES;
PGM_NAME BOO K_I D TITLE PUB_ NAME AUTHOR_ NAME NO_OF_ COPI ES
CLAUS
4 UPRISING KVS SAQUIB M 4
BOOKS
CHEMISTRY
COMIC CON 3 GANGA ADITYA KUL C 53
VOL 1
CHEMISTRY
FANDOM 5 KVS ARJUN S 20
VOL 2
CHEMISTRY
FANDOM 5 GANGA ARJUN S 20
VOL 2
BOOK AXIS 2 MY ARTEMIS RUPA ANEESHA 18

2) SELECT CARD_NO FROM BOOK_LENDING


WHERE DATE_OUT > '01-JAN-2017' AND
DATE_OUT < '01-JUN-2017'
GROUP BY CARD_NO
HAVING COUNT (*) > 3;
CARD_ NO
5002

3) DELETE FROM BOOK WHERE BOOK_ID = 001;

BOOK_I D TITLE PUBLI SHER_NAME PUB_Y EAR


2 MY ARTEMIS KVS 2004
3 CHEMISTRY VOL 1 WESTLAND 2006
4 UPRISING RUPA 2018
5 CHEMISTRY VOL 2 WESTLAND 2021

4) SELECT PUB_YEAR FROM BOOK;

PUB_Y EA R
2004
2006
2018
2021

5) CREATE VIEW BOOKS_AVAILABLE AS


SELECT B.BOOK_ID, B.TITLE, C.NO_OF_COPIES
FROM LIBRARY_PROGRAM L, BOOK B, BOOK_COPIES C
WHERE B.BOOK_ID = C.BOOK_ID AND
L.PGM_ID=C.PGM_ID;
SELECT * FROM BOOKS_AVAILABLE

BOOK_ID T ITLE NO_OF_COPI ES


2 MY ARTEMIS 18
3 CHEMISTRY VOL 1 53
4 UPRISING 4
5 CHEMISTRY VOL 2 20

You might also like