Foreign key

A constraint that says a column must match a primary key in another table. InnoDB checks it on every insert, update and delete, and automatically creates an index on the referencing column if one does not exist.

Minimal example

sql
FOREIGN KEY (restaurant_id) REFERENCES restaurants(id)

Learn this concept

Lesson 2.1 · MySQL tables, keys and InnoDB indexes

Used in exercises

Exercise 2.3 · Write the restaurant and menu migrations Exercise 2.7 · Seed 50 restaurants and one full menu Exercise 3.6 · Migration 0006: order tables
Prerequisite conceptsPrimary key