juniorMySQL
Understanding Foreign Keys
Updated May 4, 2026
Short answer
A constraint that links two tables and enforces referential integrity.
Deep explanation
Ensures values in a child table match the parent table's Primary Key. Prevents 'orphaned' records by restricting or cascading deletes.
Real-world example
Linking an 'orders' table to a 'users' table so every order belongs to a valid user.
Common mistakes
- Trying to insert a child record before the parent record exists.
Follow-up questions
- What does ON DELETE CASCADE do?