0% found this document useful (0 votes)
24 views11 pages

Sco 206 Project

The document outlines a SQL script for creating and managing a library database named NairobiLibrary, including tables for authors, books, members, and loans. It addresses challenges of managing multiple copies of books and proposes a solution to separate book titles from individual copies. Additionally, it provides SQL statements for modifying member attributes and establishing many-to-many relationships between books and authors.

Uploaded by

Briton Otieno
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)
24 views11 pages

Sco 206 Project

The document outlines a SQL script for creating and managing a library database named NairobiLibrary, including tables for authors, books, members, and loans. It addresses challenges of managing multiple copies of books and proposes a solution to separate book titles from individual copies. Additionally, it provides SQL statements for modifying member attributes and establishing many-to-many relationships between books and authors.

Uploaded by

Briton Otieno
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/ 11

KENYATTA UNIVERSITY

SCHOOL OF PURE AND SCIENCES


DEPARTMENT OF COMPUTING AND INFORMATION
SCIENCE
SCO 206- DATABASE SYSTEMS
INSTRUCTOR- DR ALFRED KARWEGA

OJWANG B. OTIENO
I163/5867/2021
Task 1:
Prepare a SQL script file that once executed would enable the following:
a) Bring into existence a database named NairobiLibrary.

CREATE DATABASE NairobiLibrary;

b) Convert the ERD above into SQL statements to create the necessary relations,
considering primary keys, foreign keys, constraints, and data types.

USE NairobiLibrary;

CREATE TABLE Author (


AuthorID INT PRIMARY KEY AUTO_INCREMENT,
AuthorName VARCHAR(255) NOT NULL,
BirthDate DATE,
Nationality VARCHAR(50)
);

CREATE TABLE Book (


BookID INT PRIMARY KEY AUTO_INCREMENT,
Title VARCHAR(255) NOT NULL,
AuthorID INT,
ISBN VARCHAR(13) NOT NULL,
Genre VARCHAR(50),
PublishedDate DATE,
QuantityAvailable INT,
FOREIGN KEY (AuthorID) REFERENCES Author(AuthorID) ON DELETE SET NULL
);

CREATE TABLE Member (


MemberID INT PRIMARY KEY AUTO_INCREMENT,
MemberName VARCHAR(255) NOT NULL,
PostalAddress VARCHAR(255),
Email VARCHAR(255),
Phone VARCHAR(20)
);

CREATE TABLE Loan (


LoanID INT PRIMARY KEY AUTO_INCREMENT,
BookID INT,
MemberID INT,
LoanDate DATE,
ReturnDate DATE,
FOREIGN KEY (BookID) REFERENCES Book(BookID) ON DELETE CASCADE,
FOREIGN KEY (MemberID) REFERENCES Member(MemberID) ON DELETE
CASCADE
);

c) Populate the relations with the following test data:


INSERT INTO Author (AuthorName, BirthDate, Nationality) VALUES
('F. Scott Fitzgerald', '1896-09-24', 'American'),
('Harper Lee', '1926-04-28', 'American'),
('George Orwell', '1903-06-25', 'British'),
('Jane Austen', '1775-12-16', 'British'),
('J.R.R. Tolkien', '1893-01-03', 'British'),
('Yuval Noah Harari', '1926-04-28', 'Israeli');

INSERT INTO Book (Title, AuthorID, ISBN, Genre, PublishedDate, QuantityAvailable)


VALUES
('The Great Gatsby', 1, '1234567890123', 'Fiction', '2004-09-30', 25),
('To Kill A Mockingbird', 2, '2345678901234', 'Fiction', '2006-10-11', 30),
('1984', 3, '2345678901234', 'Dystopian', '1950-06-08',20),
('Pride and Prejudice', 4, '2345678901234', 'Romance', '1813-01-28', 315),
('The Hobbit', 5, '2345678901234', 'Fantasy', '1937-09-21', 18),
('Sapiens: A brief history of humankind', 6, '3456789012345', 'Science', '2011-04-11', 22);

INSERT INTO Member (MemberName, PostalAddress, Email, Phone) VALUES


('John Njeru', '12345 Nyeri', 'john@example.com', '073-456-7890'),
('Emily Tsuma', '77766 00100 Nairobi', 'emily@example.com', '077-654-3210'),
('David oduor', '212121 02020 Buruburu', 'david@example.com', '076-789-0123'),
('Sarah Lormunyak', '192193 Khayega', 'sarah@example.com', '076-789-0123'),
('Alex Kimanzi', '462346 01010 Mombasa', 'alex@example.com', '076-789-0123');

INSERT INTO Loan (BookID, MemberID, LoanDate, ReturnDate) VALUES


