Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

In-class exercise

Has_many_through
Solution

ER model: Primary Key (PK)


• Primary key (PK)
• Every entity must have at least one attribute that can be used to uniquely
identify one and only one instance of each entity, which is known as the
entity’s unique identifier
• A PK is usually a single attribute
• One instance of Student entity (e.g., a student John Smith) is uniquely identified by his or
her own student id
• Composite PK
• Combines two or more attributes
• Note)) PK should be minimal for DBMS performance, usually we want to have at most
two attributes as a composite PK
• The combination of multiple attributes uniquely identify each occurrence of an
entity
ER model: Foreign Key (FK)
• When do we define a Foreign key (FK)?
• We define FK, as we translate ER diagram to Relational tables

• Foreign Key in one table points to a Primary key in another table


• FK is a column in one table that contains the primary key values from another
table (one side where we put cardinality of 1..1)
• A foreign key is a field (or collection of fields) in one table that uniquely
identifies a row of another table
• A foreign key serves as a reference to the Primary key (PK) of another
table
• Thus, FK defines the relation between tables

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

Why do we define its own id for a junction table?


• We create junction table’s own id to uniquely identify each row within it.
• person_id is a FK to uniquely identify one row in the Person table
• country_code is a FK to uniquely identify one row in the Country table

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

Person Citizenship Country


id name id start_date expiration_date status person_id country_id id name

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.

Apple sauce Apple cake


Dish-Ingredient
• Dish-Ingredient: Many-to-Many
ER Diagram Dish
needs 0..*
Ingredient
id 0..* id
name name
is_used_in

Data presentation
id name id name
1 apple sauce 1 potato
2 apple cake 2 apple
3 potato bread … …
… …

Can we uniquely identify one row with a pair of PK?


• A combination of dish_id & ingredient_id as a PK? Can’t function as a PK
• It’s possible that the same dish can use the same ingredient multiple times
• A combination of dish_id, ingredient_id, amount as a PK? PK should be minimal
We create junction table’s own id to uniquely identify each row within it.
dish_id is a FK to uniquely identify one row in Dish table
ingredient_id is a FK to uniquely identify one row in Ingredient table

dish_id ingredient_id step_no amount Instruction


1 2 1 1 Using an apple corer, core the apple and cut into the
eighths.
1 2 4 1 Place the apples in the basket of a food mill
3 1 1 1 Peel and dice
3 1 2 1 Place potato peeled and diced in a sauce pan
3 1 3 1 Mash potato, and set aside
From ER to Relational tables: Many-to-Many (2)—its own id & FKs
• Recipe as an entity with its own id
ER Diagram
Recipe
Dish 0..* id 0..* Ingredient
id 1..1 step_no 1..1 id
name amount name
instruction

Relational vocab. Dish : has_many Ingredient :through Recipe


Ingredient: has_many Dish :through Recipe
Recipe :belongs_to Dish
Recipe :belongs_to Ingredient
Relational tables

Dish Recipe Ingredient


id name id step_no amount instruction dish_id ingredient_id id name

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 … …
… …

Can we uniquely identify one row with a pair of PK?


• A combination of person_id & university_id as a PK? Can’t function as a PK
• It’s possible that a student can get multiple degrees from the same university over
time
• A combination of person_id, university_id, graduation date as a PK? Can’t function as a PK
• It’s possible that a student can get two degrees from the same university at the same time thanks to BS &
MS integration program
• A combination of student_id, university_id, grad_date & degree? PK should be minimal

person_id university_id grad_date degree


1 1 05-20-2010 Bachelor of Science
1 1 05-20-2010 Master of Science
1 1 05-20-2016 Doctor of Philosophy
2 1 05-20-2010 Master of Science
2 1 05-20-2016 Doctor of Philosophy

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

• Graduation as an entity with its own id


ER Diagram
Graduation
Person 0..* id 0..* University
id 1..1 grad_date 1..1 id
name degree name

Relational vocab. Person : has_many University :through Graduation


University: has_many Person :through Graduation
Graduation :belongs_to Person
Graduation :belongs_to University

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 … …
… …

Can we uniquely identify one row with a pair of PK?


• A combination of person_id & magazine_id as a PK? Can’t function as a PK
• It’s possible that a person has had subscriptions for the same magazine over time
• A combination of person_id, magazine_id, start_date (or end_date) as a PK? PK should be minimal
• We want to have a composite PK which is made of at most two keys
• In addition, data type of start_date (or end_date) is not integer but date. The use of integer data type for a
PK makes DBMS work much faster.

person_id magazine_id start_date end_date


1 1 09-01-2013 12-31-2013
1 1 03-08-2015 09-08-2015
1 2 07-02-2015 12-31-2015
2 1 08-01-2013 12-31-2013
2 1 01-08-2015 07-08-2015

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

Person 0..* Subscription 0..* Magazine


1..1 id 1..1
id id
name start_date name
end_date
Person : has_many Magazine :through Subscription
Relational vocab. Magazine: has_many Person :through Subscription
Subscription :belongs_to Person
Subscription :belongs_to Magazine
Relational tables
Person Subscription Magazine
id name id start_date end_date person_id magazine_id id name

5. Sporting Event, Competitors, Results


• Competitor – Olympic_event: Many-to-Many
• In this example, we think of the event a player participates (rather than a
Team):
• Michael Phelphs participated in Men’s 100 m butterfly’s semifinal & final
• Michael Phelphs participated in Men’s 200 m butterfly’s semifinal & final

• Men’s 100 m butterfly had many competitors


• Men’s 200 m butterfly had many competitors
Sporting Event, Competitors, Results
• Competitor – Olympic_event: Many-to-Many
ER Diagram Competitor participate_in 0..* Olympic_event
id id
name 0..*
has name

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 … …
… …

Can we uniquely identify one row with a pair of PKs?


• A combination of person_id & event_id as a PK? Can’t function as a PK
• It’s possible that a person has had memberships for the same club over time
• A combination of person_id, event_id & rank as a PK? Can’t function as a PK & PK should be minimal
• A combination of person_id, event_id, event_category as a PK? PK should be minimal
• We want to have a composite PK which is made of at most two keys
• In addition, data type of event_category is not integer but string. The use of integer makes DBMS work
much faster.

person_id event_id event_category time_sec rank


1 1 Semifinal1 51.58 2
1 1 final 51.14 2
2 1 Semifinal1 50.52 1
2 1 final 50.39 1
3 2 Semifinal2 51.71 5

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 … … …
… …

Can we uniquely identify one row with a pair of PKs?

• A combination of person_id & club_id as a PK? Can’t function as a PK


• It’s possible that a person has had memberships for the same club over time
• A combination of student_id, club_id, start_date (or end_date) as a PK? PK should be minimal
• We want to have a composite PK which is made of at most two keys
• In addition, data type of start_date (or end_date) is not integer but date. The use of integer makes DBMS
work much faster.

person_id club_id start_date end_date


1 1 09-01-2012 05-30-2013
1 2 09-01-2013 05-30-2014
1 1 09-01-2014 05-30-2015
2 2 09-01-2012 05-30-2013
2 2 09-01-2014 05-30-2015

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

Relational vocab. Person : has_many Club :through Membership


Club :has_many Person :through Membership
Membership :belongs_to Person
Membership :belongs_to Club
Relational tables
Person Membership Club
id name id start_date end_date person_id club_id id name date_founded

You might also like