Foreign Keys and Relationships
Real data is connected: an order belongs to a customer, a book to an author. Instead of copying the customer's details into every order, store the customer once and let each order point to it with the customer's ID. That pointer column is a foreign key.
The database can enforce it: with foreign keys switched on (PRAGMA foreign_keys = ON in SQLite) it refuses an order for a customer that does not exist. This is referential integrity, and it stops orphan records.
What does a foreign key do?
With foreign keys on, try to add a pet for owner 7 who does not exist and print refused if the database rejects it.
Finish the quick check (and any code exercise) to complete this lesson, or mark it done yourself.
Tip: use the left and right arrow keys to move between lessons.