Relational modeling: keys and relationships
Build a Node HTTP API from scratch, model data in SQL and MongoDB, and reason about transactions, indexes, caching and authentication so the endpoint you ship stays correct under load.
What you will be able to do
#- Design tables with primary keys and foreign keys that enforce referential integrity.
- Model one-to-many and many-to-many relationships.
- Choose a natural or surrogate key deliberately.
Overview
#A relational database stores data in tables of rows and columns. Relationships are expressed with keys, and the database enforces them so invalid references cannot exist.
Keys
- A primary key uniquely identifies a row. A surrogate integer or UUID is common.
- A foreign key references a primary key in another table and enforces referential integrity.
- A unique constraint enforces a natural key such as an email without making it the primary key.
One-to-many
CREATE TABLE authors (
id BIGSERIAL PRIMARY KEY,
name TEXT NOT NULL
);
CREATE TABLE articles (
id BIGSERIAL PRIMARY KEY,
author_id BIGINT NOT NULL REFERENCES authors(id),
title TEXT NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
Many-to-many
A junction table holds two foreign keys and a composite primary key.
CREATE TABLE article_tags (
article_id BIGINT NOT NULL REFERENCES articles(id) ON DELETE CASCADE,
tag_id BIGINT NOT NULL REFERENCES tags(id) ON DELETE CASCADE,
PRIMARY KEY (article_id, tag_id)
);
Common pitfalls
- Storing a comma-separated list of ids in one column instead of a junction table.
- Using a natural key such as an email as the primary key, then having to change it when a user updates the email.
- Omitting
ON DELETEbehavior and blocking legitimate deletes.
Exercises
#Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.
Exercise 1Implement
Write DDL for a many-to-many between students and courses, with cascade delete on the junction.
starter.sql-- students(id, name), courses(id, title), enrollments junctionReveal the reference answer
reference.sqlCREATE TABLE students ( id BIGSERIAL PRIMARY KEY, name TEXT NOT NULL ); CREATE TABLE courses ( id BIGSERIAL PRIMARY KEY, title TEXT NOT NULL ); CREATE TABLE enrollments ( student_id BIGINT NOT NULL REFERENCES students(id) ON DELETE CASCADE, course_id BIGINT NOT NULL REFERENCES courses(id) ON DELETE CASCADE, enrolled_at TIMESTAMPTZ NOT NULL DEFAULT now(), PRIMARY KEY (student_id, course_id) );Authored expected output: Three tables; enrollments has a composite primary key and two foreign keys with ON DELETE CASCADE.
The junction table models the relationship and its composite primary key prevents duplicate enrollments.
Exercise 2Explain
Why is storing tag ids as a comma-separated string in the articles table a modeling mistake?
starter.textarticles.tags = '3,7,12'Reveal the reference answer
reference.textIt breaks first normal form, makes joins and index lookups impossible, prevents the database from enforcing that a tag exists, and makes removing or renaming a tag a string rewrite across rows.Authored expected output: An explanation citing normalization, loss of referential integrity and query difficulty.
A junction table keeps each relationship in its own row so the database can enforce and index it.
Common misconceptions
#- Foreign keys hurt performance and should be dropped: they enforce correctness and the index they need is usually worth it.
- A primary key must be a natural business value: surrogate keys decouple identity from mutable business data.
Follow-up practice
#- backend-databases-sql-select
Your progress
#Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.
Verification: authored. Answers and outputs on this page are authored reference material, not executed results.