Database Engineering · Where data lives
Primary and foreign keys
A primary key gives every row a unique, permanent name. A foreign key stores that name in another table, and the database refuses any change that would leave it pointing at nothing.
- Time13 min
- LevelBeginner
- Pictures12 diagrams
- Check6 questions






App sends a new order for customer_id 7, total 42.00.
Foreign key looks up customer 7 in the customers table.
Found the order is saved and points at a real customer.
Not found the insert fails with a foreign key error; nothing is saved.