CS122A Conceptual Modeling of Data
CS122A Conceptual Modeling of Data
E/R Relational
Ideas and DBMSs
information
3
Database Design Process
miniworld
Requirement Analysis
5
Overview of Entity/Relationship
(ER) Model
Entities
Relationships
Roles of entities in a relationship
Constraints on entities:
domain constraints
key constraints
Constraints on relationships
Cardinality Constraints (mapping constraints in SKS)
Participation Constraints (existence dependencies in SKS)
Weak Entity Sets
Multiway relationships
Subclass/superclass Relationships
Aggregation
6
Entities and Entity Sets
Entities
nouns, things in the world.
E.g., students, courses, employees, departments,
flights, patients, ...
Attributes
properties of entities.
E.g., course name, deptname, departure time, age,
room#, ...
Entity set -- a set of entities that have the same
attributes.
In OO terminology, an entity set is similar to a class,
and an entity similar to an instance
7
Attributes
single-valued vrs multi-valued:
color of car could be multi-valued
salary of employee is single-valued
atomic vrs composite:
age of a person is atomic
address of a person could be composite
stored vrs derived:
derived attributes are those that can be derived from
other attributes or entities, e.g., age can be derived
from date of birth.
All other attributes are stored attributes
8
Relationships
Relationship:
association between multiple entities
Relationship Set:
set if relationships over the same entity sets
Binary, Ternary, 4-nary, relationship sets
Cust-Account Relationship set account
customer
259 10000
sam 62900 main austin
305 20000
364 200000
9
Visualizing ER Relationships as a
Table
10
ER Diagram -- graphical
representation of ER schema
cust name
street
opening date
worker
12
Constraints on Entity Sets
Key Constraint:
With each entity set a notion of a key can be associated.
A key is a set of attributes that uniquely identify an entity in entity
set.
Examples:
designer may specify that {ssno} is a key for a entity set
customer entity with attributes {ssno, accountno, balance,
name, address}
designer may specify that {accountno} is also a key , that is, no
joint accounts are permitted.
Denoted in ER diagram by underlining the attributes that form a
key
multiple keys may exist in which case one chosen as primary key
and underlined. Other keys called secondary keys either not
indicated or listed in a side comment attached to the diagram.
13
Constraints on Entity Sets (cont.)
Domain constraint:
with each simple attribute a domain is associated. The value
of the attribute for each entity is constrained to be in the
domain.
14
Cardinality Constraints on
Relationship Sets
Consider binary relationship set R between entity sets A
and B
One to one: an entity in A is associated with at most one
entity in B, and an entity in B is associated with at most
one entity in A.
an employee has only one spouse in a married-to relationship.
Many to One: An entity in A is associated with at most
one entity in B, an entity in B is associated with many
entities in A.
an employee works in a single department but a department
consists of many employees.
15
Cardinality Constraints on
Relationship Sets (cont.)
16
Multiplicity of Relationships
17
Many to Many Relationship
opening date
legal legal
opening date
Illegal legal
opening date
opening date
20
One to One Relationship
opening date
Illegal
1 customer can have 1
account.
One account can be owned Illegal
by 1 customer
relationship attributes can
be shifted to either of the Legal
entity sets
21
Participation Constraints
Participation of an entity set A in the
relationship set R1 can be total
Each entity in entity set A is constrained to be
related to other entities via relationship R1.
Examples
Participation of entity set employee in the
relationship belongs-to with the entity set
department may be total.
Each employee must belong to at least one
department.
22
Participation Constraints
Total participation is also called existential
dependency
If an entity does not have a total participation
in a relationship, it is said to have a partial
participation
In ER diagram, total participation represented
using a double line between the relationship
and entity set that totally participates in the
relationship
23
Example
Ss# name amount loanid
Belongs-to
Customer- branch
of
branchid location
Keys: ss#, loanid, branchid
Cardinality constraint: each loan belongs to a single branch
Participation constraints:
Each customer must be a customer of at least one branch
Each loan must belong to some branch
24
Weak Entity Sets
Entity sets that do not have sufficient attributes to
form a key are called weak entity sets.
A weak entity set existentially depend upon (one or
more) strong entity sets via a one-to-many
relationship from whom they derive their key
A weak entity set may have a discriminator (or a
partial key) that distinguish between weak entities
related to the same strong entity
25
Weak Entity Sets (cont.)
cust name
street
acct number balanc
e
ssn
city
o
opening date
log
reviewer
rating
name
28
Multiway Relationships
Usually binary relationships (connecting two E.S.) suffice.
However, there are some cases where three or more E.S. must
be connected by one relationship.
Similar to binary relationship, cardinality and participation
constraints defined over multiway relationships
branchName
key
key
branch
social- acct#
security CAB Relationship Set
balance 29
Cardinality Constraint over
Multiway Relationships
branchName
key
key
branch
social-
acct#
security
Interpretation:
Each pair of customer and
account determine the branch
(that is, have a single branch
related to them).
Legal 30
Cardinality Constraint over
Multiway Relationships
branchName
key
key branch
social-
acct#
security
key branch
social-
acct#
security
branch
CAB account
r
customer C
B A
B
customer
C account
A
branch
B
C A
r CAB account
customer
branch
36
Limitations of the Basic ER
Model Studied So Far
How to represent this in the ER model:
associate an attribute -- account-type with the accounts entity set
Problems:
different attributes may be associated with the account depending on its type
checking: overdraft amount
savings: interest rate
depending upon its type, savings and checking accounts may participate in different
relationships.
Another approach:
entity sets: checking, savings, and accounts.
relationships: 1-1 between checking and accounts, and 1-1 between savings and
accounts
Problems:
Not intuitive: checking and savings are represented as entities different from
accounts, even though they are accounts
Redundancy of information: info about accounts represented both in checking /
savings as well as account entity set
Potential Errors: Same account could be erroneously associated with both checking as
well as savings.
37
Subclass/Superclass Relationships
account#
account
balance
IS A
savings checking
38
Reason why Superclass/Subclass
relationships arise in ER Schemas
Superclass and Subclass relationships arise during
schema design due to the process of specialization
and generalization
Specialization: process of classifying a class of objects
into more specialized subclasses
E.g., during design, we begin with an employee entity set.
We then specialize the employee set into different types of
employees.
Generalization: Reverse of specialization -- it is a
process of synthesis of two or more (lower level)
entity sets to produce a higher-level entity set.
E.g., during design, we have identified a car, a sports utility
vehicle, and a truck. We generalize these classes to create
an automobile entity set. 39
Types of Class/Subclass
Relationships
Disjoint vrs Overlapping:
if the subclasses of the entity set do not overlap then it is
disjoint (denoted by a d next to ISA triangle).
Else, overlapping (denoted by a o next to ISA triangle)
Total vrs Partial:
If an entity in a superclass belongs to at least one of the
subclasses, then total. (denoted by a double line from
superclass to ISA triangle)
Else, partial
Key of entity set corresponding to the subclass is the
same as the key for the superclass.
40
Superclass/Subclass Lattice
Class/Subclass relationships might form a hierarchy (tree)
or a lattice
person
o
ISA
d
d
IS
IS A
A
student
grad
staff assistant undergrad
faculty
o
IS A
RA TA 41
Multiple Inheritance
In a class/subclass relationship, the subclass inherits all its attributes from
the superclass.
If a subclass has 2 or more superclasses, then subclass inherits from all the
superclasses (multiple inheritance)
How should conflicts be resolved?
Example:
Employee Entity Set: with an attribute country denoting the country of
citizenship
Asians Entity Set: with an attribute country denoting the country from
which a particular person originated.
Asian_Employee Entity set is a subclass of both Employee and Asians.
However, what does country attribute of the Asian_Employee
correspond to.
ER model mute on multiple inheritance
42
Limitations of ER Model
work
employee project
using
relationships among
relationships not
permitted in ER! tools
43
Aggregation
employee project
works
using
N
tools
Treat the relationship set work and the entity sets employee
and projects as a higher level entity set-- an aggregate entity
set
Permit relationships between aggregate entity sets and other
entity sets
44
Representation without
Aggregation in ER Model
employee project
works
using
redundant
relationship!
tools
employee project
EP
works using
awkward schema!
tools
45
Review of ER Model
Basic Model:
Entities : strong, weak
Attributes associated with entity sets and relationships
Relationships: binary, ternary, ...
Role of entity sets in a relationship
Constraints on entity set: domain constraints, key constraint
Constraint on relationships: cardinality -- 1-1, 1-N, M-N,
participation (also called existential) --total vrs partial
Extended Model:
Notion of superclass and subclass
Superclass/subclass relationships: disjoint vrs overlapping, total
vrs partial
Notion of aggregation
46
E/R Design Cycle
Good design important since schemas do not
change often
The first version is almost always wrong.
Typical Schema Design Cycle
1: Requirement Analysis: Learn about the application.
what problem does the application solve, what questions does the application ask
about the data, what data does the application need to answer these questions.
2: Design a trial schema
-- top-down strategy: define high level concepts and then use successive
refinements
-- bottom-up strategy: start with schema containing basic abstractions and then
combine or add to them
3: Evaluate schema for quality and completeness.
-- consider the future: how is the application likely to change? Account for
change
4: Iterate until satisfied
47
Schema Design Issues
start date
50
Redundant Relationship
is-customer-o
supplier f
supplies
project
item
used-by
The fact that a project is-customer-of a supplier can be derived from the
relationships between supplier and item and between item and project.
That is, a project is-customer-of a supplier if there is a item that the supplier
supplies which is used by the project.
Redundancy analysis can be tricky -- if supplies is a N:N relationship, then
schema does not contain redundancy.
51
A Design Problem
We wish to design a database representing
cities, counties, and states in the US.
For states, we wish to record the name,
population, and state capital (which is a city).
For counties, we wish to record the name, the
population, and the state in which it is located.
For cities, we wish to record the name, the
population, the state in which it is located and
the county in which it is located.
52
Uniqueness assumptions:
Names of states are unique.
Names of counties are only unique within a state
(e.g., 26 states have Washington Counties).
Cities are likewise unique only within a state
(e.g., there are 24 Springfields among the 50
states).
Some counties and cities have the same name,
even within a state (example: San Francisco).
All cities are located within a single county.
53
Design 1: Bad design
Popu. name
states
Co. name
Co. Popu.
Located-in2
cities
capital
County Population
Ci. name
Ci. Popu.
repeated for each city
54
Design 2 -- good design
Co. Popu.
Co. name Popu. name
Belongs-to
cities
capital
Does not capture the constraints that express trains only stop only at express
stations and local trains stop at all local stations
57
Design 2: Better Design
sname
saddress
number engineer
time stations
trains
StopsAt2
d
d IS
IS tim A
A e
stopsAt1