DBMS Lab

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

Run in command line

sqlplus / as sysdba

Exp no.2 :- Create Schema, insert at least 5 records in each table and add
appropriate constraints for the following Library Database using
ORACLE or MySQL DBMS under LINUX/Windows environment
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, Br_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 2020 to Jun 2022.
3. Delete a book in BOOK table. Update the contents of other tables to
reflect this data manipulation operation.

Create Tables and Constraints

CREATE TABLE book (


book_id NUMBER(10) PRIMARY KEY,
title VARCHAR(20),
publisher_name VARCHAR(20),
pub_year NUMBER(4)
);

CREATE TABLE book_authors (


book_id NUMBER(10),
author_name VARCHAR(20) PRIMARY KEY,
FOREIGN KEY (book_id) REFERENCES book(book_id)
);
CREATE TABLE publisher (
name VARCHAR(20) PRIMARY KEY,
address VARCHAR(20),
phone VARCHAR(20)
);

CREATE TABLE library_branch (


branch_id NUMBER(10) PRIMARY KEY,
branch_name VARCHAR(20),
address VARCHAR(20)
);

CREATE TABLE book_copies (


book_id NUMBER(10),
branch_id NUMBER(10),
no_of_copies NUMBER(10),
PRIMARY KEY (book_id, branch_id),
FOREIGN KEY (book_id) REFERENCES book(book_id),
FOREIGN KEY (branch_id) REFERENCES library_branch(branch_id)
);

CREATE TABLE book_lending (


book_id NUMBER(10),
branch_id NUMBER(10),
card_no VARCHAR(20),
date_out DATE,
due_date DATE,
FOREIGN KEY (book_id) REFERENCES book(book_id),
FOREIGN KEY (branch_id) REFERENCES library_branch(branch_id)
);
Insert Records

-- Inserting sample data into the book table


INSERT INTO book (book_id, title, publisher_name, pub_year) VALUES (1, 'Book One',
'Publisher A', '2020');
INSERT INTO book (book_id, title, publisher_name, pub_year) VALUES (2, 'Book Two',
'Publisher B', '2018');
INSERT INTO book (book_id, title, publisher_name, pub_year) VALUES (3, 'Book Three',
'Publisher C', '2019');
INSERT INTO book (book_id, title, publisher_name, pub_year) VALUES (4, 'Book Four',
'Publisher D', '2021');
INSERT INTO book (book_id, title, publisher_name, pub_year) VALUES (5, 'Book Five',
'Publisher E', '2017');

-- Inserting sample data into the book_authors table


INSERT INTO book_authors (book_id, author_name) VALUES (1, 'Author One');
INSERT INTO book_authors (book_id, author_name) VALUES (2, 'Author Two');
INSERT INTO book_authors (book_id, author_name) VALUES (3, 'Author Three');
INSERT INTO book_authors (book_id, author_name) VALUES (4, 'Author Four');
INSERT INTO book_authors (book_id, author_name) VALUES (5, 'Author Five');

-- Inserting sample data into the publisher table


INSERT INTO publisher (name, address, phone) VALUES ('Publisher A', 'Address One',
'8123456789');
INSERT INTO publisher (name, address, phone) VALUES ('Publisher B', 'Address Two',
'8123456789');
INSERT INTO publisher (name, address, phone) VALUES ('Publisher C', 'Address Three',
'8123456789');
INSERT INTO publisher (name, address, phone) VALUES ('Publisher D', 'Address Four',
'8123456789');
INSERT INTO publisher (name, address, phone) VALUES ('Publisher E', 'Address Five',
'8123456789');

-- Inserting sample data into the library_branch table


INSERT INTO library_branch (branch_id, branch_name, address) VALUES (1, 'Branch A',
'Address One');
INSERT INTO library_branch (branch_id, branch_name, address) VALUES (2, 'Branch B',
'Address Two');
INSERT INTO library_branch (branch_id, branch_name, address) VALUES (3, 'Branch C',
'Address Three');
INSERT INTO library_branch (branch_id, branch_name, address) VALUES (4, 'Branch D',
'Address Four');
INSERT INTO library_branch (branch_id, branch_name, address) VALUES (5, 'Branch E',
'Address Five');

-- Inserting sample data into the book_copies table


INSERT INTO book_copies (book_id, branch_id, no_of_copies) VALUES (1, 1, 10);
INSERT INTO book_copies (book_id, branch_id, no_of_copies) VALUES (2, 2, 8);
INSERT INTO book_copies (book_id, branch_id, no_of_copies) VALUES (3, 3, 12);
INSERT INTO book_copies (book_id, branch_id, no_of_copies) VALUES (4, 1, 6);
INSERT INTO book_copies (book_id, branch_id, no_of_copies) VALUES (5, 2, 9);

-- Inserting sample data into the book_lending table


INSERT INTO book_lending (book_id, branch_id, card_no, date_out, due_date) VALUES
(1, 1, 'Card003', TO_DATE('2021-01-05', 'YYYY-MM-DD'), TO_DATE('2021-01-15',
'YYYY-MM-DD'));
INSERT INTO book_lending (book_id, branch_id, card_no, date_out, due_date) VALUES
(2, 2, 'Card003', TO_DATE('2021-02-10', 'YYYY-MM-DD'), TO_DATE('2021-02-20',
'YYYY-MM-DD'));
INSERT INTO book_lending (book_id, branch_id, card_no, date_out, due_date) VALUES
(3, 3, 'Card003', TO_DATE('2021-03-15', 'YYYY-MM-DD'), TO_DATE('2021-03-25',
'YYYY-MM-DD'));
INSERT INTO book_lending (book_id, branch_id, card_no, date_out, due_date) VALUES
(4, 1, 'Card003', TO_DATE('2021-04-20', 'YYYY-MM-DD'), TO_DATE('2021-04-30',
'YYYY-MM-DD'));
INSERT INTO book_lending (book_id, branch_id, card_no, date_out, due_date) VALUES
(5, 2, 'Card005', TO_DATE('2021-05-25', 'YYYY-MM-DD'), TO_DATE('2021-06-04',
'YYYY-MM-DD'));

Retrieve Details of All Books

SELECT
b.book_id,
b.title,
b.publisher_name,
ba.author_name,
p.address AS publisher_address,
p.phone AS publisher_phone,
bc.branch_id,
lb.branch_name,
lb.address AS branch_address,
bc.no_of_copies
FROM
book b
JOIN
book_authors ba ON b.book_id = ba.book_id
JOIN
publisher p ON b.publisher_name = p.name
JOIN
book_copies bc ON b.book_id = bc.book_id
JOIN
library_branch lb ON bc.branch_id = lb.branch_id;

Get Borrowers with More Than 3 Books Borrowed from Jan 2020 to Jun 2022

SELECT
bl.card_no,
COUNT(bl.book_id) AS books_borrowed
FROM
book_lending bl
WHERE
bl.date_out BETWEEN TO_DATE('2020-01-01', 'YYYY-MM-DD') AND
TO_DATE('2022-06-30', 'YYYY-MM-DD')
GROUP BY
bl.card_no
HAVING
COUNT(bl.book_id) > 3;

Delete a Book and Update Other Tables

-- Deleting related records from book_authors table


DELETE FROM book_authors WHERE book_id = 5;

-- Deleting related records from book_copies table


DELETE FROM book_copies WHERE book_id = 5;

-- Deleting related records from book_lending table


DELETE FROM book_lending WHERE book_id = 5;

-- Finally, deleting the book record from the book table


DELETE FROM book WHERE book_id = 5;
--After delete checking only (Do write below content in observation)

You might also like