2. Designing Tables & Combining Data

Designing Tables & Foreign Keys

Create clean tables with constraints (PRIMARY KEY, NOT NULL, UNIQUE, DEFAULT) and link tables together safely using Foreign Keys.

3 min readUpdated 2026-11-29
In This Lesson
  • Create tables with NOT NULL, UNIQUE, and DEFAULT guardrails
  • Connect related tables safely using Foreign Keys
  • Model one-to-many and many-to-many relationships cleanly

Why don't software engineers dump students, courses, and enrollments into one giant spreadsheet-style table?

Suppose 500 students enroll in "Python Fundamentals". If you repeat the course title, description, and price inside every student row, changing the course title means editing 500 rows—and even one missed row leaves half your database disagreeing with the other half.

Instead, relational databases split each real-world concept into its own focused table (students, courses, enrollments) and wire them together safely using constraints and Foreign Keys.

Building Tables with Built-In Guardrails

When you create a table with CREATE TABLE, you specify each column's name, data type, and constraints—rules the database strictly enforces so messy or incomplete data can never slip in:

sql
CREATE TABLE students (
  id INTEGER PRIMARY KEY,
  full_name TEXT NOT NULL,
  email TEXT NOT NULL UNIQUE,
  created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE courses (
  id INTEGER PRIMARY KEY,
  slug TEXT NOT NULL UNIQUE,
  title TEXT NOT NULL,
  level TEXT NOT NULL DEFAULT 'Beginner'
);

Each constraint acts like an automatic quality inspector:

  • PRIMARY KEY gives every row a guaranteed unique ID badge (1, 2, 3, ...).
  • NOT NULL refuses any insert that leaves a required field blank.
  • UNIQUE guarantees no two students accidentally register with the exact same email, and no two courses share a URL slug.
  • DEFAULT automatically fills in a sensible fallback ('Beginner' or CURRENT_TIMESTAMP) when an insert omits that column.

Linking Tables Safely with FOREIGN KEY

With students and courses defined separately, how do we record which student enrolled in which course? We create a third junction table named enrollments holding IDs that point to both tables:

sql
CREATE TABLE enrollments (
  id INTEGER PRIMARY KEY,
  student_id INTEGER NOT NULL,
  course_id INTEGER NOT NULL,
  enrolled_on TEXT NOT NULL DEFAULT CURRENT_DATE,
  FOREIGN KEY (student_id) REFERENCES students(id),
  FOREIGN KEY (course_id) REFERENCES courses(id)
);

Because student_id is declared as a FOREIGN KEY, the database actively protects referential integrity: if your application accidentally tries to insert an enrollment for student_id = 9999 (a student who doesn't exist), SQL blocks it immediately instead of leaving orphan data behind.

Try Creating & Querying Linked Tables

Run the live SQL playground below to create students, courses, and enrollments, insert sample rows, and query the linked enrollment data:

SQL (SQLite)Live SQL — Linked Tables with Foreign Keys
Output

Click “Run Code” to execute this snippet right in your browser.

Three Relationship Patterns You Will See Everywhere
  1. One-to-One: One user ↔ one profile settings row.
  2. One-to-Many: One customer → many orders (store customer_id on orders).
  3. Many-to-Many: Many students ↔ many courses (use a middle junction table like enrollments!).

Quick Checkpoints

Check Your Understanding

How do you model a Many-to-Many relationship (e.g., Students enrolling in multiple Courses) in a relational database?