Foreign Key
Question
What is a foreign key?
Hint
Foreign keys enforce referential integrity.
Explanation
A foreign key is a column (or set of columns) that references the primary key of another table, establishing a relationship. Example: orders.customer_id REFERENCES customers.id. Foreign keys enforce referential integrity — you cannot insert an order with a non-existent customer_id, and deleting a customer with orders is prevented (or cascaded). Foreign keys can be NULL (optional relationships). ON DELETE CASCADE/SET NULL defines behavior when the referenced row is deleted. Foreign keys create implicit indexes in most databases.
Related Resources
Related Lessons
- Relationships
Learn relationships.
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.