A relational database project built with PostgreSQL to model a realistic library system.
This project demonstrates strong fundamentals in schema design, relational modeling, constraints, views, triggers, and analytical SQL queries, with an emphasis on data integrity and real-world use cases.
This database models a library where:
- Members borrow books
- Books can have one or more authors
- Loans track borrowing activity over time
- Late returns automatically generate fines
The project was designed and tested in PostgreSQL and organized for reproducibility and clarity, following standard database engineering practices.
- Relational schema design
- Primary keys & foreign keys
- Many-to-many relationships
- Data integrity via constraints
- Indexes for performance
- Views for reusable query logic
- Triggers for automatic business rules
- Analytical SQL queries (joins, filters)
members– library membersbooks– books and availability statusauthors– book authorsbook_authors– join table (many-to-many relationship)loans– borrowing events over timefines– penalties for late returns
- Many-to-many relationships are modeled using a composite primary key (
book_authors) - Triggers enforce business rules directly in the database
- Indexes are added only where query patterns justify them
- Seed data uses explicit IDs for deterministic rebuilds
- When a loan’s
return_dateis updated:- If the return is late, a fine is automatically created
- This logic is enforced at the database level, not the application layer
library-database/
├── schema/
│ └── schema.sql # Table definitions, PKs, FKs, constraints
├── seed/
│ └── seed.sql # Deterministic sample data with explicit IDs
├── views/
│ └── available_books.sql # Reusable view for available books
├── functions/
│ └── apply_overdue_fine.sql
├── triggers/
│ └── overdue_fine.sql
├── indexes/
│ └── indexes.sql
├── queries/
│ └── analytics.sql # Example analytical queries
└── README.md
Run the SQL files in this order:
schema/schema.sqlseed/seed.sqlindexes/indexes.sqlviews/available_books.sqlfunctions/functions.sqltriggers/overdue_fine.sql
After running these files, the database will be fully recreated with sample data.
The queries/analytics.sql file includes example queries such as:
- Which books are currently available?
- Which loans are overdue?
These queries are read-only and are intended to demonstrate SQL reasoning and analysis.
- All data is fake and for demonstration purposes only
- Primary keys are explicitly set for reproducibility
- Sequences are reset after seeding to allow normal inserts
- Seed data intentionally includes edge cases (late returns, multi-author books)
- PostgreSQL
- DBeaver (development & testing)
- SQL
This project was built to demonstrate practical SQL skills beyond basic CRUD operations, including:
- Designing schemas that reflect real-world systems
- Enforcing rules with database-level logic
- Writing queries that answer meaningful questions
- Organizing database code in a professional, reusable way