Foreign Keys and Referential Integrity

Introduction

A foreign key is a column (or set of columns) in one table whose values must match an existing primary key or unique key value in another table. That simple rule is what turns a collection of separate tables into a connected relational database: an order row can point to the customer who placed it, an enrollment row can point to both a student and a course. Without foreign keys, nothing stops your database from storing an order for a customer who does not exist.

The rule that these references must always point to something real is called referential integrity, and the database enforces it for you through FOREIGN KEY constraints. In this topic you will see how to declare those constraints in CREATE TABLE and ALTER TABLE statements, how NULL values are handled in a foreign key column (a NULL means "no relationship yet," not a broken one), and how referential actions such as ON DELETE CASCADE, ON DELETE SET NULL, and ON UPDATE CASCADE tell the database what to do when a referenced parent row is deleted or changed.

As you watch, keep three questions in mind. First, which table is the parent (the one holding the referenced key) and which is the child (the one holding the foreign key)? Second, what exact SQL syntax creates the constraint, and what error does the database raise when the constraint is violated? Third, what happens to the child rows when a parent row is deleted — and why choosing the right referential action matters more than it first appears. Note that syntax and default behavior can vary slightly between database systems, so compare what you see in the videos with the syntax used in our course environment.

Video 1: What is a Foreign Key Constraint? Understanding Primary & Foreign Keys

CockroachDB · 3:53 · 110,538 views · 5y ago · captions — Watch on YouTube

About this video

A short conceptual introduction to foreign key constraints: what a foreign key is, how it differs from a primary key, and why the constraint matters for keeping related tables consistent. Based on its title and description, it frames foreign keys as references from a child table to a parent table's key and explains why the database refuses data that would break that reference.

Why this video

It directly addresses the "What Is a Foreign Key?" and "Foreign Keys vs. Primary Keys" subtopics in under four minutes, and it comes from CockroachDB, a distributed SQL database vendor whose educational material is aimed at developers learning relational fundamentals.

Video 2: How to Create Tables with Foreign Keys in SQL

CockroachDB · 3:42 · 19,553 views · 5y ago · captions — Watch on YouTube

About this video

A practical follow-up that walks through writing the SQL to create tables containing foreign keys. Expect to see CREATE TABLE statements in which a child table declares a FOREIGN KEY (or REFERENCES) clause pointing at a parent table's key column, with discussion of how this maintains referential integrity.

Why this video

It moves from concept to syntax, covering the "Defining a Foreign Key Constraint in SQL" and "Understanding Referential Integrity" subtopics with hands-on examples you can adapt to your own tables.

Video 3: Sql query to delete from multiple tables

kudvenkat · 9:57 · 188,064 views · 5y ago · captions — Watch on YouTube

About this video

This lesson from the kudvenkat SQL Server series looks at deleting rows that participate in parent-child relationships, including cascade delete behavior and the complication of the same foreign key appearing in multiple child tables. It demonstrates the practical consequences of referential actions when you try to remove a referenced parent row.

Why this video

It gives concrete, demonstrated coverage of the ON DELETE subtopic — the piece students most often misunderstand — from a long-running and widely used SQL tutorial channel; just be aware the demos use SQL Server syntax.

How These Videos Support the Learning Outcomes

Together these videos build from concept to code to consequence. The first clarifies the distinct roles of primary and foreign keys, supporting MO1's requirement that you distinguish key types by their function in a relational design. The second models the actual SQL for declaring a FOREIGN KEY constraint inside CREATE TABLE, which feeds directly into MO2 (you should then practice the equivalent ALTER TABLE form in the course materials). The third demonstrates what happens when a referenced parent row is deleted and how cascade behavior resolves it, giving you the practical grounding needed for MO5's goal of configuring ON DELETE and ON UPDATE actions. Outcomes MO3 and MO4, which concern candidate and surrogate keys, are addressed elsewhere in this module.

About These Videos

These videos were chosen from YouTube on October 2, 2026. Each has captions; the player turns them on. Videos on YouTube can be changed or removed — if one no longer plays, tell your instructor. If a player does not appear, use its Watch on YouTube link.