MySQL Project: Library Management System
Project Overview
Objective: Design and implement a relational database for a library system,
including tables, constraints, transactions, joins, views, and other MySQL concepts. This
project
will help students learn and practice fundamental MySQL features in a real-world
scenario.
Database Name: library_management
Part 1: Database Design and Schema Creation
1. Tables and Structure
- Books: Stores information about each book.
- book_id (INT, Primary Key)
- title (VARCHAR, NOT NULL)
- author_id (INT, Foreign Key referencing Authors.author_id)
- genre (VARCHAR)
- publication_year (YEAR)
- price (DECIMAL(10, 2))
- Authors: Stores information about book authors.
- author_id (INT, Primary Key)
- name (VARCHAR, NOT NULL)
- Members: Stores information about members who borrow books.
- member_id (INT, Primary Key)
- name (VARCHAR, NOT NULL)
- email (VARCHAR, UNIQUE, NOT NULL)
- join_date (DATE)
- Loans: Stores records of books that are loaned out to members.
- loan_id (INT, Primary Key)
- book_id (INT, Foreign Key referencing Books.book_id, NOT NULL)
- member_id (INT, Foreign Key referencing Members.member_id, NOT NULL)
- loan_date (DATE, Default: Current Date)
- return_date (DATE, Nullable)
2. Constraints
Constraints:
- Ensure book_id, author_id, member_id, and loan_id have appropriate Primary Keys and
Foreign Key constraints.
- Set a NOT NULL constraint on all necessary columns.
- Set UNIQUE constraints on fields like email in Members.
- Add CHECK constraints to enforce logical data limits:
- In the Books table, ensure the publication_year is between 1800 and the current year.
- In the Members table, ensure the email column has a valid email format using a
CHECK constraint on the pattern.
- In the Loans table, ensure that return_date is either NULL or greater than loan_date to
avoid inconsistencies.
Part 2: Implementing Joins and Queries
1. Inner Join:
- Write a query to display the title and author name for each book.
2. Left Join:
- Write a query to display all members and the books they have currently loaned (if any).
Include members who haven’t borrowed any books.
3. Aggregates with Group By:
- Write a query to find the number of books each author has in the library.
4. Subqueries:
- Write a query to find the books that were published in the same year as the oldest book
in the library.
5. Views:
- Create a view called borrowed_books that lists all currently loaned books with columns:
title, member_name, loan_date.
Part 3: Transactions and Error Handling
1. Transactions:
- Implement a transaction for borrowing a book. The transaction should:
1. Check if the book is already loaned out (check if return_date is NULL for an existing
Loans record).
2. If not loaned out, create a new entry in Loans.
3. If the book is unavailable, rollback the transaction and show an error message.
2. Error Handling:
- Handle potential errors like duplicate member registration (e.g., unique constraint on
email).
- Use Stored Procedures to wrap this functionality, allowing members to borrow and
return books with error handling for availability and constraints.
Part 4: Advanced MySQL Concepts
1. Views with Aggregation:
- Create a view called author_stats that displays the author_id, author_name, and total
number of books they have authored.
2. Triggers:
- Create a trigger to automatically update the return_date in Loans when a book is
returned. This trigger should ensure that return_date is set to the current date once the
book is marked as returned.
3. Stored Procedures:
- Write a stored procedure borrow_book(member_id, book_id) to manage the borrowing
transaction, with checks for availability.
- Write another stored procedure return_book(loan_id) to handle book returns, setting
return_date to the current date.
Part 5: Additional Queries and Reports
1. Frequently Borrowed Books:
- Write a query to display the top 5 most frequently borrowed books, along with the
number of times they have been borrowed.
2. Books Not Borrowed:
- Write a query to list books that have never been borrowed.
3. Books Due for Return:
- Write a query that lists all books due for return within the next 7 days, displaying title,
member_name, and loan_date.
Final Project Submission
1. Database Export:
- Export the complete database schema and data as a .sql file.
2. Documentation:
- Write a brief document (one page per part) explaining:
- What each part of the project does.
- Example SQL statements for each query.
- Explanations of any stored procedures, triggers, and views created.
3. Project Presentation:
- Each student should prepare a short presentation (5-10 minutes) to showcase their
solution, highlighting the structure, queries, and additional features they’ve implemented.