You are on page 1of 52

Database Systems

Mapping from the (E)ER Model to


the Relational Model
CMPS 244
Conceptual Modeling Using
the Entity-Relationship Model
firstname lastname
name year
credithours
hobby id name CID

m n
numofcourses STUDENT ENROLLED COURSE

grade
n

TEACHES
office
m

id
PROFESSOR name
Logical Modeling Using the
Relational Model
STUDENT HOBBY

Id Firstname Lastname Student_id Hobby

ENROLLMENT

Student_id Course_name Course_year Grade

COURSE

Name Year Credit_hours

TEACHES
PROFESSOR
Professor_id Course_name Course_year

Id Name Office
Mapping from the (E)ER
Model to the Relational Model
• Mapping of Entity Sets

• Mapping of Relationship Sets

• Mapping of Specializations and Generalizations

• Mapping of Categorization
Mapping from the (E)ER
Model to the Relational Model
• Mapping of Entity Sets

• Mapping of Relationship Sets

• Mapping of Specializations and Generalizations

• Mapping of Categorization
Mapping of Strong Entity Sets

• For each strong entity set E, create a relation R that


includes all the simple attributes of E

• The primary key of R is the key of E


Mapping of Non-Simple
Attributes
• Composite attributes
• Include all the simple attributes that the composite attribute
is composed of

• Derived attributes
• Do not include them

• Multivalued attributes
• Create a new relation R’ for each multivalued attribute A
• Include a foreign key FK to R in R’
• Primary key of R’ is the combination of A and the PK of R
Mapping Strong Entity Set
Examples
office

PROFESSOR
id
PROFESSOR name Id Name Office

name year
credithours
CID
COURSE

COURSE Name Year Credit_hours


Mapping Non-Simple
Attributes Example
firstname lastname

hobby id name

numofcourses STUDENT

STUDENT HOBBY

Id Firstname Lastname Student_id Hobby


Mapping of Weak Entity sets

• For each weak entity set E’ with owner E whose


corresponding relation is R
• Create a relation R’
• Include a foreign key FK to the relation R in R’
• The primary key of R’ is the partial key of E’ and the
key of E
Mapping of Weak Entity Sets
Example
office

PROFESSOR
id
PROFESSOR name Id Name Office

RELATED RELATIVE

Professor_id Name Relationship


relationship m
name

RELATIVE
Mapping from the (E)ER
Model to the Relational Model
• Mapping of Entity Sets

• Mapping of Relationship Sets

• Mapping of Specializations and Generalizations

• Mapping of Categorization
Mapping of 1:1 Relationships

• For each 1:1 relationship set P with participating


entity sets Ei and Ej whose corresponding relations
are Ri and Rj respectively
• Include a foreign key FK to Ri in Rj and add the
attributes of the relationship P to Rj, or
• Include a foreign key FK to Rj in Ri and add the
attributes of the relationship P to Ri, or
• Create a new relation R with two foreign keys, FKi to
Ri and FKj to Rj and add the attributes of the
relationship P to R
• Primary key of R is either FKi or FKj
Mapping of 1:1 Relationships
PKi A PKj
Ri Rj
1 1
Ei P Ej PKi … PKj …

Ri Rj
PKi … PKj … FKi A

Ri Rj
PKi … FKj A PKj …

Ri R Rj
PKi … FKi FKj A PKj …

Ri R Rj
PKi … FKi FKj A PKj …
Mapping of 1:1 Relationship
Example (Option 1)
office

PROFESSOR
id
PROFESSOR name Id Name Office Course_name Course_year

LEADS COURSE

name Name Year Credit_hours


credithours 1 year

CID
COURSE
Mapping of a 1:1 Relationship
Example (Option 2)
office

PROFESSOR
id
PROFESSOR name Id Name Office

LEADS COURSE

name Name Year Credit_hours Professor_id


credithours 1 year

CID
COURSE
Mapping of a 1:1 Relationship
Example (Option 3a)
office PROFESSOR

Id Name Office
id
PROFESSOR name

LEADS
1
Professor_id Course_name Course_year

LEADS
COURSE
name
credithours 1 year
Name Year Credit_hours
CID
COURSE
Mapping of a 1:1 Relationship
Example (Option 3b)
office PROFESSOR

Id Name Office
id
PROFESSOR name

LEADS
1
Professor_id Course_name Course_year

LEADS
COURSE
name
credithours 1 year
Name Year Credit_hours
CID
COURSE
Which approach is better here?
PROFESSOR COURSE

Include a foreign key to PROFESSOR in


COURSE since the foreign key in this case is
smaller, i.e., Id of PROFESSOR is smaller
than (name, year) of COURSE

19
Which approach is better here?
PROFESSOR COURSE

Include a foreign key to COURSE in


PROFESSOR since majority of courses do not
participate in the relationship, i.e., many null
values the other way around

20
Which approach is better here?
PROFESSOR COURSE

Create a new relation since most of the entities


from both sets do not participate in the
relationship, i.e., many null values otherwise

21
Which approach is better here?
office

PROFESSOR
id
PROFESSOR name Id Name Office Course_name Course_year

LEADS COURSE

name Name Year Credit_hours


credithours 1 year

CID
Better approach because we know that every
COURSE
professor participates in the relationship so no
null values
Which approach is better here?
office

PROFESSOR
id
PROFESSOR name Id Name Office

LEADS COURSE

name Name Year Credit_hours Professor_id


credithours 1 year

CID
Better approach because Id of professor is
COURSE
smaller
Mapping of 1:1 Relationships
Summary
• Try to minimize space consumed by
• choosing the relation with the smaller primary key as
the referenced relation
• choosing the relation of the entity set with total
participation as the referencing relation (no null values)
• choosing the relation of the entity set whose most
entities participate in the relationship as the referencing
relation (fewer null values)
• creating a new relation if most entities from both entity
sets do not participate in the relationship
Mapping of 1:N Relationships

• For each 1:N relationship set P with participating entity


sets Ei and Ej whose corresponding relations are Ri and
Rj, respectively
• If Ej is the entity set at the N-side, then include a foreign key
FK to Ri in Rj and add the attributes of the relationship P to
Rj, or
• If Ei is the entity set at the N-side, then include a foreign key
FK to Rj in Ri and add the attributes of the relationship P to
Ri, or
• Create a new relationship R with two foreign keys, FKi to Ri
and FKj to Rj and add the attributes of the relationship P to
R
• Primary key of R is Fki if Fki is at the N-side or Fkj if Fkj is at
the N-side
Mapping of 1:N Relationships
PKi A PKj PKi A PKj

1 N N 1
Ei P Ej Ei P Ej

Ri Rj Ri Rj
PKi … PKj … FKi A PKi … FKj A PKj …

Ri Rj Ri Rj
PKi … PKj … PKi … PKj …

R R
FKi FKj A FKi FKj A
Mapping of 1:N Relationship
Example (Approach 1)
office

PROFESSOR
id
PROFESSOR name Id Name Office

LEADS COURSE

name Name Year Credit_hours Professor_id


credithours m year

CID
Because COURSE is at the N-side, i.e., each
COURSE
course is related to at most one professor only
Mapping of 1:N Relationship
Example (Approach 2)
office PROFESSOR

Id Name Office
id
PROFESSOR name

LEADS
1
Professor_id Course_name Course_year

LEADS
COURSE
name
credithours m year
Name Year Credit_hours
CID
COURSE
Which approach is better here?
PROFESSOR COURSE

Create a new relation since a lot of courses do


not participate in the relationship, i.e., many
null values otherwise

29
Mapping of N:M
Relationships
• For each M:N relationship set P with participating
entity sets Ei and Ej whose corresponding relations
are Ri and Rj, respectively
• Create a new relation R with two foreign keys FKi to Ri
and FKj to Rj and include all attributes of P in R
• Primary key of R is FKi and FKj
Mapping of M:N Relationship
Example
office PROFESSOR

Id Name Office
id
PROFESSOR name

LEADS
n
Professor_id Course_name Course_year

LEADS
COURSE
name
credithours m year
Name Year Credit_hours
CID
COURSE
Mapping of n-ary
Relationships
• For each n-ary relationship set P
• Create a new relation S to represent P
• Include primary keys of relations of participating entity
sets as foreign keys in S
• Include all attributes of P as attributes in S
• Primary key of S is the combination of all foreign keys
in general (unless one or more entity sets participating
in P has a cardinality of 1)
Mapping of n-ary
Relationship Example
firstname lastname
name year
credithours
hobby id name
CID

m n
numofcourses STUDENT TEACHES COURSE

grade

1 office

