beginner
Foreign Key Constraint
5 min readLast updated: 2026-07-23
Overview
A Foreign Key maintains referential integrity between two tables by linking a column to a Primary Key in another table.
Learning Objectives
- Link relational tables using
FOREIGN KEY ... REFERENCES. - Prevent orphan records and enforce cascading actions (
ON DELETE CASCADE).
Code Examples
SQL
sql
CREATE TABLE orders (
order_id INT PRIMARY KEY,
customer_id INT REFERENCES customers(customer_id) ON DELETE CASCADE
);
Summary
Foreign Keys enforce referential integrity between parent and child tables.