Upload
others
View
7
Download
0
Embed Size (px)
Citation preview
CS2300: File Structures and
Introduction to Database Systems
Lecture 5: Entity-Relationship Model
Chapter 3
Doug McGeehan
1
2
Overview of Database Design Process
• Two Main Activities
– Database design
– Applications design
• We focus on Database Design
• Applications Design
– Focuses on the programs and interfaces that
access a database
– Generally considered part of software engineering
33
Purpose of ER Model
• Entity-Relational (ER) model– Informal sketch of a database
– A type of conceptual schema
• ER Diagram:a high-levelvisualizationof a database
ER Diagram
• Three main components
– Entities
– Attributes
– Relationships
4
ER Diagram: Entity
• Recall:
– An Entity is a collection of attributes
defining a particular object
• In the ER model:
– Same definition for Entity
– Entity type: the template for an entity
– Entity set: the entity instances
in a database
5
ER Diagram: Entity
• Recall:
– An Entity is a collection of attributes
defining a particular object
• In an ER Diagram:
– A box enclosing an entity type name
6
Student
Student
name phone
ER Diagram: Attribute
• ER diagram representation of a
simple attribute
– An ellipse enclosing an attribute name
7
• Several types of attributes
– Simple (aka Atomic)
– Composite
– Multi-valued
– Derived
ER Diagram: Attribute
8
Student
age
name
first
middle
last
phone
date of
birth
• Several types of attributes
– Key
• Value must be unique for each and every
individual entity
• Can be used to uniquely identify an entity
• ER diagram: underlined
ER Diagram: Attribute
9
Student
SSN
• Several types of attributes
– Key
• Multiple keys permitted, but each must satisfy
uniqueness on its own
ER Diagram: Attribute
10
Student
SSN
Student ID
License
Number
• Several types of attributes
– Composite key
• Provides uniqueness to an entity
by grouping together several attributes
• Combined attributes must be minimal
• i.e. Uniqueness requires every attribute in
group
ER Diagram: Attribute
11
Vehicle
VINRegistration
State
Tag No.
Exercise
• Identify the entities
present in the
scenario hand out
• Identify attributes
for these entities
• Don’t worry about
relationships yet
Exercise
• Are there attributes that suggest an
intuitive relationship between entities?
13
ER Diagram: Relationship
• Relationships associate two or more
entities
• ER diagram:
– A diamond enclosing a relationship name
14
Instructor SectionTEACHES
ER Diagram: Relationship
• Relationship type:
– Instructor TEACHES Section
• Relationship instance:
– “Doug McGeehan” TEACHES “CS23001A”
15
Instructor SectionTEACHES
ER Diagram: Relationship
• Constraints
– Rules governing participation of entities
in a relationship
– Typically defined by the mini-world being
represented
– A dog can be a stray (no owner)
or owned by one person
– A person can only have two biological
parents 16
ER Diagram: Relationship
• Constraints: Cardinality ratio
– The maximum number of relationship
instances in which an entity can participate
– “How many sections can an instructor
teach?”
– “How many instructors are teaching one
particular section?”
17
Instructor SectionTEACHES
ER Diagram: Relationship
• Constraints: Cardinality ratio
– Instructor:Section would be 1:N
• “One-to-many”
• Each instructor can teach many distinct
sections
• A single section can only be taught by one
instructor
18
Instructor SectionTEACHES1 N
ER Diagram: Relationship
• Constraints: Cardinality ratio
– 1:1 “One-to-One”
– 1:N “One-to-Many”
– N:1 “Many-to-One”
– M:N “Many-to-Many”
19
Exercise
• Who is enrolled in which course?
• What would the cardinality ratio of this
relationship type be?
20
Student CourseENROLLED IN
ER Diagram: Relationship
• Constraints: Participation
– Does an entity’s existence depend on its
relationship to another entity?
21
Student CourseENROLLED IN
ER Diagram: Relationship
• Constraints: Participation
– Total participation:
• Every student must be enrolled
in at least one course
• Every course needs enrolled students
22
Student CourseENROLLED IN
ER Diagram: Relationship
• Constraints: Participation
– Partial participation
• Every instructor does not necessarily need to
teach any sections of any courses
• But every section needs an instructor
23
Instructor SectionTEACHES
Partial Total
ER Diagram: Relationship
• Relationship roles
– What would you name a member variable
of a C++ class that references another
class?
24
ER Diagram: Relationship
• Relationship roles
– What would you name a member variable
of a C++ class that references another
class?
25Instructor SectionTEACHES
sectioninstructor
Instructor SectionTEACHEScurrently
teachingtaught by
ER Diagram: Relationship
• Recursive relationship
– A relationship with another entity of the
same type
26
Person
RENTS FROM
landlord tenant
ER Diagram: Relationship
• Relationship degree
– The number of entity types
that participate in a relationship type
27
ER Diagram: Relationship
• Relationship degree: Binary
28
Instructor SectionTEACHES
currently
teachingtaught by
ER Diagram: Relationship
• Relationship degree: Ternary
– Entities:
• Person
• Beer
• Tavern
– Relationship:
• Some people prefer to drink a particular beer
at a particular bar
• Better prices? Available food pairings?
29
ER Diagram: Relationship
• Relationship degree: Ternary
30
Tavern Beer
Person
PREFERS
ER Diagram: Relationship
• Relationships with Attributes:
• Can this attribute be moved to one of
the participating entity types? 31
Person DogADOPTED
adoption
date
ER Diagram: Weak Entity
Dependent
• An entity type that does not have a key
attribute
– 1. Double box the weak entity type
32
Employee DependentSUPPORTS
Name Age
ER Diagram: Weak Entity
Dependent
• An entity type that does not have a key
attribute
– 2. Underline partial key with dotted line
33
Employee DependentSUPPORTS
Name Age
SUPPORTS
ER Diagram: Weak Entity
Dependent
• An entity type that does not have a key
attribute
– 3. Identifying relationship becomes a
double diamond
34
Employee DependentSUPPORTS
Name Age
SUPPORTS
ER Diagram: Weak Entity
Dependent
• An entity type that does not have a key
attribute
– Can a dependent exist without its
associated employee?
– 4. Total participation of the
weak entity
35
Employee DependentSUPPORTS
Name Age
SUPPORTS
ER Diagram: Weak Entity
Dependent
• Composite attribute or Weak entity?
– Rule of thumb: weak entity if it participates
in other relationship types
36
Employee DependentSUPPORTS
Name Age
COVERSInsurance
ER Diagram: Weak Entity
• Composite attribute or Weak entity?
– Otherwise, composite attribute
37
Employee Dependent
Name Age
Exercise
• Incorporate relationships into our
database scenario’s diagram
38
39
Another Exercise
• We want to elaborate on the model for Beers,
Taverns, and Drinkers:
– A beer has a name, which is its identifier.
– A beer also has a manufacturer.
– A bar has a name, address and license number. Either
address or license number can be used as its identifier.
– A drinker has a name and a customer ID
– Taverns sell some beers.
– Drinkers enjoy some beers.
– Drinkers frequent some taverns
4040
Another Exercise
Drinker IDname
Beer
manfname
SELLS Bars sell somebeers.
ENJOYS
Drinkers enjoysome beers.FREQUENTS
Drinkers frequentsome taverns.
Tavern
name
license
addr
phone
Announcement
• Group formation
– Those not in a group will be grouped at
random
– Random groups should choose their team
leader themselves
– I’ll email random groups for their leader
• Homework #1 is coming soon
41
Announcement
• Quiz #1 is also coming soon
– Same day as homework is due
• Quiz and Homework will cover all
lectures up to the due date
• Closed book, 1 page crib sheet
• 15 minutes
42
43
What’s Next …
• More about Entity-Relationship (ER)
Model (continued)
• Good Design Principles
43