Why this lesson matters
Understand foreign keys and reason about it with a concrete relational example. Primary keys give rows stable identity, foreign keys protect references between tables, and relationship cardinality determines where those keys belong.
How to reason about it
- For Foreign Keys, the outcome to verify is: Run the statement against sample data and inspect the result before moving to the next case.
- In Foreign Keys, keep this failure controlled: Storing related names or labels instead of keys creates duplicates and makes updates unable to preserve referential integrity.
- Foreign Keys practice target: Model customer, order and product relationships with keys and identify one-to-many and many-to-many cardinality.
Foreign-key relation
customers.id
IDrelation_key1 — Norders.customer_id
IDrelation_key1 — NFK Constraint
IDrelation_keyPractical walkthrough
In the Foreign Keys walkthrough: Run the statement against sample data and inspect the result before moving to the next case.
example.sqlsql
CREATE TABLE orders (
id BIGINT PRIMARY KEY,
customer_id BIGINT NOT NULL REFERENCES customers(id)
);Practice it yourself
Foreign Keys exercise
Model customer, order and product relationships with keys and identify one-to-many and many-to-many cardinality.
- Record the expected result before execution
- Test one valid path and one lesson-specific failure path
- Explain in two lines which boundary owns the decision
