One-to-Many and Many-to-Many
Tables relate in three ways:
- One-to-one: each person has one passport. Rare; often the two tables could be merged.
- One-to-many: one customer places many orders. The "many" table holds the foreign key. This is the commonest relationship.
- Many-to-many: a student takes many courses and a course has many students. No single column can hold this, so you add a junction table (also called a link table) with two foreign keys, one for each side.
Examplepython
Quick check
How do you model a many-to-many relationship?
Your turn: write the code
Using the junction table, print how many courses Ama (student 1) takes.
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.