International University, VNU-HCMC
Lecture 6:
ER –Relational Translation
School of Computer Science and Engineering
Instructor: Nguyen Thi Thuy Loan
nttloan@hcmiu.edu.vn nthithuyloan@gmail.com,
https://nttloan.wordpress.com/
International University, VNU-HCMC
2
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
Acknowledgement
The following slides have been created adapting
the the [GUW] book provided by the materials of
authors Prof Ullman and et al..Jeffrey D.
Other slides are referenced from Northeastern
University and Duke University.
International University, VNU-HCMC
3
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
ER model: review
Design ERD
•Notation
•Entity/ Weak entity
•Attributes
•Relationship
oAttributes on relationships
oMultiplicity
oBinary versus n-ary relationships
oISA relationships
International University, VNU-HCMC
4
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
To d a y s topics
•Understand the real-world domain b ngei
mo ddele
•Spec u tabify it sing a da ase de dsign mo el (e.g.,
E/R)
•Translate specification to the data model of
D (e.g. e ti al)BMS , r la on
•Create DBMS schema
•Reading material: [GUW] Chapter 3
International University, VNU-HCMC
5
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
You designed an ER digram
5
Tr slatan e it to a Relat Dat seional aba
Train (number e, ngineer,type)
Station (name,address,type)
TrainStop (train_number,stati ameon_n ,time)
Example
name
ad essdr
type
type time
number
e inng eerStationsTrains StopsAt
nn
International University, VNU-HCMC
6
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
From E/R Diagrams to Relations
•Entity set -> relation.
–Attributes -> attributes.
•Relationships -> relations whose attributes are only:
–The keys of the connected entity sets.
–Attributes of the relationship itself.
International University, VNU-HCMC
7
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
General steps
•Each entity (regular/weak) in the ER is converted into a
schema.
•For (1,1)-(1,1) relationships. We make one entity’s primary key
another entity’s foreign key and vice versa.
•The same for (1,1)-(1,n) relationships, we don’t establish a
new schema but bring the primary key from the (1,n) entity
into the (1,1) entity to make a foreign key.
•For (1,n)-(1,n) relationships, we establish a new schema
containing the primary keys of the entities that formed it and
its own attributes.
•For multivalued attribute is converted into a new schema.
International University, VNU-HCMC
8
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
i. For each regular/strong entity type, create a
corresponding relation that includes all the
simple attributes (includes simple attributes of
composite relations)
ii. Choose one of the key attributes as primary
§If composite, the simple attributes together
form the primary key.
iii. Any remaining key attributes are kept as
secondary unique keys (these will be useful for
physical tuning with reference to indexing
analysis)
Step 1: Regular Entity Types
International University, VNU-HCMC
9
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
i. For each weak entity type, create a
corresponding relation that includes all the
simple attributes
ii. Add as a foreign key all of the primary key
attribute(s) in the entity corresponding to the
owner entity type
iii. The primary key is the the combination of all
primary key attributes from the owner and the
partial key of the weak entity, if any
Step 2: Weak Entity Types
International University, VNU-HCMC
10
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
Three approaches
–Foreign Key
•Usually appropriate
–Merged Relation
•Possible when both participations are total
–Relationship Relation
•Not discussed
Step 3: Mapping Binary 1- -1to
International University, VNU-HCMC
11
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
i. Choose one relation as S, the other T
§Better if S has total participation (reduces
number of NULL values)
ii. Add to S all the simple the attributes of
relationship
iii. Add as a foreign key key in S the primary
attributes of T
Step 3: Mapping Binary 1- -1to
International University, VNU-HCMC
12
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
i. Choose the the the S relation as type at N-side
of the relationship, other is T
ii. Add as a foreign key to S all of the primary
key attribute(s) of T
Another approach: create a relationship relation
Step 4: Binary 1- -Nto
International University, VNU-HCMC
13
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
i. Create a new relation S(termed: relationship
relation)
–In some ERD dialects, actually drawn in
ii. Add as foreign keys keys the primary of both
relations; their combination forms the primary key
of S
iii. Add any simple attributes of the M : N
relationship to S
Step 5: Binary M- -Nto
International University, VNU-HCMC
14
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
i. Create a new relation S
ii. Add as foreign keys keys the primary of the
corresponding relation
iii. Add the theattribute to S(if composite,
simple attributes);the combination of all
attributes in Sforms the primary key
Step 6: Multivalued Attributes
International University, VNU-HCMC
15
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
A. Multiple relations –subclass and superclass
§Usually works (assumes unique id at parent)
B. Multiple relations –subclass only
§Should only be used for disjoint
C. Single relation with one type attribute
§Only for disjoint, can result in many NULLs
D. Single relation with multiple type attributes
§Better for overlapping, could be disjoint
Step 7: Specialization/Generalization
International University, VNU-HCMC
16
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
Example ERD
International University, VNU-HCMC
17
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
Resulting Relational Schema
International University, VNU-HCMC
18
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
i. For each regular/strong entity type, create a
corresponding relation that includes all the
simple attributes (includes simple attributes of
composite relations)
ii. Choose one of the key attributes as primary
§If composite, the simple attributes together
form the primary key
iii. Any remaining key attributes are kept as
secondary unique keys (these will be useful for
physical tuning w.r.t. indexing analysis)
Step 1: Regular Entity Types
International University, VNU-HCMC
19
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
Example ERD
International University, VNU-HCMC
20
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
Step 1Result
International University, VNU-HCMC
21
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
i. For each weak entity type, create a
corresponding relation that includes all the
simple attributes
ii. Add as a foreign key all of the primary key
attribute(s) in the entity corresponding to the
owner entity type
iii. The primary key is the the combination of all
primary key attributes from the owner and the
partial key of the weak entity, if any
Step 2: Weak Entity Types
International University, VNU-HCMC
22
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
Step 2Result
International University, VNU-HCMC
23
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
Three approaches
–Foreign Key
•Usually appropriate
–Merged Relation
•Possible when both participations are total
–Relationship Relation
•Not discussed
Step 3: Mapping Binary 1- -1to
International University, VNU-HCMC
24
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
i. Choose one relation as S, the other T
§Better if S has total participation (reduces
number of NULL values)
ii. Add to S all the simple the attributes of
relationship
iii. Add as a foreign key key in S the primary
attributes of T
Step 3: Mapping Binary 1- -1to
International University, VNU-HCMC
25
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
Step 2Result
International University, VNU-HCMC
26
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
Step 3Result
International University, VNU-HCMC
27
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
i. Choose the the the S relation as type at N-
side of the relationship, other is T
ii. Add as a foreign key to S all of the
primary key attribute(s) of T
Another approach: create a relationship
relation
Step 4: Binary 1- -Nto
International University, VNU-HCMC
28
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
Step 4Result
International University, VNU-HCMC
29
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
i. Create a new relation S(termed: relationship
relation)
–In some ERD dialects, actually drawn in
ii. Add as foreign keys keys the primary of both
relations; their combination forms the primary
key of S
iii. Add any simple attributes of the M : N
relationship to S
Step 5: Binary M- -Nto
International University, VNU-HCMC
30
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
Step 5Result
International University, VNU-HCMC
31
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
i. Create a new relation S
ii. Add as foreign keys keys the primary of the
corresponding relation
iii. Add the attribute to S(if composite, the simple
attributes);the combination of all attributes in S
forms the primary key
Step 6: Multivalued Attributes
International University, VNU-HCMC
32
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
Step 6Result
International University, VNU-HCMC
33
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
A. Multiple relations –subclass and superclass
§Usually works (assumes unique id at parent)
B. Multiple relations –subclass only
§Should only be used for disjoint
C. Single relation with one type attribute
§Only for disjoint, can result in many NULLs
D. Single relation with multiple type attributes
§Better for overlapping, could be disjoint
Step 7: Specialization/Generalization
International University, VNU-HCMC
34
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
Specialization/Generalization (A)
International University, VNU-HCMC
35
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
Specialization/Generalization (B)
B. Multiple relations –subclass only
Should only be used for disjoint
International University, VNU-HCMC
36
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
Specialization/Generalization (C)
C. Single relation with one type attribute
Only for disjoint, can result in many NULLs
International University, VNU-HCMC
37
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
Specialization/Generalization (D)
D. Single relation with multiple type attributes
Better for overlapping, could be disjoint
International University, VNU-HCMC
38
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
Subclasses: Three Approaches
1. Object-oriented: One relation per subset of
subclasses, with all relevant attributes.
2. Use nulls: One relation; entities have NULL in
attributes that don’t belong to them.
3. E/R style: One relation for each subclass:
–Key attribute(s).
–Attributes of that subclass.
International University, VNU-HCMC
39
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
Example: Subclass -> Relations
Beers
Ales
isa
name manf
color
International University, VNU-HCMC
40
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
Object Oriented-
Good for queries like “find the color of ales made by Pete’s.”
Name Manf
Bud Anheuser -Busch
Beers
Name Manf Color
Summerbrew Pete’s Drak
Ales
Beers
Ales
isa
name manf
color

