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

Programming Assignment Unit 4

The document outlines a programming assignment for a library database management system using SQL, focusing on creating and manipulating data across three tables: Books, Members, and Loans. It includes SQL scripts for table creation, data insertion, and example queries, emphasizing database design principles like normalization and referential integrity. The assignment demonstrates the application of SQL commands to manage library operations effectively.

Uploaded by

Lutalo Michael
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)
5 views

Programming Assignment Unit 4

The document outlines a programming assignment for a library database management system using SQL, focusing on creating and manipulating data across three tables: Books, Members, and Loans. It includes SQL scripts for table creation, data insertion, and example queries, emphasizing database design principles like normalization and referential integrity. The assignment demonstrates the application of SQL commands to manage library operations effectively.

Uploaded by

Lutalo Michael
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/ 9

UNIVERSITY OF THE PEOPLE (UoPeople)

CS 2203-01 - Databases 1

Lutalo Martin

BSc. Computer Science

CS 2203-01 - AY2025-T4 / Programming Assignment Unit 4

Instructor Baraka Laiza

8th May 2025


Library database management using SQL
Introduction
Structured Query Language (SQL) is not only recognizable, it serves a major role in database
management systems by providing a standard way to interact with relational databases. For the
purposes of this activity, we will create a simple library system using SQL to create and
manipulate data in three straightforward tables; Books, Members, and Loans. The guiding
objective is to create a working schema, and then execute simple SQL operations using DDL
(Data Definition Language) and DML (Data Manipulation Language).
Database schema overview
The database consists of the following entities:
 Books – This entity holds information such as ISBN, title, author, genre, and number of
available copies for each book.
 Members – This entity holds details of member information such as MemberID, Name,
Email, and Phone.
 Loans – This entity tracks which books are borrowed by which members containing
LoanID, MemberID, ISBN, LoanDate and Return Date.
SQL script:

-- TABLE CLEANUP (FOR RESETTING DATABASE)

-- Drop tables in reverse order of dependency to avoid foreign key violations


-- Loans must be dropped first as it references both Books and Members
DROP TABLE IF EXISTS Loans;
DROP TABLE IF EXISTS Members;
DROP TABLE IF EXISTS Books;

-- TABLE CREATION WITH CONSTRAINTS


-- Books table stores the library's book inventory
-- ISBN is used as primary key as it's a unique international standard identifier
CREATE TABLE Books (
ISBN VARCHAR(13) PRIMARY KEY, -- International Standard Book Number (13 digits)
Title VARCHAR(100) NOT NULL, -- Book title (required)
Author VARCHAR(100) NOT NULL, -- Author name (required)
Genre VARCHAR(50), -- Book category/genre
Quantity INT DEFAULT 1, -- Copies available (default 1)

-- Ensure we always have at least 0 copies


CONSTRAINT chk_quantity CHECK (Quantity >= 0)
);

-- Members table stores registered library users


CREATE TABLE Members (
MemberID INT PRIMARY KEY, -- Unique member identifier
Name VARCHAR(100) NOT NULL, -- Member's full name (required)
Email VARCHAR(100) UNIQUE, -- Email address (must be unique)
Phone VARCHAR(15) -- Contact number
);

-- Loans table tracks book borrowing transactions


-- Uses ON DELETE CASCADE to automatically remove loan records when
-- either the associated book or member is deleted
CREATE TABLE Loans (
LoanID INT PRIMARY KEY, -- Unique loan identifier
MemberID INT NOT NULL, -- Borrowing member (required)
ISBN VARCHAR(13) NOT NULL, -- Borrowed book (required)
LoanDate DATE DEFAULT CURRENT_DATE, -- Date borrowed (defaults to today)
ReturnDate DATE, -- Expected return date

-- Foreign key to Members with cascading delete


FOREIGN KEY (MemberID)
REFERENCES Members(MemberID)
ON DELETE CASCADE,

-- Foreign key to Books with cascading delete


FOREIGN KEY (ISBN)
REFERENCES Books(ISBN)
ON DELETE CASCADE,

-- Ensure return date is after loan date


CONSTRAINT chk_dates CHECK (ReturnDate > LoanDate)
);

-- DATA INSERTION

-- Insert sample book records