(1, 1, '2023-10-15', '2023-11-05'),
(2, 2, '2023-11-07', NULL),
(2, 2, '2023-11-09', NULL),
(2, 2, '2023-11-09', NULL),
(3, 3, '2023-11-14', '2023-11-20');
d) Briefly outline the problem that is presented (to database designers) by the
library’s requirement to manage situations where it holds multiple copies of a
single title of a book. Clearly indicate the points at which it is sufficient to
identify a book by its title and points where it is necessary to go further and
identify a copy.

The challenge in managing multiple copies of a single title in a library database lies in
distinguishing between the title of a book and individual physical copies or instances of that title.
This introduces a need for a nuanced approach to database design. Here are the key points to
consider:

1. Title Identification:
- Sufficient Information: The title of a book is often sufficient to identify a unique work in a
library catalog. Users typically search and request books based on their titles.
- Primary Key Use: The primary key of the `Book` table can be the `BookID`, ensuring that each
book title is unique in the database.

2. Copy Identification:
- Multiple Copies: When the library holds multiple copies of the same title, it becomes necessary
to distinguish between these copies.
- Quantity Available: The `QuantityAvailable` field in the `Book` table helps manage the total
count of available copies, but it does not distinguish between individual copies.
- Unique Identifiers: To uniquely identify each copy, a separate identifier, such as a copy number
or barcode, may be required. This identifier can be associated with each physical copy and used to
manage circulation and availability.

3. Loan Tracking:
- One-to-Many Relationship: Loans are typically managed in a one-to-many relationship
between books and loans. However, the need to track individual copies becomes crucial when
managing due dates and returns for specific physical copies.
- BookID in Loan Table: The `BookID` in the `Loan` table is sufficient to associate a loan with a
specific title, but it does not differentiate between multiple copies of the same title.

e) Propose a way by which the database design could be modified to effectively


accommodate the concept of a ‘book’ separate from a ‘copy-of-book’.

To effectively accommodate the concept of a 'book' separate from a 'copy-of-book' in the database
design, you can introduce a new table specifically for tracking individual copies. This allows for
better management of each physical instance of a book. Here's a modified design:

1. Book Table:
- Continue to use the existing `Book` table to store information related to the book title, author,
ISBN, genre, etc.

CREATE TABLE Book (


BookID INT PRIMARY KEY AUTO_INCREMENT,
Title VARCHAR(255) NOT NULL,
AuthorID INT,
ISBN VARCHAR(13) NOT NULL,
Genre VARCHAR(50),
PublishedDate DATE,
UNIQUE (Title, AuthorID), -- Ensures unique titles for each author
FOREIGN KEY (AuthorID) REFERENCES Author(AuthorID) ON DELETE SET NULL
);

2. Copy Table:
- Create a new table, let's call it `Copy`, to manage individual copies of books. This table will
have a unique identifier for each copy, a reference to the corresponding book, and a status to
indicate whether the copy is available for loan.

CREATE TABLE Copy (


CopyID INT PRIMARY KEY AUTO_INCREMENT,
BookID INT,
CopyNumber VARCHAR(20) NOT NULL,
Status ENUM('Available', 'On Loan', 'Reserved') DEFAULT 'Available',
UNIQUE (BookID, CopyNumber), -- Ensures unique copy numbers for each book
FOREIGN KEY (BookID) REFERENCES Book(BookID) ON DELETE CASCADE
);

3. Loan Table:
- Update the `Loan` table to reference the `Copy` table instead of the `Book` table. This allows
you to track loans at the copy level.

CREATE TABLE Loan (


LoanID INT PRIMARY KEY AUTO_INCREMENT,
CopyID INT,
MemberID INT,
LoanDate DATE,
ReturnDate DATE,
FOREIGN KEY (CopyID) REFERENCES Copy(CopyID) ON DELETE CASCADE,
FOREIGN KEY (MemberID) REFERENCES Member(MemberID) ON DELETE
CASCADE
);

Task 2
Write out SQL statements that would achieve the following:
a) With an intention to replace the attribute named address with two
attributes: POBox, PostCode and Town, write a set of statements that would
achieve the separation of the data in the original column and recording it
into the new columns, before doing away with the column named address.

1. Add New Columns:


- Add the new columns (`POBox`, `PostCode`, and `Town`) to the `Member` table.

ALTER TABLE Member


ADD COLUMN POBox VARCHAR(20),
ADD COLUMN PostCode VARCHAR(10),
ADD COLUMN Town VARCHAR(50);

2. Update New Columns with Data:


- Update the new columns with the data from the original `address` column.