Preview text:

International University, VNU-HCMC
School of Computer Science and Engineering Lecture 6: ER –Relational Translation
Instructor: Nguyen Thi Thuy Loan
nttloan@hcmiu.edu.vn nthithuyloan@ , gmail.com https://nttloan.wordpress.com/
International University, VNU-HCMC Acknowledgement
The following slides have been created adapting
the materials of the [GUW] book provided by the
authors Prof.Jeffrey D. Ullman and et al.
Other slides are referenced from Northeastern
University and Duke University.
Assoc. Prof. Nguyen Thi Thuy Loan, PhD 2
International University, VNU-HCMC ER model: review Design ERD •Notation •Entity/ Weak entity •Attributes •Relationship oAttributes on relationships oMultiplicity
oBinary versus n-ary relationships
Assoc. Prof. Nguyen Thi Thuy Loan, PhD oISA relationships 3
International University, VNU-HCMC To d a y ’s topics
•Understand the real-world domain being modeled
•Specify it using a database design model (e.g., E/R)
•Translate specification to the data model of DBMS (e.g., relational) •Create DBMS schema
•Reading material: [GUW] Chapter 3
Assoc. Prof. Nguyen Thi Thuy Loan, PhD 4
International University, VNU-HCMC 5 Example number name nn Trains StopsAt e in ng eerStations address type time type You designed an ER digram
Translate it to a Relational Database Train (number e , ngineer,type) Station (name,address,type)
Assoc. Prof. Nguyen Thi Thuy Loan, PhD TrainStop (train_number,stati a on_n me,time) 5
International University, VNU-HCMC From E/R Diagrams to Relations •Entity set -> relation.
–Attributes -> attributes.
•Relationships -> relations whose attributes are only:
–The keys of the connected entity sets.
–Attributes of the relationship itself.
Assoc. Prof. Nguyen Thi Thuy Loan, PhD 6
International University, VNU-HCMC General steps
•Each entity (regular/weak) in the ER is converted into a schema.
•For (1,1)-(1,1) relationships. We make one entity’s primary key
another entity’s foreign key and vice versa.
•The same for (1,1)-(1,n) relationships, we don’t establish a
new schema but bring the primary key from the (1,n) entity
into the (1,1) entity to make a foreign key.
•For (1,n)-(1,n) relationships, we establish a new schema
containing the primary keys of the entities that formed it and its own attributes.
•For multivalued attribute is converted into a new schema.
Assoc. Prof. Nguyen Thi Thuy Loan, PhD 7
International University, VNU-HCMC Step 1: Regular Entity Types
i. For each regular/strong entity type, create a
corresponding relation that includes all the
simple attributes (includes simple attributes of composite relations)
ii. Choose one of the key attributes as primary
§If composite, the simple attributes together form the primary key.
iii. Any remaining key attributes are kept as
secondary unique keys (these will be useful for
physical tuning with reference to indexing analysis)
Assoc. Prof. Nguyen Thi Thuy Loan, PhD 8
International University, VNU-HCMC Step 2: Weak Entity Types
i. For each weak entity type, create a
corresponding relation that includes all the simple attributes
ii. Add as a foreign key all of the primary key
attribute(s) in the entity corresponding to the owner entity type
iii. The primary key is the combination of all the
primary key attributes from the owner and the
partial key of the weak entity, if any
Assoc. Prof. Nguyen Thi Thuy Loan, PhD 9
International University, VNU-HCMC Step 3: Mapping Binary 1-to-1 Three approaches –Foreign Key •Usually appropriate –Merged Relation
•Possible when both participations are total –Relationship Relation •Not discussed
Assoc. Prof. Nguyen Thi Thuy Loan, PhD 10
International University, VNU-HCMC Step 3: Mapping Binary 1-to-1
i. Choose one relation as S, the other T
§Better if S has total participation (reduces number of NULL values)
ii. Add to S all the simple attributes of the relationship
iii. Add as a foreign key in S the primary key attributes of T
Assoc. Prof. Nguyen Thi Thuy Loan, PhD 11
International University, VNU-HCMC Step 4: Binary 1-to-N
i. Choose the S relation as the type at the N-side
of the relationship, other is T
ii. Add as a foreign key to S all of the primary key attribute(s) of T
Another approach: create a relationship relation
Assoc. Prof. Nguyen Thi Thuy Loan, PhD 12
International University, VNU-HCMC Step 5: Binary M-to-N
i. Create a new relation S(termed: relationship relation)
–In some ERD dialects, actually drawn in
ii. Add as foreign keys the primar keys y of both
relations; their combination forms the primary key of S
iii. Add any simple attributes of the M : N relationship to S
Assoc. Prof. Nguyen Thi Thuy Loan, PhD 13
International University, VNU-HCMC Step 6: Multivalued Attributes i. Create a new relation S
ii. Add as foreign keys the primary keys of the corresponding relation
iii. Add the attribute to S(if composite, the
simple attributes);the combination of all
attributes in Sforms the primary key
Assoc. Prof. Nguyen Thi Thuy Loan, PhD 14
International University, VNU-HCMC
Step 7: Specialization/Generalization
A. Multiple relations –subclass and superclass
§Usually works (assumes unique id at parent)
B. Multiple relations –subclass only
§Should only be used for disjoint
C. Single relation with one type attribute
§Only for disjoint, can result in many NULLs
D. Single relation with multiple type attributes Assoc. Prof. Nguyen § Thi Better Thuy Loan, PhD
for overlapping, could be disjoint 15
International University, VNU-HCMC Example ERD
Assoc. Prof. Nguyen Thi Thuy Loan, PhD 16
International University, VNU-HCMC Resulting Relational Schema
Assoc. Prof. Nguyen Thi Thuy Loan, PhD 17
International University, VNU-HCMC Step 1: Regular Entity Types
i. For each regular/strong entity type, create a
corresponding relation that includes all the
simple attributes (includes simple attributes of composite relations)
ii. Choose one of the key attributes as primary
§If composite, the simple attributes together form the primary key
iii. Any remaining key attributes are kept as
secondary unique keys (these will be useful for
physical tuning w.r.t. indexing analysis)
Assoc. Prof. Nguyen Thi Thuy Loan, PhD 18
International University, VNU-HCMC Example ERD
Assoc. Prof. Nguyen Thi Thuy Loan, PhD 19
International University, VNU-HCMC Step 1Result
Assoc. Prof. Nguyen Thi Thuy Loan, PhD 20
International University, VNU-HCMC Step 2: Weak Entity Types
i. For each weak entity type, create a
corresponding relation that includes all the simple attributes
ii. Add as a foreign key all of the primary key
attribute(s) in the entity corresponding to the owner entity type
iii. The primary key is the combination of all the
primary key attributes from the owner and the
partial key of the weak entity, if any
Assoc. Prof. Nguyen Thi Thuy Loan, PhD 21
International University, VNU-HCMC Step 2Result
Assoc. Prof. Nguyen Thi Thuy Loan, PhD 22
International University, VNU-HCMC Step 3: Mapping Binary 1-to-1 Three approaches –Foreign Key •Usually appropriate –Merged Relation
•Possible when both participations are total –Relationship Relation •Not discussed
Assoc. Prof. Nguyen Thi Thuy Loan, PhD 23
International University, VNU-HCMC Step 3: Mapping Binary 1-to-1
i. Choose one relation as S, the other T
§Better if S has total participation (reduces number of NULL values)
ii. Add to S all the simple attributes of the relationship
iii. Add as a foreign key in S the primary key attributes of T
Assoc. Prof. Nguyen Thi Thuy Loan, PhD 24
International University, VNU-HCMC Step 2Result
Assoc. Prof. Nguyen Thi Thuy Loan, PhD 25
International University, VNU-HCMC Step 3Result
Assoc. Prof. Nguyen Thi Thuy Loan, PhD 26
International University, VNU-HCMC Step 4: Binary 1-to-N
i. Choose the S relation as the type at the N-
side of the relationship, other is T
ii. Add as a foreign key to S all of the primary key attribute(s) of T
Another approach: create a relationship relation
Assoc. Prof. Nguyen Thi Thuy Loan, PhD 27
International University, VNU-HCMC Step 4Result
Assoc. Prof. Nguyen Thi Thuy Loan, PhD 28
International University, VNU-HCMC Step 5: Binary M-to-N
i. Create a new relation S(termed: relationship relation)
–In some ERD dialects, actually drawn in
ii. Add as foreign keys the primar keys y of both
relations; their combination forms the primary key of S
iii. Add any simple attributes of the M : N relationship to S
Assoc. Prof. Nguyen Thi Thuy Loan, PhD 29
International University, VNU-HCMC Step 5Result
Assoc. Prof. Nguyen Thi Thuy Loan, PhD 30
International University, VNU-HCMC Step 6: Multivalued Attributes i. Create a new relation S
ii. Add as foreign keys the primary keys of the corresponding relation
iii. Add the attribute to S(if composite, the simple
attributes);the combination of all attributes in S forms the primary key
Assoc. Prof. Nguyen Thi Thuy Loan, PhD 31
International University, VNU-HCMC Step 6Result
Assoc. Prof. Nguyen Thi Thuy Loan, PhD 32
International University, VNU-HCMC
Step 7: Specialization/Generalization
A. Multiple relations –subclass and superclass
§Usually works (assumes unique id at parent)
B. Multiple relations –subclass only
§Should only be used for disjoint
C. Single relation with one type attribute
§Only for disjoint, can result in many NULLs
D. Single relation with multiple type attributes
§Better for overlapping, could be disjoint
Assoc. Prof. Nguyen Thi Thuy Loan, PhD 33
International University, VNU-HCMC
Specialization/Generalization (A)
Assoc. Prof. Nguyen Thi Thuy Loan, PhD 34
International University, VNU-HCMC
Specialization/Generalization (B)
B. Multiple relations –subclass only
Should only be used for disjoint
Assoc. Prof. Nguyen Thi Thuy Loan, PhD 35
International University, VNU-HCMC
Specialization/Generalization (C)
C. Single relation with one type attribute
Only for disjoint, can result in many NULLs
Assoc. Prof. Nguyen Thi Thuy Loan, PhD 36
International University, VNU-HCMC
Specialization/Generalization (D)
D. Single relation with multiple type attributes
Better for overlapping, could be disjoint
Assoc. Prof. Nguyen Thi Thuy Loan, PhD 37
International University, VNU-HCMC Subclasses: Three Approaches
1. Object-oriented: One relation per subset of
subclasses, with all relevant attributes.
2. Use nulls: One relation; entities have NULL in
attributes that don’t belong to them.
3. E/R style: One relation for each subclass: –Key attribute(s).
–Attributes of that subclass.
Assoc. Prof. Nguyen Thi Thuy Loan, PhD 38
International University, VNU-HCMC
Example: Subclass -> Relations name manf Beers isa color Ales
Assoc. Prof. Nguyen Thi Thuy Loan, PhD 39
International University, VNU-HCMC Object-Oriented Name Manf Bud Anheuser -Busch Beers name manf Beers Name Manf Color Summerbrew Pete’s Drak Ales isa color Ales
Good for queries like “find the color of ales made by Pete’s.”
Assoc. Prof. Nguyen Thi Thuy Loan, PhD 40