Ch07 ER Model
Ch07 ER Model
Jason Arnold
Chapter 7: Entity-Relationship Model
modified from:
Database System Concepts, 6th Ed.
©Silberschatz, Korth and Sudarshan
See www.db-book.com for conditions on re-use
Requirement Analysis Example
Zoo
n The zoo stores information about animals, cages, and zoo keepers.
n Animals are of a certain species and have a name. For each animal
we want to record its weight and age.
n Each cage is located in a section of the zoo. Cages can house
animals, but there may be cages that are currently empty. Cages have
a size in square meter.
n Zoo keepers are identified by their social security number. We store a
first name, last name, and for each zoo keeper. Zoo keepers are
assigned to cages they have to take care of (clean, …). Each cage
that is not empty has a zoo keeper assigned to it. A zoo keeper can
take care of several cages. Each zoo keeper takes care of at least one
cage.
CS425 – Fall 2013 – Boris Glavic 7.2 ©Silberschatz, Korth and Sudarshan
Modeling – ER model
CS425 – Fall 2013 – Boris Glavic 7.3 ©Silberschatz, Korth and Sudarshan
Entity Sets instructor and student
CS425 – Fall 2013 – Boris Glavic 7.4 ©Silberschatz, Korth and Sudarshan
Relationship Sets
CS425 – Fall 2013 – Boris Glavic 7.5 ©Silberschatz, Korth and Sudarshan
Relationship Set advisor
CS425 – Fall 2013 – Boris Glavic 7.6 ©Silberschatz, Korth and Sudarshan
Relationship Sets (Cont.)
student
CS425 – Fall 2013 – Boris Glavic 7.7 ©Silberschatz, Korth and Sudarshan
Degree of a Relationship Set
n binary relationship
l involve two entity sets (or degree two).
n Relationships between more than two entity sets are rare. Most
relationships are binary. (More on this later.)
Example: students work on research projects under the
guidance of an instructor.
relationship proj_guide is a ternary relationship between
instructor, student, and project
CS425 – Fall 2013 – Boris Glavic 7.8 ©Silberschatz, Korth and Sudarshan
Attributes
CS425 – Fall 2013 – Boris Glavic 7.9 ©Silberschatz, Korth and Sudarshan
Composite Attributes
component
attributes
street_number street_name apartment_number
CS425 – Fall 2013 – Boris Glavic 7.10 ©Silberschatz, Korth and Sudarshan
Mapping Cardinality Constraints
n Express the number of entities to which another entity can be
associated via a relationship set.
n For a binary relationship set the mapping cardinality must be one of
the following types:
l One to one (1-1)
l One to many (1-N)
l Many to one (N-1)
l Many to many (N-M)
CS425 – Fall 2013 – Boris Glavic 7.11 ©Silberschatz, Korth and Sudarshan
Mapping Cardinalities
CS425 – Fall 2013 – Boris Glavic 7.12 ©Silberschatz, Korth and Sudarshan
Mapping Cardinalities Example
Person Birth Advisor Student
certificate
CS425 – Fall 2013 – Boris Glavic 7.13 ©Silberschatz, Korth and Sudarshan
Mapping Cardinalities Example
Employee Department Student Course
CS425 – Fall 2013 – Boris Glavic 7.14 ©Silberschatz, Korth and Sudarshan
Mapping Cardinality Constraints Cont.
n What if we allow some elements to not be mapped to another
element?
l E.g., 0:1 – 1
n For a binary relationship set the mapping cardinality must be one of
the following types:
n 1-1 n N-1
l 1-1 l N-1
l 0:1-1 l N-0:1
l 1-0:1 l 0:N-1
l 0:1-0:1 l 0:N-0:1
n 1-N n N-M
l 0:1-N l N-M
l 0:1-0:N l N-0:M
l 1-N l 0:N-M
l 1-0:N l 0:N-0:M
CS425 – Fall 2013 – Boris Glavic 7.15 ©Silberschatz, Korth and Sudarshan
Mapping Cardinality Constraints Cont.
n Typical Notation
l (0:1) – (1:N)
CS425 – Fall 2013 – Boris Glavic 7.16 ©Silberschatz, Korth and Sudarshan
Keys
CS425 – Fall 2013 – Boris Glavic 7.17 ©Silberschatz, Korth and Sudarshan
Keys for Relationship Sets
CS425 – Fall 2013 – Boris Glavic 7.18 ©Silberschatz, Korth and Sudarshan
Keys for Relationship Sets Cont.
CS425 – Fall 2013 – Boris Glavic 7.19 ©Silberschatz, Korth and Sudarshan
Redundant Attributes
n Suppose we have entity sets
l instructor, with attributes including dept_name
l department
and a relationship
l inst_dept relating instructor and department
n Attribute dept_name in entity instructor is redundant since there is an
explicit relationship inst_dept which relates instructors to departments
l The attribute replicates information present in the relationship, and
should be removed from instructor
l BUT: when converting back to tables, in some cases the attribute
gets reintroduced, as we will see.
CS425 – Fall 2013 – Boris Glavic 7.20 ©Silberschatz, Korth and Sudarshan
E-R Diagrams
CS425 – Fall 2013 – Boris Glavic 7.21 ©Silberschatz, Korth and Sudarshan
Entity With Composite, Multivalued, and Derived
Attributes
CS425 – Fall 2013 – Boris Glavic 7.22 ©Silberschatz, Korth and Sudarshan
Entity With Composite, Multivalued, and Derived
Attributes
composite
derived
Multi-valued
CS425 – Fall 2013 – Boris Glavic 7.23 ©Silberschatz, Korth and Sudarshan
Relationship Sets with Attributes
CS425 – Fall 2013 – Boris Glavic 7.24 ©Silberschatz, Korth and Sudarshan
Roles
CS425 – Fall 2013 – Boris Glavic 7.25 ©Silberschatz, Korth and Sudarshan
Cardinality Constraints
CS425 – Fall 2013 – Boris Glavic 7.26 ©Silberschatz, Korth and Sudarshan
One-to-One Relationship
CS425 – Fall 2013 – Boris Glavic 7.27 ©Silberschatz, Korth and Sudarshan
One-to-Many Relationship
CS425 – Fall 2013 – Boris Glavic 7.28 ©Silberschatz, Korth and Sudarshan
Many-to-One Relationships
CS425 – Fall 2013 – Boris Glavic 7.29 ©Silberschatz, Korth and Sudarshan
Many-to-Many Relationship
n An instructor is associated with several (possibly 0) students via
advisor
n A student is associated with several (possibly 0) instructors via
advisor
CS425 – Fall 2013 – Boris Glavic 7.30 ©Silberschatz, Korth and Sudarshan
Participation of an Entity Set in a
Relationship Set
n Total participation (indicated by double line): every entity in the
entity set participates in at least one relationship in the relationship
set
l E.g., participation of section in sec_course is total
every section must have an associated course
n Partial participation: some entities may not participate in any
relationship in the relationship set
l Example: participation of instructor in advisor is partial
CS425 – Fall 2013 – Boris Glavic 7.31 ©Silberschatz, Korth and Sudarshan
Alternative Notation for Cardinality Limits
CS425 – Fall 2013 – Boris Glavic 7.32 ©Silberschatz, Korth and Sudarshan
Alternative Notation for Cardinality Limits
n Alternative Notation
(0,n) (1,1)
CS425 – Fall 2013 – Boris Glavic 7.33 ©Silberschatz, Korth and Sudarshan
E-R Diagram with a Ternary Relationship
CS425 – Fall 2013 – Boris Glavic 7.34 ©Silberschatz, Korth and Sudarshan
Cardinality Constraints on Ternary
Relationship
n We allow at most one arrow out of a ternary (or greater degree)
relationship to indicate a cardinality constraint
n E.g., an arrow from proj_guide to instructor indicates each student has
at most one guide for a project
n If there is more than one arrow, there are two ways of defining the
meaning.
l E.g., a ternary relationship R between A, B and C with arrows to B
and C could mean
1. each A entity is associated with a unique entity from B and C or
2. each pair of entities from (A, B) is associated with a unique C
entity, and each pair (A, C) is associated with a unique B
l Each alternative has been used in different formalisms
l To avoid confusion we outlaw more than one arrow
n Better to use cardinality constraints such as (0,n)
CS425 – Fall 2013 – Boris Glavic 7.35 ©Silberschatz, Korth and Sudarshan
Weak Entity Sets
CS425 – Fall 2013 – Boris Glavic 7.36 ©Silberschatz, Korth and Sudarshan
Weak Entity Sets (Cont.)
n We underline the discriminator of a weak entity set with a dashed
line.
n We put the identifying relationship of a weak entity in a double
diamond.
n Primary key for section – (course_id, sec_id, semester, year)
CS425 – Fall 2013 – Boris Glavic 7.37 ©Silberschatz, Korth and Sudarshan
Weak Entity Sets (Cont.)
n Note: the primary key of the strong entity set is not explicitly stored
with the weak entity set, since it is implicit in the identifying
relationship.
n If course_id were explicitly stored, section could be made a strong
entity, but then the relationship between section and course would
be duplicated by an implicit relationship defined by the attribute
course_id common to course and section
CS425 – Fall 2013 – Boris Glavic 7.38 ©Silberschatz, Korth and Sudarshan
E-R Diagram for a University Enterprise
CS425 – Fall 2013 – Boris Glavic 7.39 ©Silberschatz, Korth and Sudarshan
Reduction to Relational Schemas
CS425 – Fall 2013 – Boris Glavic 7.40 ©Silberschatz, Korth and Sudarshan
Representing Entity Sets With Simple
Attributes
CS425 – Fall 2013 – Boris Glavic 7.41 ©Silberschatz, Korth and Sudarshan
Representing Relationship Sets
CS425 – Fall 2013 – Boris Glavic 7.42 ©Silberschatz, Korth and Sudarshan
Redundancy of Schemas
n Many-to-one and one-to-many relationship sets that are total on the
many-side can be represented by adding an extra attribute to the
“many” side, containing the primary key of the “one” side
n Example: Instead of creating a schema for relationship set inst_dept,
add an attribute dept_name to the schema arising from entity set
instructor
CS425 – Fall 2013 – Boris Glavic 7.43 ©Silberschatz, Korth and Sudarshan
Redundancy of Schemas (Cont.)
CS425 – Fall 2013 – Boris Glavic 7.44 ©Silberschatz, Korth and Sudarshan
Composite and Multivalued Attributes
CS425 – Fall 2013 – Boris Glavic 7.45 ©Silberschatz, Korth and Sudarshan
Composite and Multivalued Attributes
CS425 – Fall 2013 – Boris Glavic 7.46 ©Silberschatz, Korth and Sudarshan
Multivalued Attributes (Cont.)
n Special case:entity time_slot has only one attribute other than the
primary-key attribute, and that attribute is multivalued
l Optimization: Don’t create the relation corresponding to the entity,
just create the one corresponding to the multivalued attribute
l time_slot(time_slot_id, day, start_time, end_time)
l Caveat: time_slot attribute of section (from sec_time_slot) cannot be
a foreign key due to this optimization
CS425 – Fall 2013 – Boris Glavic 7.47 ©Silberschatz, Korth and Sudarshan
Design Issues
n Use of entity sets vs. attributes
n Designing phone as an entity allow for primary key constraints for phone
n Designing phone as an entity allow phone numbers to be used in
relationships with other entities (e.g., student)
n Use of phone as an entity allows extra information about phone numbers
CS425 – Fall 2013 – Boris Glavic 7.48 ©Silberschatz, Korth and Sudarshan
Design Issues
n Use of entity sets vs. relationship sets
l Possible guideline is to designate a relationship set to describe an
action that occurs between entities
l Possible hint: the relationship only relates entities, but does not have an
existence by itself. E.g., hasAddress: (department-address)
CS425 – Fall 2013 – Boris Glavic 7.49 ©Silberschatz, Korth and Sudarshan
Design Issues
n Binary versus n-ary relationship sets
l Although it is possible to replace any nonbinary (n-ary, for n > 2)
relationship set by a number of distinct binary relationship sets + an
aritifical entity set, a n-ary relationship set shows more clearly that
several entities participate in a single relationship.
CS425 – Fall 2013 – Boris Glavic 7.50 ©Silberschatz, Korth and Sudarshan
Binary Vs. Non-Binary Relationships
CS425 – Fall 2013 – Boris Glavic 7.51 ©Silberschatz, Korth and Sudarshan
Converting Non-Binary Relationships to Binary Form
CS425 – Fall 2013 – Boris Glavic 7.52 ©Silberschatz, Korth and Sudarshan
ER-model to Relational Summary
n Rule 1) Strong entity E
l Create relation with attributes of E
l Primary key is equal to the PK of E
n Rule 2) Weak entity W identified by E through relationship R
l Create relation with attributes of W and R and PK(E).
l Set PK to discriminator attributes combined with PK(E). PK(E) is a
foreign key to E.
n Rule 3) Binary relationship R between A and B: one-to-one
l If no side is total add PK of A to as foreign key in B or the other
way around. Add any attributes of the relationship R to A
respective B.
l If one side is total add PK of the other-side as foreign key. Add any
attributes of the relationship R to the total side.
l If both sides are total merge the two relation into a new relation E
and choose either PK(A) as PK(B) as the new PK. Add any
attributes of the relationship R to the new relation E.
CS425 – Fall 2013 – Boris Glavic 7.53 ©Silberschatz, Korth and Sudarshan
ER-model to Relational Summary (Cont.)
n Rule 4) Binary relationship R between A and B: one-to-many/many-to-
one
l Add PK of the “one” side as foreign key to the “many” side.
l Add any attributes of the relationship R to the “many” side.
n Rule 5) Binary relationship R between A and B: many-to-many
l Create a new relation R.
l Add PK’s of A and B as attributes + plus all attributes of R.
l The primary key of the relationship is PK(A) + PK(B). The PK
attributes of A/B form a foreign key to A/B
n Rule 6) N-ary relationship R between E1 … En
l Create a new relation.
l Add all the PK’s of E1 … En. Add all attributes of R to the new
relation.
l The primary key or R is PK(E1) … PK(En). Each PK(Ei) is a foreign
key to the corresponding relation.
CS425 – Fall 2013 – Boris Glavic 7.54 ©Silberschatz, Korth and Sudarshan
ER-model to Relational Summary (Cont.)
n Rule 7) Entity E with multi-valued attribute A
l Create new relation. Add A and PK(E) as attributes.
l PK is all attributes. PK(E) is a foreign key.
CS425 – Fall 2013 – Boris Glavic 7.55 ©Silberschatz, Korth and Sudarshan
E-R Diagram for a University Enterprise
CS425 – Fall 2013 – Boris Glavic 7.56 ©Silberschatz, Korth and Sudarshan
Translate the University ER-Model
n Rule 1) Strong Entities
l department(dept_name, building, budget)
l instructor(ID, name, salary)
l student(ID, name, tot_cred)
l course(course_id, title, credits)
l time_slot(time_slot_id)
l classroom(building,room_number, capacity)
n Rule 2) Weak Entities
l section(course_id, sec_id, semester, year)
CS425 – Fall 2013 – Boris Glavic 7.57 ©Silberschatz, Korth and Sudarshan
Translate the University ER-Model
n Rule 3) Relationships one-to-one
l None exist
CS425 – Fall 2013 – Boris Glavic 7.58 ©Silberschatz, Korth and Sudarshan
Translate the University ER-Model
CS425 – Fall 2013 – Boris Glavic 7.59 ©Silberschatz, Korth and Sudarshan
Translate the University ER-Model
n Rule 7) Multivalued attributes
l department(dept_name, building, budget)
l instructor(ID, name, salary, dept_name)
l student(ID, name, tot_cred, dept_name, instr_ID)
l course(course_id, title, credits, dept_name)
l time_slot(time_slot_id)
l time_slot_day(time_slot_id, start_time, end_time)
l classroom(building,room_number, capacity)
l section(course_id, sec_id, semester, year,
room_building, room_number, time_slot_id)
l prereq(course_id, prereq_id)
l teaches(ID, course_id, sec_id, semester, year)
l takes(ID, course_id, sec_id, semester, year, grade)
CS425 – Fall 2013 – Boris Glavic 7.60 ©Silberschatz, Korth and Sudarshan
ER Design Decisions
n The use of an attribute or entity set to represent an object.
n Whether a real-world concept is best expressed by an entity set or
a relationship set.
n The use of a ternary relationship versus a pair of binary
relationships.
n The use of a strong or weak entity set.
n The use of specialization/generalization – contributes to modularity
in the design.
n The use of aggregation – can treat the aggregate entity set as a
single unit without concern for the details of its internal structure.
CS425 – Fall 2013 – Boris Glavic 7.61 ©Silberschatz, Korth and Sudarshan
Summary of Symbols Used in E-R Notation
CS425 – Fall 2013 – Boris Glavic 7.62 ©Silberschatz, Korth and Sudarshan
End of Chapter 7
modified from:
Database System Concepts, 6th Ed.
©Silberschatz, Korth and Sudarshan
See www.db-book.com for conditions on re-use