Skip to content
Learn Netverks

Lesson

Step 14/36 39% through track

foreign-keys

Foreign keys

Last reviewed May 28, 2026 Content v20260528
Track mode
sql_sandbox
Means
SQL sandbox
Reading
~2 min
Level
intermediate

This lesson

This lesson teaches Foreign keys: the SQL patterns, schema habits, and query reasoning you need before advancing in SQL.

Keys enforce referential integrity—ORMs hide them until migrations fail in production.

You will apply Foreign keys in contexts like: Postgres, MySQL, SQLite, warehouses, and ORMs that still expose SQL.

Copy SQL from each lesson into SQLite (sqlite3), DB Fiddle, or local Postgres—read result grids and row counts. The in-browser SQL lab (sql_sandbox) will run queries when the runner ships; until then, local clients are the practice path.

When you can explain the previous lesson's ideas without copying example queries verbatim.

A foreign key declares that values in one table must reference existing rows in another—enforcing referential integrity between customers and orders, posts and comments, etc.

Declaring FK

CREATE TABLE orders (
  id INTEGER PRIMARY KEY,
  customer_id INTEGER NOT NULL,
  total REAL,
  FOREIGN KEY (customer_id) REFERENCES customers(id)
);

Practice: Run DDL in a fresh SQLite file or DB Fiddle schema pane. Drop test tables when experimenting.

Referential actions

CREATE TABLE order_items (
  id INTEGER PRIMARY KEY,
  order_id INTEGER NOT NULL,
  product_id INTEGER NOT NULL,
  FOREIGN KEY (order_id) REFERENCES orders(id) ON DELETE CASCADE,
  FOREIGN KEY (product_id) REFERENCES products(id) ON DELETE RESTRICT
);

ON DELETE CASCADE removes child rows; RESTRICT blocks delete if children exist; SET NULL is another option.

JOIN preview

SELECT c.name, o.id AS order_id, o.total
FROM customers c
JOIN orders o ON o.customer_id = c.id;

Foreign keys model relationships; JOINs query across them—covered next in the queries module.

Important interview questions and answers

  1. Q: FK without constraint?
    A: Application code can enforce integrity, but race conditions make DB constraints safer.
  2. Q: ON DELETE CASCADE risk?
    A: Accidental parent delete removes all children—understand cascade graphs before enabling.

Self-check

  1. What error occurs inserting order with nonexistent customer_id (when FK enforced)?
  2. Name two ON DELETE actions.

Tip: Draw FK arrows on ER diagrams before writing JOIN queries—it prevents backwards joins.

Interview prep

FK purpose?

Ensures referenced parent row exists—referential integrity.

ON DELETE CASCADE?

Deleting parent removes dependent child rows automatically.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • ON DELETE CASCADE?
  • Orphan rows?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump