ER-to-Relational Mapping
1
Example ERD
2
Resulting Relational Schema
3
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)
4
Example ERD
5
Step 1
Result
6
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
7
Example ERD
8
Step 2
Result
9
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
10
Step 3: Mapping Binary 1-to-1
Foreign Key
i. Choose one relation asS , the otherT
§ 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 inS the primary key
attributes ofT
11
Example ERD
12
Step 2
Result
13
Step 3
Result
14
Step 4: Binary 1-to-N
i. Choose theS relation as the type at the
N-side of the relationship, other isT
ii. Add as a foreign key to S all of the
primary key attribute(s) ofT
Another approach: create a relationship
relation
15
Example ERD
16
Step 4
Result
17
Step 5: Binary M-to-N
i. Create a new relationS (termed:
relationship relation )
– In some ERD dialects, actually drawn in
ii. Add as foreign keys the primary keys of
both relations; their combination forms the
primary key ofS
iii. Add any simple attributes of the M:N
relationship toS
18
Example ERD
19
Step 5
Result
20
Step 6: Multivalued Attributes
i. Create a new relationS
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
S forms the primary key
all attributes in
21
Example ERD
22
Step 6
Result
23
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
24
Specialization/Generalization (A)
25
Specialization/Generalization (B)
26
Specialization/Generalization (C)
27
Specialization/Generalization (D)
28
Summary
• Mapping from ERDs to relations is an
algorithmic process
• Some choice points involve comparing
time-space tradeoffs (more in physical
design)
29
Mapping Exercise
Exercise 7.4.
FIGURE 7.7
An ER schema for a
SHIP_TRACKING database.
Slide 7- 30
In-Class Exercise
Consider the following relations for a database that keeps track of student
enrollment in courses and the books adopted for each course:
STUDENT(SSN, Name, Major, Bdate)
COURSE(Course#, Cname, Dept)
ENROLL(SSN, Course#, Quarter, Grade)
BOOK_ADOPTION(Course#, Quarter, Book_ISBN)
TEXT(Book_ISBN, Book_Title, Publisher, Author)
Draw a relational schema diagram specifying the foreign keys for this
schema.
Slide 7- 31