UPDATE Member
SET
POBox = SUBSTRING_INDEX(address, ' ', 1),
PostCode = SUBSTRING_INDEX(SUBSTRING_INDEX(address, ' ', -2), ' ', 1),
Town = SUBSTRING_INDEX(address, ' ', -1);

This assumes that the `address` format is consistent, with the POBox, PostCode, and Town
separated by spaces. Adjust the extraction logic based on the actual structure of your data.

3. Verify the Data:


- Check the new columns to ensure the data has been correctly separated.

SELECT MemberID, POBox, PostCode, Town


FROM Member;
4. Remove the Original Column:
- After verifying the new columns, you can drop the original `address` column.

ALTER TABLE Member


DROP COLUMN address;

b) Propose a solution to the problem of enabling the desirable state where a book could have
multiple authors while a single author could also be associated with multiple books

To enable a many-to-many relationship between books and authors, you introduce an intermediate
table, referred to as a junction table or an associative table.

1. Modify the Book Table:


- Remove the `AuthorID` column from the `Book` table since a book can have multiple authors.

-- Original Book table


CREATE TABLE Book (
BookID INT PRIMARY KEY AUTO_INCREMENT,
Title VARCHAR(255) NOT NULL,
ISBN VARCHAR(13) NOT NULL,
Genre VARCHAR(50),
PublishedDate DATE,
QuantityAvailable INT
);

2. Create AuthorBook Table:


- Introduce a new table named `AuthorBook` to represent the many-to-many relationship between
books and authors. This table will store associations between book IDs and author IDs.

CREATE TABLE AuthorBook (


AuthorBookID INT PRIMARY KEY AUTO_INCREMENT,
BookID INT,
AuthorID INT,
FOREIGN KEY (BookID) REFERENCES Book(BookID) ON DELETE CASCADE,
FOREIGN KEY (AuthorID) REFERENCES Author(AuthorID) ON DELETE CASCADE
);
3. Update Author Table (Optional):
- If needed, you can add a column to the `Author` table to represent the total number of books
written by that author.

ALTER TABLE Author


ADD COLUMN TotalBooksWritten INT DEFAULT 0;

c) Make the following changes to the relation named member


i) Correct the Title and ISBN of the book titled '1984':

UPDATE Book
SET Title = 'The Year That Was', ISBN = '999111000'
WHERE Title = '1984';

ii) List the names and nationalities of authors whose books are still out on loan:

SELECT DISTINCT A.AuthorName, A.Nationality


FROM Author A
JOIN AuthorBook AB ON A.AuthorID = AB.AuthorID
JOIN Book B ON AB.BookID = B.BookID
JOIN Loan L ON B.BookID = L.BookID
WHERE L.ReturnDate IS NULL;

iii) Delete the author associated with the book titled 'Pride and Prejudice':

-- Find the AuthorID associated with the book 'Pride and Prejudice'
SELECT AB.AuthorID
FROM AuthorBook AB
JOIN Book B ON AB.BookID = B.BookID
WHERE B.Title = 'Pride and Prejudice';

-- Delete the author from AuthorBook


DELETE FROM AuthorBook
WHERE AuthorID = 1;
-- Delete the author from the Author table
DELETE FROM Author
WHERE AuthorID = 2;

Deleting the author associated with 'Pride and Prejudice' will remove the record from the
`AuthorBook` table, breaking the association between the author and the book. However, there are
potential problems with this action:

- Integrity Issues: If other books are also associated with the same author in the `AuthorBook`
table, those associations will be lost as well. Deleting an author might lead to integrity issues if the
author has written multiple books.

- Loss of Data: Deleting an author removes not only the association but also the author's
information from the `Author` table. If the author has no other associated books, this might not be a
problem, but if the author is associated with multiple books, consider updating the association
instead of deleting.

d) Write out a query that achieves the following:


i) Left Outer Join:

-- Left Outer Join Example


SELECT Book.Title, Author.AuthorName
FROM Book
LEFT JOIN Author ON Book.AuthorID = Author.AuthorID;

ii) Full Join:

-- Full Join Example


SELECT Book.Title, Author.AuthorName
FROM Book
FULL JOIN Author ON Book.AuthorID = Author.AuthorID;

iii) Subquery with Set Operation:

-- Subquery with Set Operation Example


SELECT Title, ISBN
FROM Book
WHERE BookID IN (
SELECT BookID FROM Loan
);

iv) List Borrowed Books with Borrowing Statistics:

-- Borrowed Books with Statistics Example


SELECT
B.Title,
B.ISBN,
COUNT(L.LoanID) AS BorrowCount
FROM
Book B
LEFT JOIN
Loan L ON B.BookID = L.BookID
GROUP BY
B.Title, B.ISBN;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy