0% found this document useful (0 votes)
8 views4 pages

Untitled Document

The library database is structured with three main tables: Books, Members, and Loans, each uniquely identified to manage book information, member details, and loan transactions. It outlines SQL operations for creating tables, inserting records, and managing relationships between members and loans, as well as books and loans. The document includes example SQL scripts for data insertion and retrieval, along with references for further reading on database management systems.

Uploaded by

auw242154
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views4 pages

Untitled Document

The library database is structured with three main tables: Books, Members, and Loans, each uniquely identified to manage book information, member details, and loan transactions. It outlines SQL operations for creating tables, inserting records, and managing relationships between members and loans, as well as books and loans. The document includes example SQL scripts for data insertion and retrieval, along with references for further reading on database management systems.

Uploaded by

auw242154
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

The library database is designed to manage books, members, and the loans

between them. It consists of three main tables:

Database Schema Overview

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:

● There is a one-to-many relationship between Members and Loans (one


member can have many loans).
● There is a one-to-many relationship between Books and Loans (one book
can be involved in many loans over time, though a specific copy might
only be on one loan at a time depending on implementation details not
covered here, like tracking individual copies).
-- Brief overview of the database schema:
-- This script creates a simple library database with tables
for Books, Members, and Loans.
-- Books table stores book details.
-- Members table stores member details.
-- Loans table records borrowing transactions, linking books
and members.

-- 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;

-- Create the Books table CREATE TABLE Books ( ISBN VARCHAR(20)


PRIMARY KEY, -- Unique identifier for the book Title VARCHAR(255) NOT
NULL, -- Title of the book Author VARCHAR(255) NOT NULL, -- Author of the
book Genre VARCHAR(100), -- Genre of the book Quantity INT NOT NULL
CHECK (Quantity >= 0) -- Number of copies available, cannot be negative );

-- Create the Members table CREATE TABLE Members ( MemberID INT


PRIMARY KEY, -- Unique identifier for the member Name VARCHAR(255) NOT
NULL, -- Full name of the member Email VARCHAR(255) UNIQUE NOT NULL, --
Email address, must be unique Phone VARCHAR(20) -- Phone number of the
member );

-- 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;

-- 2. Retrieve all information about books borrowed by a specific member --


Let's retrieve books borrowed by MemberID 101 (Alice Smith) SELECT b.ISBN,
b.Title, b.Author, b.Genre, l.LoanDate, l.ReturnDate FROM Books b JOIN Loans
l ON b.ISBN = l.ISBN WHERE l.MemberID = 101; -- Specify the MemberID here

-- 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';

-- 4. Delete a member record from the Members table -- To delete a member, we


must first delete any loans associated with that member -- due to the foreign
key constraint. -- Let's delete MemberID 103 (Charlie Brown), who has no loans
in our current data. -- If we were deleting MemberID 101 or 102, we would first
delete their loans.

Screenshots Displaying Records

-Screenshot: Records in the Books table after insertion


References:

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/

You might also like