INSERT INTO Books VALUES
('9780131103627', 'The C Programming Language', 'Kernighan & Ritchie', 'Programming', 5);

INSERT INTO Books VALUES


('9780596009205', 'Head First Java', 'Kathy Sierra', 'Programming', 3);

-- Insert sample member records


INSERT INTO Members VALUES
(1, 'Lutalo Martin', '[email protected]', '0703533442');

INSERT INTO Members VALUES


(2, 'Mukiibi Mark', '[email protected]', '0779269605');

-- Insert sample loan transactions


INSERT INTO Loans VALUES
(101, 1, '9780131103627', '2025-05-01', '2025-05-10');

INSERT INTO Loans VALUES


(102, 2, '9780596009205', '2025-05-03', '2025-05-13');

-- EXAMPLE QUERIES

-- Query 1: Retrieve all books borrowed by MemberID 1


-- Demonstrates a basic join operation between Books and Loans
SELECT Books.*
FROM Books
JOIN Loans ON Books.ISBN = Loans.ISBN
WHERE Loans.MemberID = 1;

-- Query 2: Update book quantity when a copy is borrowed


-- Shows how to maintain inventory levels
UPDATE Books
SET Quantity = Quantity - 1
WHERE ISBN = '9780131103627';

-- Query 3: Safe member deletion process


-- First delete any associated loans to maintain referential integrity
DELETE FROM Loans WHERE MemberID = 2;

-- Then delete the member record


DELETE FROM Members WHERE MemberID = 2;

-- DATABASE DESIGN NOTES

/*
KEY DESIGN DECISIONS:

1. Normalization:
- Achieved 3NF by separating entities into distinct tables
- No redundant data storage
- All non-key attributes depend only on primary keys

2. Referential Integrity:
- ON DELETE CASCADE ensures automatic cleanup of loan records
- Prevents orphaned records when books/members are deleted
3. Data Validation:
- CHECK constraints enforce business rules
- NOT NULL constraints prevent missing critical data
- UNIQUE constraint on email prevents duplicate accounts

4. Practical Considerations:
- ISBN as natural primary key for books
- Default values simplify common operations
- Quantity tracking enables inventory management
*/
Explanation
SQL is divided into several components—DDL, DML, and DCL—that serve distinct purposes.
DDL statements such as CREATE and ALTER define and manage schema structures, while
DML commands like INSERT, SELECT, UPDATE, and DELETE enable efficient data
manipulation (Vidhya et al., 2016). These commands are foundational for database operations
and essential for data retrieval and integrity.
Using appropriate SQL domain types (e.g., VARCHAR, INT, DATE) helps ensure data accuracy,
consistency, and optimal storage (Peterson, 2023). By defining relationships with FOREIGN
KEY constraints, the schema enforces referential integrity between tables—an essential aspect of
relational databases (GeeksforGeeks, 2025).
The SQL queries implemented in this assignment provide the ability to add and track members,
catalog books, and monitor loans. For instance, updating the quantity of books upon lending
ensures the system reflects real-time inventory. Deleting members when necessary maintains a
clean and updated database.
Screenshots of output

Conclusion
This assignment showcases how SQL can be used to model and manage a library system. By
defining a structured schema and leveraging SQL commands, we ensure reliable data storage and
operations. Understanding core SQL operations like CREATE, INSERT, SELECT, UPDATE,
and DELETE equips database managers to efficiently handle day-to-day tasks (EnableGeek, n.d.;
Codecademy, 2025).
References
Vidhya, V., Jeyaram, G., & Ishwarya, K. (2016). Database management systems. Alpha

Science International.

Peterson, R. (2023, December 16). SQL Cheat sheet with commands & description (2024).

GURU99. https://www.guru99.com/sql-cheat-sheet.html

SQL: How to create and delete a table with SQL query. (n.d.). EnableGeek.

https://www.enablegeek.com/tutorial/create-delete-table-sql-query/

GeeksforGeeks. (2025, January 13). Database schemas.

https://www.geeksforgeeks.org/database-schemas/

Codecademy. (2025, February 26). SQL Commands list: Basic database queries.

https://www.codecademy.com/article/sql-commands

SQL Online Editor. (n.d.). https://www.programiz.com/sql/online-compiler

You might also like