id
PROFESSOR name
Mapping of N-ary
Relationship Example (2)
STUDENT HOBBY

Id Firstname Lastname Student_id Hobby

TEACHES
Student_id Course_name Course_year Professor_id Grade

COURSE

Name Year Credit_hours

PROFESSOR

Id Name Office
Mapping from the (E)ER
Model to the Relational Model
• Mapping of Entity Sets

• Mapping of Relationship Sets

• Mapping of Specializations and Generalizations

• Mapping of Categorization
Mapping of Specializations
and Generalizations
• Given a superclass S and subclasses S1, S2, …, Sn
• Map the superclass S into a relation R
• Map each subclass Si into a relation Ri
• Include a foreign key to R in each Ri
• Primary key of Ri is primary key of R
• Works for all specializations/generalizations (total or
partial, overlapping or disjoint)
Mapping of Specialization
Example
firstname lastname

hobby id name

STUDENT

gpa
thesis
numofcourses

UNDERGRAD POSTGRAD
Mapping of Specialization
Example (2)
STUDENT HOBBY

Id Firstname Lastname Student_id Hobby

UNDERGRAD POSTGRAD

Id Gpa Id Thesis
Mapping of Specializations
and Generalizations (2)
• Given a superclass S and subclasses S1, S2, …, Sn
• Map each subclass Si into a relation Ri
• Include all attributes of S in each relation Ri
• Primary key of Ri is the key of S
• Works only for total specializations/generalizations (i.e.,
every entity in the superclass must belong to at least one of
the subclasses)
• Only recommended if
• the specialization is disjoint, otherwise, we repeat the same
entity with its superclass attributes multiple times, and
• there are few specific attributes and relationships for the
superclass
Mapping of Specialization
Example
firstname lastname

hobby id name

STUDENT

gpa
thesis
numofcourses

UNDERGRAD POSTGRAD
Mapping of Specialization
Example (2)
UNDERGRAD UNDERGRAD_HOBBY

Id Firstname Lastname Gpa Student_id Hobby

POSTGRAD POSTGRAD_HOBBY

Id Firstname Lastname Thesis Student_id Hobby


Mapping of Specializations
and Generalizations (3)
• Given a superclass S and subclasses S1, S2, …, Sn
• Map the superclass S into a relation R
• Include all attributes of each subclasses Si in R
• Primary key of R is the key of S
• Add a type attribute to R
• Works only for disjoint subclasses, otherwise type must
be a multivalued attribute
• Recommended only if very few specific attributes and
relationships for the subclasses
Mapping of Specialization
Example
firstname lastname

hobby id name

STUDENT

gpa
thesis
numofcourses

UNDERGRAD POSTGRAD
Mapping of Specialization
Example (2)

STUDENT HOBBY

Id Firstname Lastname Gpa Thesis Type Student_id Hobby


Mapping of Specializations
and Generalizations (4)
• Given a superclass S and subclasses S1, S2, …, Sn
• Map the superclass S into a relation R
• Include all attributes of each subclasses Si in R
• Primary key of R is the key of S
• Add n Boolean attributes ti to R indicating whether an
entity belongs to subclass Si or not
• Works for overlapping specialization/generalization
• Recommended only if very few specific attributes and
relationships for the subclasses
Mapping of Generalization
Example
firstname lastname

id name

PERSON

hobby office
numofcourses
Type=“staff ”

Type=“student”

STUDENT STAFF
Mapping of Generalization
Example (2)

PERSON HOBBY

Id Firstname Lastname Office Is_student Is_staff Person_id Hobby


Mapping from the (E)ER
Model to the Relational Model
• Mapping of Entity Sets

• Mapping of Relationship Sets

• Mapping of Specializations and Generalizations

• Mapping of Categorization
Mapping of Categorization

• Given a category S and superclasses S1, S2, …, Sn


• Map the category S into a relation R
• Include all attributes of S in R
• Use key of the category as the primary key of R or
create a surrogate key as the primary key of R
• Create a relation Ri for each superclass Si
• Add a foreign key FKi to R in each relation Ri
Mapping Categorization
Example
account no balance

OWNER

name
id name
address

job
PERSON COMPANY
Mapping of Specialization
Example (2)
OWNER

Account_no Balance

PERSON COMPANY

Id Name Job Account_no Name Address Account_no


Further Readings

• Chapter 8 of the main textbook

• Chapter 9 if you are using the online version

You might also like