Skip to content

Beginner Project: A School Library Database

Programming SQL Difficulty 1/5

Beginning of the lesson

Beginner Project: A School Library Database

Put everything you have learned together and build a real, working library database.

Big Idea: A database is most useful when it answers real questions. In this final project you will design three tables, fill them with sample data, and then ask the database questions like "Which book is borrowed the most?" and "Which books have never been taken out?"
By the end you will be able to:
  • plan a small database of books, students and loans,
  • write CREATE TABLE statements with a primary key and foreign keys,
  • add rows with INSERT,
  • answer real questions using SELECT, WHERE, ORDER BY, JOIN and GROUP BY.

1. The plan: three tables

A school library keeps track of three kinds of thing, so we will use three tables. Think of each table as one neat list:

  • books - one row for each book the library owns.
  • students - one row for each student who can borrow.
  • loans - one row every time a student borrows a book.

Why three tables and not one big one? Because a single giant table would repeat the same book title and the same student name over and over. Splitting the data keeps each fact stored once. The loans table is the clever part: instead of copying a whole book and a whole student, each loan just stores the book_id and the student_id. Those little numbers point back to the real rows. That pointer is called a foreign key.

books book_id (PK) title author category copies loans loan_id (PK) book_id (FK) student_id (FK) loan_date return_date students student_id (PK) name grade loans links books and students together
The loans table connects a book to a student. book_id and student_id are foreign keys.

Analogy: imagine a shelf of books and a class of students. A loan is like a little sticky note that says "book number 1 is with student number 3". The note does not copy the whole book or the whole student - it just writes down their numbers. That is exactly what a foreign key does.

Continue this lesson on YourAIZone

The full lesson has the complete explanation, worked examples, common mistakes, tests with instant checking and an AI tutor. Start right away as a guest, or create an account to keep your progress.

In the full lesson

  • The complete explanation
  • Worked examples
  • Common mistakes and how to avoid them
  • Test questions: 15
  • AI tutor that answers questions about the topic
  • Progress tracking for students, parents and teachers