CHAPTER 29
Intermediate
Real-World Database Design Projects
Updated: May 16, 2026
8 min read
# CHAPTER 29
Real-World Database Design Projects
1. Introduction
You have learned the syntax, the data types, the constraints, and the optimization techniques. But typing SQL commands is only half of a Database Engineer's job. The other half is Architecture—the ability to look at a complex business requirement and translate it into a perfectly normalized, highly performant relational schema. In this chapter, we will walk through the blueprinting of three massive, real-world database architectures.2. Learning Objectives
By the end of this chapter, you will be able to:- Architect an E-Commerce system handling products, users, and orders.
- Understand the complexity of tracking historical data (Pricing).
- Architect a SaaS (Software as a Service) multi-tenant schema.
- Architect a highly regulated Hospital Management System.
- Apply JSONB, Constraints, and Primary Keys effectively in the real world.
3. Project 1: The E-Commerce Platform
A basic E-Commerce store needs Users, Products, Orders, and Order Items. The Architectural Challenge: If a product costs $10 today, John buys it, and the admin changes the price to $15 tomorrow, John's historical receipt must still say $10! You cannot justJOIN the live product table. The price at the exact moment of checkout must be permanently frozen.
sql
4. Project 2: The Multi-Tenant SaaS App (e.g., Slack or Trello)
A SaaS application has many "Organizations" (Companies), and each Organization has many Users. The Architectural Challenge: You must strictly ensure that User A from Company 1 can never view the data of Company 2. We use atenant_id (Organization ID) on almost every table to enforce absolute data isolation.
sql
*(By requiring WHERE org_id = ? on every query, the backend ensures data never bleeds between companies).*
5. Project 3: Hospital Management System
A hospital handles incredibly sensitive, highly regulated (HIPAA) data. The Architectural Challenge: Doctors treat many Patients, and Patients see many Doctors. This is a massive N:M relationship. Furthermore, we must track every single Appointment meticulously.
sql
6. The Danger of Over-Normalization
Looking at the Hospital schema, a purely academic database architect might say: *"A Patient has a First Name and Last Name. They should be stored in separate columns!"* While academically correct (1NF), it makes the application highly tedious to develop. If the front desk just needs to print a wristband that says "John Doe", forcing PostgreSQL to continually concatenatefirstname || ' ' || lastname is a waste of processing power.
The Lesson: Normalize to protect data integrity, but do not normalize past the point of practical business utility.
7. Mini Project: Auditing the E-Commerce Store
How do we track if an Admin maliciously changes the price of a product? We use the Triggers we learned in Chapter 22!
sql
8. Common Mistakes
-
Using Cascading Deletes on Critical Data: Notice in the E-Commerce schema,
userid INT REFERENCES users(userid) ON DELETE RESTRICT. WhyRESTRICTinstead ofCASCADE? If an admin accidentally deletes John's account,CASCADEwould instantly delete all of John's financial orders, destroying the company's accounting records! Financial data should *never* cascade. UseRESTRICTto aggressively block the deletion.
9. Best Practices
-
Draw it First: Never start typing
CREATE TABLEuntil you have physically drawn an ERD (Entity Relationship Diagram) on a whiteboard or piece of paper. Map out the 1:N and N:M links visually. If the drawing is confusing, the code will be a disaster.
10. Exercises
-
1.
In the E-Commerce schema, why is there a
priceatpurchasecolumn inside theorderitemspivot table?
-
2.
Why does the Hospital schema utilize
UUIDfor patient IDs instead of standard integers?
11. SQL Challenges
In the Multi-Tenant SaaS schema, write aSELECT query that joins all 3 tables to fetch the companyname, the email of the user, and the name of the project, but strictly ONLY for org_id = 1.
sql
12. MCQ Quiz with Answers
Question 1
In an E-Commerce architecture, why is it considered a fatal architectural flaw to omit a historical price column (e.g., priceatpurchase) in the pivot table linking Orders to Products?
Question 2
When architecting a multi-tenant SaaS application (where multiple companies use the same database), what is the most critical architectural requirement to ensure data isolation?
13. Interview Questions
-
Q: Describe a business scenario where you would explicitly choose
ON DELETE RESTRICToverON DELETE CASCADEwhen defining a Foreign Key.
- Q: Walk me through the database schema you would design for a ride-sharing application (like Uber) involving Riders, Drivers, and Trips.