7.3 Real-World Database Schema Design: 1:1, 1:N & M:N Modeling
Put everything together to design production-grade schemas from scratch: modeling 1:1, 1:N, and M:N relationships, Surrogate vs Natural keys, Soft Deletes (deleted_at), and a complete LMS schema.
1. The 3 Relationship Patterns (How Every App Is Modeled)
| Relationship | Real-World Example | How to Model in SQL |
|---|---|---|
One-to-One (1 : 1) | users ↔ user_profiles | Put user_id as both PK & FK (or UNIQUE FK) on user_profiles |
One-to-Many (1 : N) | users → orders | Put user_id FK on the "Many" side (orders) — never on users! |
Many-to-Many (M : N) | students ↔ courses | Create a Junction / Bridge Table enrollments(student_id, course_id) with a Composite PK! |
2. 3 Real-World Production Design Decisions
Decision 1: Surrogate Key (user_id BIGINT) vs Natural Key (email)
| Key Style | Example | Verdict |
|---|---|---|
| Natural Key | email VARCHAR(255) PRIMARY KEY | ❌ Avoid as PK! Users change emails, and repeating a 40-byte string in every FK table bloats all indexes! |
| Surrogate Key | user_id BIGINT PRIMARY KEY (or UUID v7) | ✅ Best practice! Immutable, tiny (8 bytes), fast joins; keep email UNIQUE NOT NULL separately! |
Decision 2: Soft Delete (deleted_at TIMESTAMP) vs Hard Delete (DELETE)
In production SaaS and e-commerce, hard-deleting a user breaks historical invoices and analytics. Instead, add a nullable deleted_at TIMESTAMP:
-- "Soft delete" a user (preserves foreign keys & allows undo!)
UPDATE users
SET deleted_at = CURRENT_TIMESTAMP
WHERE user_id = 42;
-- Active users query:
SELECT user_id, name, email
FROM users
WHERE deleted_at IS NULL;
Pro PostgreSQL Trick (Partial Unique Index with Soft Delete):
If a soft-deleted user re-registers with the same email, a normalUNIQUE (email)constraint blocks them! Use a Partial Unique Index:SQLCREATE UNIQUE INDEX idx_users_active_email ON users (email) WHERE deleted_at IS NULL;
3. Complete Real-World Schema: Mini Learning Platform (Like BigOSlate!)
CREATE TABLE users (
user_id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
name VARCHAR(100) NOT NULL,
email VARCHAR(255) NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
deleted_at TIMESTAMP NULL
);
CREATE TABLE courses (
course_id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
slug VARCHAR(120) NOT NULL UNIQUE,
title VARCHAR(200) NOT NULL,
price_inr DECIMAL(10, 2) NOT NULL DEFAULT 0 CHECK (price_inr >= 0),
is_published BOOLEAN NOT NULL DEFAULT FALSE
);
-- Many-to-Many Junction Table with progress tracking
CREATE TABLE enrollments (
user_id BIGINT NOT NULL REFERENCES users(user_id) ON DELETE RESTRICT,
course_id BIGINT NOT NULL REFERENCES courses(course_id) ON DELETE RESTRICT,
progress_pct SMALLINT NOT NULL DEFAULT 0 CHECK (progress_pct BETWEEN 0 AND 100),
enrolled_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
completed_at TIMESTAMP NULL,
PRIMARY KEY (user_id, course_id)
);
4. Quick Check Your Understanding
Module 7.3 Quick Quiz
Keep Exploring: Related Lessons & Tools
All Courses →Convert Unix epoch seconds/milliseconds to ISO-8601 and local timezones, and bulk-generate UUID v4, time-ordered UUID v7, and sortable ULIDs.
Paste any Python, JS, Java, or C++ snippet to estimate Time & Space complexity, or enter input size N to see which algorithms pass in 1s.