Untitled Document
Untitled Document
Here is the database schema for the library system, along with SQL queries to
perform the requested operations.
Library Database
Schema and
Operations
● Books: Stores information about the books available in the library. Each
book is uniquely identified by its ISBN.
● Members: Stores information about the library members. Each member
is uniquely identified by a MemberID.
● Loans: Records instances of books being borrowed by members. Each
loan is uniquely identified by a LoanID and links a specific book (by
ISBN) to a specific member (by MemberID) with dates for loan and
return.
SQL Script
Relationships:
-- Drop tables if they exist to allow for clean re-creation -- This is useful for
development and testing purposes. DROP TABLE IF EXISTS Loans; DROP
TABLE IF EXISTS Books; DROP TABLE IF EXISTS Members;
-- 1. Insert new records into the Books, Members, and Loans tables
-- Create the Loans table CREATE TABLE Loans ( LoanID INT PRIMARY KEY, --
Unique identifier for the loan transaction MemberID INT NOT NULL, -- ID of the
member who borrowed the book ISBN VARCHAR(20) NOT NULL, -- ISBN of the
book that was borrowed LoanDate DATE NOT NULL, -- Date the book was
borrowed ReturnDate DATE, -- Date the book was returned (can be NULL if not
returned yet) FOREIGN KEY (MemberID) REFERENCES Members(MemberID), --
Link to the Members table FOREIGN KEY (ISBN) REFERENCES Books(ISBN) --
Link to the Books table );
-- Insert data into the Books table INSERT INTO Books (ISBN, Title, Author,
Genre, Quantity) VALUES ('978-0321765723', 'The Lord of the Rings', 'J.R.R.
Tolkien', 'Fantasy', 5), ('978-0743273565', 'The Great Gatsby', 'F. Scott
Fitzgerald', 'Classic', 3), ('978-0132350884', 'Clean Code', 'Robert C. Martin',
'Programming', 2);
-- Insert data into the Members table INSERT INTO Members (MemberID, Name,
Email, Phone) VALUES (101, 'Alice Smith', '[email protected]', '555-
1234'), (102, 'Bob Johnson', '[email protected]', '555-5678'), (103,
'Charlie Brown', '[email protected]', '555-8765');
-- Display all records in the Members table after insertion SELECT * FROM
Members;
-- Display all records in the Books table after insertion SELECT * FROM Books;
-- Insert data into the Loans table -- Assume MemberID 101 borrows 'The Lord
of the Rings' and 'Clean Code' -- Assume MemberID 102 borrows 'The Great
Gatsby' INSERT INTO Loans (LoanID, MemberID, ISBN, LoanDate, ReturnDate)
VALUES (1, 101, '978-0321765723', '2025-04-15', NULL), -- Alice borrows Lord of
the Rings (2, 102, '978-0743273565', '2025-04-20', NULL), -- Bob borrows The
Great Gatsby (3, 101, '978-0132350884', '2025-04-22', NULL); -- Alice borrows
Clean Code
-- Display all records in the Loans table after insertion SELECT * FROM Loans;
-- 3. Update the quantity of a particular book in the Books table -- Let's assume
one copy of 'The Great Gatsby' is returned, increasing the quantity by 1. -- The
current quantity for '978-0743273565' is 3. We will update it to 4. UPDATE
Books SET Quantity = Quantity + 1 -- Increase quantity by 1 WHERE ISBN =
'978-0743273565'; -- Specify the ISBN of the book to update
-- Display the updated Books table to verify SELECT * FROM Books WHERE
ISBN = '978-0743273565';
1. Vidhya, V., Jeyaram, G., & Ishwarya, K. (2016). Database management systems.
Alpha Science International.
2. Peterson, R. (2023, December 16). SQL Cheat sheet with commands &
description (2024). GURU99. https://www.guru99.com/sql-cheat-sheet.html
3. SQL: How to create and delete a table with SQL query. (n.d.). EnableGeek.
https://www.enablegeek.com/tutorial/create-delete-table-sql-query/