Sco 206 Project
Sco 206 Project
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.
b) Convert the ERD above into SQL statements to create the necessary relations,
considering primary keys, foreign keys, constraints, and data types.
USE NairobiLibrary;
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.
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.
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.
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.
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.
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.
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.
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:
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';
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.