SQL · Module 07: Schema Design, Normalization & ViewsTutorial 21 of 24
All Tutorials·~1.5 hrs

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.

01 Intuition: Plain-English mental model
02 Visuals: Step-by-step diagrams
03 Practice: Clean examples & tradeoffs

1. The 3 Relationship Patterns (How Every App Is Modeled)

RelationshipReal-World ExampleHow to Model in SQL
One-to-One (1 : 1)users ↔ user_profilesPut user_id as both PK & FK (or UNIQUE FK) on user_profiles
One-to-Many (1 : N)users → ordersPut user_id FK on the "Many" side (orders) — never on users!
Many-to-Many (M : N)students ↔ coursesCreate 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 StyleExampleVerdict
Natural Keyemail VARCHAR(255) PRIMARY KEY❌ Avoid as PK! Users change emails, and repeating a 40-byte string in every FK table bloats all indexes!
Surrogate Keyuser_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:

SQL
-- "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 normal UNIQUE (email) constraint blocks them! Use a Partial Unique Index:

SQL
CREATE UNIQUE INDEX idx_users_active_email
ON users (email)
WHERE deleted_at IS NULL;

3. Complete Real-World Schema: Mini Learning Platform (Like BigOSlate!)

SQL
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

Q1.In a One-to-Many (1:N) relationship between departments (1) and employees (Many), which table holds the Foreign Key?
Q2.How do you model a Many-to-Many (M:N) relationship between students and courses in SQL?

Keep Exploring: Related Lessons & Tools

All Courses →