Database Design Strategies | Real-World SQL Architecture
# CHAPTER 29
Real-World Database Design Projects
1. Introduction
You have learned the syntax of SQL. You know how toJOIN tables, create FOREIGN KEYS, and build VIEWS. But typing queries is only half of a Database Engineer's job. The other half is Architecture—the ability to look at a complex business requirement, grab a whiteboard marker, and translate that requirement into a perfectly normalized, flawless relational schema. In this chapter, we will walk through the blueprints 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 (Products, Carts, Orders).
- Architect a Social Media Platform (Users, Posts, Comments, Follows).
- Architect a Multi-Tenant SaaS application.
- Apply the Extended Reference Pattern (Snapshots).
- Recognize practical scenarios for denormalization.
3. Project 1: The E-Commerce Platform
A basic E-Commerce store needs Users, Products, Shopping Carts, and Orders. The Architectural Challenge: When an order is finalized, we must permanently freeze the price of the item. If a user buys a Laptop for $1000 today, and the live price changes to $1200 tomorrow, their historical receipt MUST still say $1000.The Schema:
-
1.
users:id,name,email
-
2.
products:id,name,liveprice,stock
-
3.
orders:id,userid(FK),purchasedate,status
-
4.
orderitems(The Pivot/Snapshot Table):id,orderid(FK),productid(FK),quantity,priceatcheckout
*(CRITICAL: The priceatcheckout column in the Pivot table creates a permanent historical snapshot of the price, protecting the accounting data from future product price updates!).*
4. Project 2: Social Media Platform (Twitter/X Clone)
A social media site has Users, Posts, Comments, and a Following system. The Architectural Challenge: How do you represent a user following another user? They are both in the exact same table! You must use a Self-Referencing Many-to-Many Pivot Table.The Schema:
-
1.
users:id,handle,joindate
-
2.
posts:id,authorid(FK to users),content,createdat
-
3.
comments:id,postid(FK),userid(FK),content
-
4.
follows(The Self-Referencing Bridge):followerid(FK to users),followedid(FK to users)
*(To find out who Alice is following, you query the follows table where followerid = Alice's ID and JOIN it back to the users table!).*
5. Project 3: The Multi-Tenant SaaS App (e.g., Slack or Trello)
A SaaS application has many "Organizations" (Companies), and each Organization has many Users and Projects. The Architectural Challenge: You must strictly ensure that User A from Company 1 can never view the data of Company 2. We use atenantid (Organization ID) on almost every table to enforce absolute data isolation.
The Schema:
-
1.
organizations(The Tenants):id,companyname,subscriptiontier
-
2.
users:id,orgid(FK),email,roleid
-
3.
projects:id,orgid(FK),projectname
-
4.
tasks:id,projectid(FK),orgid(FK),title,status
*(By physically placing the orgid on the tasks table, backend engineers can effortlessly append WHERE orgid = ? to every single query, guaranteeing absolute data isolation).*
6. The Danger of Over-Normalization
Looking at the E-Commerce schema, an academic SQL architect might say: *"A User has a First Name and Last Name. They should be stored in a separatenames table!"*
While academically correct (1st Normal Form), it makes the database agonizingly slow. If the front desk just needs to print a shipping label that says "John Doe", forcing SQL to run a JOIN just to get a last name is a massive waste of processing power.
The Lesson: Normalize to protect data integrity, but do not normalize past the point of practical business utility.
7. Auditing with Triggers (Real-World Utility)
In the financial systems of the E-commerce project, the DBA would immediately implement an Audit Trigger (Chapter 25). If any employee executes anUPDATE on the orders table to change a status to 'Refunded', a trigger must automatically insert a record into an orderauditlogs table tracking the exact timestamp and the ID of the employee who ran the query.
8. Common Mistakes
-
Missing Foreign Keys: Beginners will build the E-Commerce schema, name a column
productid, but completely forget to write the DDL commandFOREIGN KEY (productid) REFERENCES products(id). Without the explicit constraint, the database cannot enforce Referential Integrity, and orphaned records will destroy the application logic.
9. Best Practices
-
Draw it First: Never start typing
CREATE TABLEuntil you have physically drawn an Entity Relationship Diagram (ERD) on a whiteboard or piece of paper. Map out the 1:N and N:M links visually. Identify where the Foreign Keys must live.
10. Exercises
-
1.
In the E-Commerce schema, why is it structurally critical to store
priceatcheckoutin theorderitemspivot table rather than just referencing theproductstable via a JOIN?
- 2. How do you architect a "Follower" system where Users follow other Users?
11. SQL Challenges
In the Multi-Tenant SaaS schema, write aSELECT query that joins the users table and the organizations table. Return the user's email and the company_name. Filter the query to ONLY show users who belong to the organization with ID #5.
12. MCQ Quiz with Answers
When architecting a multi-tenant SaaS application (where multiple companies share the exact same database cluster), what is the most critical architectural requirement to ensure data security and isolation?
In a Social Media database schema, what architectural pattern is used to map a relationship where a User "follows" another User?
13. Interview Questions
- Q: Explain the concept of the "Historical Price Snapshot" in E-commerce database design. Why does a pure, fully normalized 3rd Normal Form architecture fail to handle historical financial receipts?
- Q: Walk me through the database schema you would design for a ride-sharing application (like Uber) involving Riders, Drivers, and Trips. Identify the exact locations of the Foreign Keys.