Professional Documents
Culture Documents
In-Class Exercise: ER Model: Primary Key (PK)
In-Class Exercise: ER Model: Primary Key (PK)
Has_many_through
Solution
In-class exercise
1. Citizenship (Naturalization and Birth)
2. Recipes and Ingredients (handle # of teaspoons)
3. Graduations
4. Magazine Subscriptions
5. Sporting Event, Competitors, Results.
6. Club memberships
Background knowledge about Citizenship
• For instance, Apu in Simpsons:
• Apu came from Rahmatpur, India.
• Apu became a naturalized citizen.
• Manjula became a citizen of USA, as she is
marred to Apu
• In real-world:
• T.S. Eliot was born in the USA in 1888, but
moved to the UK and became a British
Subject in 1927, relinquishing his US
citizenship.
1. Citizenship
• Person – Country: Many-to-Many
ER Diagram Person has status to stay Country
0..*
id 0..*
id
name has name
Data presentation
id name id name
1 Apu Nahasapeemapetilon 1 India
2 Manjula Nahasapeemapetilon 2 United States of America
3 T.S. Eliot … …
… …
# In case that the names used in both ER and Relational tables consist of more than one word,
use underscores rather than spaces (e.g., first_name)
Can we uniquely identify one row with a pair of PK?
• A combination of person_id & country_code as a PK? Can’t function as a PK
• It’s possible that a person can have different status in the same country over time
• Then, the pair of person_id & country_code does NOT uniquely identify each row
in a junction table, which violates the requirement of PK.
• A combination of student_id, class_id, start_date as a PK? PK should be minimal
• We want to define a composite PK that has at most two columns within a table
Data presentation
person_id country_id start_date expiration_date status
1 1 1962 NULL Born citizen
1 2 1990 NULL Naturalized citizen
2 1 1960 NULL Born citizen
2 2 1992 NULL Naturalized citizen
3 2 1912 1927 Born citizen
3 2 1927 NULL Relinquished
Data presentation
id person_id country_id start_date expiration_date status
1 1 1 1955 NULL Born citizen
2 1 2 1980 NULL Naturalized citizen
3 2 1 1960 NULL Born citizen
4 2 2 1982 NULL Naturalized citizen
5 3 2 1912 1927 Born citizen
6 3 2 1927 NULL Relinquish
Citizenship status’ own id is shorter than the concatenated key, which means DBMS can work much faster
From ER to Relational tables: Many-to-Many (2)—its own id & FKs
• Citizenship as an entity with its own id
ER Diagram
Citizenship
Person 0..* id 0..* Country
id 1..1 start_date 1..1 id
name expiration_date name
status
Relational vocab. Person : has_many Country :through Citizenship
Country: has_many Person :through Citizenship
Citizenship :belongs_to Person
Citizenship :belongs_to Country
Relational tables
2. Recipe
• Dish – Ingredient: Many-to-Many
• The same ingredient can be used in multiple recipes
• The same ingredient can be used in multiple steps of the same recipe
• A recipe can also be a "sub-recipe" of multiple other recipes.
Data presentation
id name id name
1 apple sauce 1 potato
2 apple cake 2 apple
3 potato bread … …
… …
3. Graduations
• Person – University: Many-to-Many
• A person can get multiple degrees from the same university (B.S., M.S., and
PhD) over time
• A university educates many people
Graduations
• Person – University: Many-to-Many
ER Diagram Person graduates_from 0..* University
id id
name 0..* educates name
Data presentation
id name id name
1 Alice Heschel 1 University of Texas at Austin
2 Robert Light 2 University of Texas at Dallas
3 Joshua Smith … …
… …
Graduation’s own id is shorter than the concatenated key, which means DBMS can work much faster
From ER to Relational tables: Many-to-Many (2)—its own id & FKs
Relational tables
Person Graduation University
id name id grad_date degree person_id univ_id id name
4. Magazine subscriptions
• Person – Magazine: Many-to-Many
• For instance:
• Alice subscribed Wired magazine twice, and Nature magazine once in the past.
• Wired magazine has been subscribed by thousands of people.
4. Magazine subscriptions
• Person – Magazine: Many-to-Many
ER Diagram Person subscribes
Magazine
0..*
id id
name 0..* is_subscribed name
Data presentation
id name id name
1 Alice Heschel 1 wired
2 Robert Light 2 nature
3 Joshua Smith … …
… …
Subscription’s own id is shorter than the concatenated key, which means DBMS can work much faster
From ER to Relational tables: Many-to-Many (2)—its own id & FKs
• Subscription relationship as an entity
• The Foreign keys (person_id, magazine_id) go into the table but this time the table also
has its own id & additional columns such as start_date and end_date
ER Diagram
Data presentation
id name id name
1 Michael Phelphs 1 Men’s 100 m butterfly
2 Joseph Schooling 2 Men’s 200 m butterfly
3 Alexandra Sadovnikov … …
… …
Membership’s own id is shorter than the concatenated key, which means DBMS can work much faster
From ER to Relational tables: Many-to-Many (2)—its own id & FKs
• Result relationship as an entity with its own id
• The Foreign keys (person_id, event_id) go into the table but this time the table
also has its own id & additional columns such as event_category, time, & rank
ER Diagram
Person 0..* Result 0..* Olympic_event
1..1 id 1..1
id id
name event_category name
time_sec
rank
Relational vocab. Person : has_many Olympic_event :through Result
Olympic_event: has_many Person :through Result
Result :belongs_to Person
Result :belongs_to Olympic_event
Relational tables
Person Result Olympic_event
id name id event_category time_sec rank person_id event_id id name
6. Club membership
• Person – Club: Many-to-Many
• For instance, we can think about student club:
• Alice was a member of Alpha Phi Omega (APO) from September 1st 2012 to May 30th 2013.
• Alice was a member of Creative Writing Club from September 1st 2013 to May 30th 2014.
• Alice was a members of Alpha Phi Omega(APO) from September 1st 2014 to May 30th 2015.
• Alpha Phi Omega (APO) has hundreds of people as members over years.
• Creative Writing Club also has dozens of people as members over years.
Club membership
• Person – Club: Many-to-Many
Club
ER Diagram Person a_member_of 0..*
id
id
0..* name
name has
date_founded
Data presentation
id name id name date_founded
1 Alice Heschel 1 Alpha Phi Omega 1956
2 Robert Light 2 Creative Writing Club 1978
3 Joshua Smith … … …
… …
Membership’s own id is shorter than the concatenated key, which means DBMS can work much faster
From ER to Relational tables: Many-to-Many (2)—its own id & FKs
• Membership relationship as an entity with its own id
• The Foreign keys (person_id, club_id) go into the table but this time the table
also has its own id & additional columns such as date
ER Diagram
Person 0..* Membership 0..* Club
id 1..1 id 1..1
id
name start_date name
end_date date_founded