Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
MySQL + SQL · Lesson 118

Library Database Project in MySQL

In this lesson: Library database project in MySQL — books, members, issue/return tables and queries with output.

The project

A library database is a favourite DBMS project because it shows a clear many-to-many relationship: members borrow books, and each book can be borrowed by many members over time. The heart of the design is a borrowings link table.

💡 members ↔ books is many-to-many, resolved by a borrowings link table with both keys.

Designing the tables

  • members — id, name, phone.
  • books — id, title, author.
  • borrowings — id, member_id (FK), book_id (FK), issue_date, due_date, return_date.

Members and books connect only through borrowings, which records each loan.

Creating the schema

CREATE TABLE members (
    id INT PRIMARY KEY AUTO_INCREMENT,
    name VARCHAR(50) NOT NULL,
    phone VARCHAR(15)
);
CREATE TABLE books (
    id INT PRIMARY KEY AUTO_INCREMENT,
    title VARCHAR(100) NOT NULL,
    author VARCHAR(50)
);
CREATE TABLE borrowings (
    id INT PRIMARY KEY AUTO_INCREMENT,
    member_id INT, book_id INT,
    issue_date DATE, due_date DATE, return_date DATE,
    FOREIGN KEY (member_id) REFERENCES members(id),
    FOREIGN KEY (book_id) REFERENCES books(id)
);

The borrowing link table

The borrowings table is the many-to-many link. Each row is one loan: which member took which book, when it was issued, when it is due, and when it was returned. A NULL return_date means the book is still out.

Sample data

INSERT INTO members (name, phone) VALUES ('Aman', '9990001'), ('Diya', '9990002');
INSERT INTO books (title, author) VALUES ('SQL Basics', 'R. Roy'), ('PHP Guide', 'S. Iyer');
INSERT INTO borrowings (member_id, book_id, issue_date, due_date, return_date) VALUES
    (1, 1, '2026-09-01', '2026-09-15', NULL),
    (2, 2, '2026-08-20', '2026-09-03', '2026-09-02');

Useful queries

-- currently issued books (not returned)
SELECT m.name, b.title, br.due_date
FROM borrowings br
JOIN members m ON br.member_id = m.id
JOIN books b ON br.book_id = b.id
WHERE br.return_date IS NULL;

-- overdue books
SELECT m.name, b.title, br.due_date
FROM borrowings br
JOIN members m ON br.member_id = m.id
JOIN books b ON br.book_id = b.id
WHERE br.return_date IS NULL AND br.due_date < CURDATE();

-- how many books each member has borrowed
SELECT m.name, COUNT(*) AS total_borrowed
FROM borrowings br JOIN members m ON br.member_id = m.id
GROUP BY m.id;

Common mistakes

  • Putting member_id directly in books. That cannot model many-to-many; use a link table.
  • No return_date column. You need it to tell issued from returned.
  • Deleting a book that has borrowings. Handle the foreign key (RESTRICT or archive).
  • Confusing due_date and return_date. Due is the deadline; return is when it came back.

Summary

  • A library database has members, books and a borrowings link table.
  • members ↔ books is many-to-many, resolved by borrowings.
  • Each borrowing records issue, due and return dates.
  • NULL return_date means the book is still issued.
  • Query issued, overdue and per-member counts with JOIN and WHERE.

Next, revise many-to-many in ER diagram examples, or build the school database.

Frequently Asked Questions

How do I design a library database in MySQL?
Make a members table and a books table, then a borrowings table linking them with member_id and book_id, plus issue and return dates. Members and books have a many-to-many relationship, resolved by the borrowings link table.
Why does a library database need a borrowing table?
A member can borrow many books and a book can be borrowed (over time) by many members — a many-to-many relationship. You cannot model that with a single foreign key, so a borrowings link table with both keys is required.
How do I find which books are currently issued?
Query the borrowings table for rows where the return_date is NULL: SELECT * FROM borrowings WHERE return_date IS NULL;. A missing return date means the book is still out with a member.
How do I find overdue books?
Filter borrowings that are not returned and whose due date has passed: WHERE return_date IS NULL AND due_date < CURDATE(). This lists books that should have been returned by now.
Is a library database a good DBMS project?
Yes. A library database is a classic project because it clearly demonstrates a many-to-many relationship through a link table, plus useful real-world queries like issued, returned and overdue books.
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.
Next topicHospital Database Project Your progress: 118 / 151 (78%) · Lesson 119 / 151 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free