Foreign Key Constraint

SQL · Working with Tables

Theory

A FOREIGN KEY ties a column in a child table to the primary key (or a unique key) of a parent table. orders.customer_id REFERENCES customers(id) means every order must belong to a customer that exists. The database checks this on every insert and update of the child, and on every delete or key update in the parent; this guarantee is called referential integrity.

A NULL foreign key is allowed unless the column is also NOT NULL; it simply means "no parent yet". You choose what happens when a parent row is deleted: the default (NO ACTION / RESTRICT) blocks the delete, ON DELETE CASCADE deletes the child rows too, and ON DELETE SET NULL clears the reference. Indexing the foreign key column speeds up joins and parent deletes. In SQLite, foreign keys are enforced only after PRAGMA foreign_keys = ON.

Why we use it

Without foreign keys, orders can point to customers that never existed or were deleted, and joins silently lose data. The constraint keeps relationships between tables valid at all times.

When to use it

Use foreign keys between related tables in transactional databases. Pick CASCADE only when children have no meaning without the parent. Many analytics warehouses accept the syntax but do not enforce it, so check data with joins there.

How it works

  1. INSERT or UPDATE a child row (orders)
  2. Is customer_id NULL? Yes: Allowed (unless NOT NULL). No: Look up the parent.
  3. Does customers have a row with that id? Yes: Accept the row. No: Reject: no parent key.
  4. On parent delete: block, CASCADE or SET NULL
  5. Relationships stay valid

Syntax

FOREIGN KEY (column) REFERENCES other_table(column)

Example

CREATE TABLE orders (
  id INT PRIMARY KEY,
  customer_id INT,
  FOREIGN KEY (customer_id) REFERENCES customers(id)
);

Key points

SQL topics

Introduction to SQL

Querying Data

Sorting Rows

Limiting Rows

Filtering Rows

Joining Multiple Tables

Grouping Rows

SET Operators

Working with Tables

Modifying Data

Views & Triggers

Conditional Expressions

SQL Subqueries

Aggregate Functions

Window Functions

String Functions

Date Functions

Math Functions

Extra Concepts

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Master SQL from Zero to Pro

All SQL topics in one place. Click any topic in the sidebar — content opens right here without leaving the page.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice SQL Online Solve 150 SQL interview problems in a live editor with instant checking. Start practising →
Section {{ sec.num }}

{{ sec.label }}

SQL  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs against a real sample database
{{ liveError }}
{{ col }}
{{ cell }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic