Why this lesson matters

Understand relationship types 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 Relationship Types, the outcome to verify is: Run the statement against sample data and inspect the result before moving to the next case.
  • In Relationship Types, keep this failure controlled: Storing related names or labels instead of keys creates duplicates and makes updates unable to preserve referential integrity.
  • Relationship Types practice target: Model customer, order and product relationships with keys and identify one-to-many and many-to-many cardinality.
Diagram

Relationship types

Practical walkthrough

In the Relationship Types walkthrough: Run the statement against sample data and inspect the result before moving to the next case.

example.sqlsql
CREATE TABLE order_items (
  order_id BIGINT REFERENCES orders(id),
  product_id BIGINT REFERENCES products(id),
  PRIMARY KEY (order_id, product_id)
);

Practice it yourself

Practice

Relationship Types 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

Summary