CASE STUDY 03 · RESOURCE LIFECYCLE

Library Operations Constraints + Triggers

Library Database

Distinguish a book title from its physical copies, enforce one open loan per copy, coordinate issue/return state and produce availability and overdue reports.

01 · PROBLEM & VOCABULARY

A title is not a lendable object

A catalogue may describe one title while the library owns several physical copies. A member borrows a particular accessioned copy, not the abstract title. Mixing these concepts makes “available” ambiguous and can allow the same copy to be issued twice.

Catalogue

ISBN, title, author and subject describe a work/edition.

Inventory

Accession number and status identify a physical copy.

Circulation

Loan links one member to one copy across time.

Scope: The demonstration uses a fixed reference date for reproducible overdue output. Production queries use the database date and an approved fine policy.
02 · BUSINESS RULES

Represent valid states explicitly

RuleMechanismMeaning
ISBN and accession number are uniqueUNIQUENo duplicate catalogue/copy identity
Every copy belongs to a known bookFOREIGN KEYNo orphan inventory
Due date is not before issue dateCHECKValid loan interval
A copy has at most one unreturned loanPartial UNIQUE indexNo double issue
Return date is absent until returnedNULL semanticsOpen versus closed loan

Member limits differ for students and faculty and are better represented by a policy table in an extension. “Lost” and “Repair” are copy states independent of title. A real design must define how a lost copy closes or suspends its loan.

03 · NORMALIZATION & CARDINALITY

Book 1—N copy 1—N loan

Book
1 → N
Copy
1 → N
Loan
N ← 1
Member

Book metadata is stored once. Each copy stores only inventory-specific facts. Loan stores the temporal relationship, so returning a book updates the loan instead of deleting history. This supports borrowing statistics and dispute investigation.

The partial index UNIQUE(copy_id) WHERE returned_on IS NULL is more precise than a general unique copy constraint: one copy may have many historical loans but only one open loan. Partial indexes are SQLite/PostgreSQL-style features; other databases may use a generated column, filtered index or transaction with locking.

04 · ISSUE & RETURN TRANSACTIONS

Protect invariants under concurrent requests

Issue

  1. Confirm member is active and within their current policy limit.
  2. Confirm copy is available.
  3. Insert an open loan inside a transaction.
  4. Let uniqueness reject a competing open loan; trigger marks the copy On Loan.
  5. Commit only if every step succeeds.

Return

  1. Update the matching open loan with a return date.
  2. The return trigger marks its copy Available.
  3. Calculate charges from the policy effective for that loan.
  4. Commit; if no open row changed, investigate rather than creating a false return.
Single source question: status is derivable from an open loan but is also needed for Lost/Repair. If both loan and status are stored, constrain all write paths so they agree.
05 · AVAILABILITY, OVERDUE & POPULARITY

Choose joins that match the question

Availability begins with book joined to copy because owned copies must exist. Overdue reporting begins with open loan and joins member, copy and book for readable identifiers. Popularity uses LEFT JOIN loan so titles with zero loans are retained.

days overdue = reference date − due date, only when returned_on IS NULL

An illustrative fine of ₹2 per day appears in the demonstration but is not claimed as institutional policy. Production code should store fine rules with effective dates, cap rules and waiver audit. Date calculations and Boolean expressions differ among SQL products, so portability must be planned.

06 · COMPLETE IMPLEMENTATION

Executable SQLite lifecycle and reports

programs/library-database.sql
Loading source…

Run with sqlite3 :memory: < library-database.sql. It creates the normalized schema, partial unique index, two synchronization triggers, deterministic records, availability/overdue reports and an atomic return.

07 · INTERACTIVE RETURN TRACE

Follow loan 101 back to availability

  1. Observe open loan.
  2. Open transaction.
  3. Guard the return.
  4. Validate chronology.
  5. Synchronize inventory.
  6. Publish consistent state.
  7. Verify postcondition.
Current state

Press Next to begin.

08 · INVARIANT TESTS

Test the lifecycle, not only SELECT output

Multiple physical copies
Create two copies of one title; loan one and verify availability reports one remaining copy.
Double issue
Insert a second open loan for the same copy and expect the partial unique index to reject it.
Historical reissue
Close a loan, then insert a later loan for that copy; this must succeed.
Invalid chronology
Reject due/return dates before the issue date.
Idempotent return
Repeat the guarded return; zero rows should change and no new state should be invented.
Zero-loan title
Confirm the popularity report retains a title with borrow_count zero.
09 · PERFORMANCE & RECOVERY

Indexes follow circulation access paths

The unique accession index supports scanning/issue lookup. The partial index both enforces and finds open loans by copy. idx_loan_member_open supports a member’s current/history list. An overdue workload may benefit from an index involving returned_on and due_on; verify selectivity and plans before adding it.

Transactions do not replace durability planning. Use tested backups, point-in-time recovery where required, role-based access, change logs and periodic inventory reconciliation. Barcode input must still be validated; fast incorrect input remains incorrect.

10 · KNOWLEDGE CHECK & EXTENSIONS

Reason about identity and time

What does loan.copy_id reference?

Why use a partial unique index?

Extensions

  1. Add reservations with an ordered queue and expiration time.
  2. Model fine policies by member type and effective date.
  3. Add acquisitions, suppliers and copy withdrawal history.
  4. Create monthly circulation and most-borrowed-subject reports.
  5. Implement safe issue/return procedures in PostgreSQL or MySQL and compare syntax.
11 · INTERVIEW PREPARATION

Explain why time changes the design

Why separate book and copy?

Shared bibliographic facts describe the title; accession/status describe each lendable unit. Separation prevents duplication and supports correct availability.

Why not delete a loan on return?

Closing it preserves history for analytics, audits and disputes.

What does a partial index do?

It indexes only rows satisfying a predicate; adding UNIQUE enforces uniqueness only inside that subset.

LEFT JOIN purpose in popularity?

It keeps books that have copies but no matching loans, allowing a zero count.

How do transactions prevent double issue?

They make the operation atomic; a unique invariant or appropriate locking resolves concurrent attempts.

12 · KEY TAKEAWAY

Model the physical object and its history separately

A trustworthy library system distinguishes catalogue from inventory, represents open/closed time explicitly and enforces the one-open-loan rule at the database boundary. Transactions coordinate lifecycle changes; carefully chosen joins turn that reliable history into useful reports.