Vous êtes sur la page 1sur 49

COMP231

ER Diagram

COMP231 1
Entity-Relationship Model
 Entity-Relationship (ER) model is a
popular conceptual data model.
 This model is used in the design of database
applications
 The model describes data to be stored and
the constraints over the data.
 E-R model views the real world as a collection
of entities and relationships among
entities.

COMP231 2
Entities and attributes
 An entity is an object in the real world
that is distinguishable from other
objects
 E.g.,
 A classroom
 A teacher
 The address of the teacher

COMP231 3
Entities and attributes
 An entity is described using a set of
attributes whose values are used to
distinguish one entity from another of
same type
name = Chan Tai Man

address = 25, Siu Road, Shatin

age = 55

phone = 1234-5667
COMP231 4
Entities and attributes
 An entity set is a collection of entities
of the same type

e1
(Chan Tai Man, 55, …)

e2
(Cheung Tin, 20, …)

e3
(Wong King, 33, …)

COMP231 5
Entities and attributes
 All entries in a given entity set have the
same attributes (the values may be
different).
employee = (name, address, age, phone)

employee 1 employee 2

name = Chan Tai Man name = Cheung Tin


address = 25, Siu Road, Shatin address = 25, Big Street, Shatin

age = 55 age = 20
phone = 1234-5667 phone = 2338-7779

COMP231 6
ER Diagram
 The ER model can be presented
graphically by an ER diagram

age
age address
address

phone
phone
name
name

Employee
Employee attribute

entity
COMP231 7
Different attribute types
 Simple attribute
 Composite attribute
 Multi-valued attribute
 Derived attribute

COMP231 8
Simple attribute
 Simple attribute
 contains a single value

age
age address
address

phone
phone
name
name

Employee
Employee attribute
COMP231 entity 9
Composite attribute
 Composite attribute
 Contains several components
street city
city
street

country
country
age
age address
address

phone
phone
name
name

Employee
Employee attribute
COMP231 entity 10
Multi-valued attribute
 Multi-valued attribute
 Contains more than one value
street city
city
street

country
country
age
age address
address Double ovals

phone
phone
name
name

Employee
Employee attribute
COMP231 entity 11
Derived attribute
 Derived attribute
 Computed from other attributes

 e.g., age can be computed from data of birth and the

current date
street city
city
One dashed oval street

country
country
age
age address
address

phone
phone
name
name

Date
Date of
of Birth
Birth Employee
Employee attribute
COMP231 entity 12
Key attributes
 Key
 A set of attributes that can uniquely
identity an entity
 E.g., HKID
HKID
HKID name
name

underline
Employee
Employee

COMP231 13
Key attributes
 Composite Key
 Two or more attributes are used to serve
as a key
 E.g., Name or Address alone cannot
uniquely identify an employee, but
together they can
Name
Name Address
Address

Employee
Employee
COMP231 14
Key attributes
 An entity may have more than one key
 E.g., {HKID} and {Name, Address} both
are two keys

Name
Name Address
Address
Is {HKID, Name,
Address} a key?

HKID
HKID Employee
Employee

COMP231 15
Key attributes
 A minimal set of attributes that
uniquely identifies an entity is called a
candidate key.
 E.g., {HKID} and {Name, Address} both
are two candidate keys
Is {HKID, Name,
 If there are many candidate Address} a
keys, we should choose candidate key?

one candidate key as


the primary key.
COMP231 16
Key attributes
 Sometimes, artificial keys can be
created
 E.g., if there is no HKID stored in
Employee, we can create a new
attribute called “EmpID”
Name
Name Gender
Gender

EmpID
EmpID Employee
Employee

COMP231 17
Relationship
 A relationship is an association among several
entities
 The degree refers to the number of entity
sets that participate in a relationship set.
 Relationship sets that involve two entity sets
are binary (or degree two).
 Relationships among more than two entity
sets are rare.

COMP231 18
Binary Relationship
 Employees work in departments
 “Work_in” is a relationship between Employees and
Departments
 It means that an employee works in multiple
departments and vice versa

Name
Name EmpID
EmpID did
did dname
dname

Employees
Employees Work_in
Work_in Departments
Departments

COMP231 19
Recursive Relationship
 Recursive Relationship
 Entity sets of a relationship need not be distinct
 Sometimes, a relationship might involve two entities in the same
entity set
 E.g., Employees related to employees

Name EmpID
EmpID
Name

Employees
Employees

supervisor subordinate

Reports_to
Reports_to
COMP231 20
Recursive Relationship
 Since employees report to other
employees
EmpID
EmpID
Name
Name  Every relationship in “Reports_To” is

of form (emp1, emp2) where both


emp1 and emp2 are entities in
Employees
Employees
employees.
 However, they play different roles.

supervisor subordinate  emp1 reports to emp2, which is

reflect in the role indicators


Reports_to
Reports_to supervisor and subordinate in the
diagram

COMP231 21
Constraints
 The model describes data to be stored
and the constraints over the data

COMP231 22
Constraints
 The mapping of a binary relationship
can be classified into the following
cases:

1-to-1 1-to Many Many-to-1 Many-to-Many


COMP231 23
One-to-many relationship
 One-to-many (1-to-Many) relationship
 An entity in B can be associated with at most one entity
in A
A B

1-to Many
COMP231 24
One-to-many relationship
 A loan is associated with at most one customer via
borrower
 A customer is associated with several (including 0)
A B loans via borrower

1-to Many
Key = {customer-id, loan-number}
COMP231 Not a candidate key! Candidate key = ? 25
Many-to-one relationship
 Many-to-one (Many-to-1) relationship
 Similar to 1-to-many

A B

Many-to-1
COMP231 26
Many-to-one relationship
 A loan is associated with several (including 0)
customers via borrower,
 A customer is associated with at most one loan via
borrower
A B

Many-to-1 Key = {customer-id, loan-number}


COMP231 Not a candidate key! Candidate key = ? 27
One-to-one relationship
 One-to-one (1-to-1) relationship
 An entity in A is related to at most one entity in B
 An entity in B is related to at most one entity in A.
A B

1-to-1
COMP231 28
One-to-one relationship
 A customer is associated with at most one loan via the
relationship borrower
 A loan is associated with at most one customer via
borrower
A B

Key = {customer-id, loan-number}


1-to-1 Candidate key = ?
COMP231 Not a candidate key! 29
Many-to-many Relationship
 Many-to-many Relationship
 An entity in A is associated with any number of entities in B
 An entity in B is associated with any number of entities in A
 That is, there is no restriction in the mapping.
A B

COMP231 Many-to-Many 30
Many-to-many Relationship
 A customer is associated with several (possibly 0)
loans via borrower
 A loan is associated with several (possibly 0)
B
customers via borrower
A

Many-to-ManyKey = {customer-id, loan-number}


COMP231 Candidate key! 31
Participation Constraint
 The above constraints (e.g., 1-to-many) tells
us that a customer borrows some loans.
 A natural question to ask is to whether every
A B
loan is borrowed by at least one customer.
 Suppose that each loan is borrowed by at
least one customer. Such a constraint is
called a participation constraint.

1-to Many

COMP231 32
Participation Constraint
 We can classify participation in
relationships as follows.
A B
 Total
 Each entity in the entity set must be associated
in at least one relationship
 Partial
 Each entity in the entity set may (or may not)
be associated in a relationship
1-to Many

COMP231 33
Participation Constraint
 The participation of loan in borrower is total
 Every loan must be borrowed by a customer
 The participation of customer in borrow is partial
 Some customers may (or may not) borrow loans

COMP231 34
Outline
 Entity
 Relationship
 Binary relationship
 Weak Entity/Strong Entity
 Class Hierarchy
 Relationship
 Non-Binary relationship

COMP231 35
Weak Entities
 Strong Entity
 An entity can be uniquely identified by
some attributes related to this entity
 E.g., Employee has an attribute EmpID
(which can be used to uniquely identify
each employee)
 Weak Entity
 An entity cannot be uniquely identified by
all attributes related to this entity
COMP231 36
Weak Entities
 Example
 Suppose employees can purchase insurance policies to cover
their dependants.
 The attribute of the dependents entity set are pname and
age
 The attribute pname cannot uniquely identify a dependent
 Dependent is a weak entity set.
 A dependent can only be identified by considering some of
its attributes in conjunction with the primary key of
employee (identifying entity set).
 The set of attributes that uniquely identify a weak entity for
a given owner entity is called a discriminator or partial
key.

COMP231 37
Identifying entity set = Employee
Discriminator = {pname}

Weak Entities Key = {EmpID, pname}

EmpID pname

age
Employee Policy
Dependent

 A dependent cannot be uniquely identified by


“pname”.
 Note:
 A child may not be old enough to have a HKID number
 Even if he/she has a HKID number, the company may not
be interested in keeping it in the database.
COMP231 38
Identifying entity set = Employee
Discriminator = {pname}

Weak Entities Key = {EmpID, pname}

EmpID pname

age
Employee Policy
Dependent

 Definition: If a weak entity set W is dependent on a


strong entity set E, we say that E owns W.

 E.g., Employee owns Dependent

COMP231 39
Identifying entity set = loan
Discriminator = {payment-number}

Weak Entities Key = {loan-number, payment-number}

loan-number amount payment-number

amount
loan loan-payment
payment

 A payment itself cannot be identified by


“payment-number”
 loan owns payment
COMP231 40
Outline
 Entity
 Relationship
 Binary relationship
 Weak Entity/Strong Entity
 Class Hierarchy
 Relationship
 Non-Binary relationship

COMP231 41
Class Hierarchy
 Sometimes, it is natural to classify the
entities in an entity set into subclasses
name
name
ID
ID address
address

Employees
Employees

Hour_worked
Hour_worked Contract_ID
Contract_ID
Hour_wages
Hour_wages ISA
ISA

Hourly_Emps
Hourly_Emps Contract_Emps
Contract_Emps
COMP231 42
Class Hierarchy
 Attributes are inherited by the entity set in
the subclass.
 E.g., the attributes defined for an Hourly_Emps
entity are the attributes for Employees plus that of
Hourly_Emps
 A class hierarchy can be viewed in one of the
two ways.
 A class is specialized into subclasses.
 The subclasses are generalized by a superclass.

COMP231 43
Class Hierarchy
 We can specify two kinds of constraints with
respect to ISA hierarchies
 Overlap constraints
 Determine whether two subclasses are allowed to
contain the same entity
e.g., Can an employee be an Hourly_Emps as well as a
Contract_emps entity?
 Covering constraints
 Determine whether the entities in the subclasses
collectively include all entities in the superclass.
E.g., Does every Employees entity also have to be an
Hourly_Emps or a Contract_emps

COMP231 44
Class Hierarchy
 Reason why we use class hierarchy
 Add descriptive attribute that make
sense only for the entities in a subclass
 E.g., Hourly_wages does not make sense for a
Contract_Emps entity.
 Identify the set of entities that participate
in some relationships
 E.g., We may want to have a relationship called
“Bonus” with Contract_Emps (not Hourly_Emps)

COMP231 45
Outline
 Entity
 Relationship
 Binary relationship
 Weak Entity/Strong Entity
 Class Hierarchy
 Relationship
 Non-Binary relationship

COMP231 46
Non-binary Relationship
 Ternary Relationship
(i.e., a relationship involving 3 entities)
 An employee works for the department in a
location
EmpID
EmpID Name
Name did
did dname
dname

Employees
Employees Work_in
Work_in Departments
Departments

Key = ?

address
address Locations
Locations capacity
capacity
COMP231 47
Non-binary Relationship
 In general, any non-binary relationship
can be represented using binary
relationships by creating an artificial
entity set.

COMP231 48
Non-binary Relationship
R a1 b1 c1 e1
a2 b1 c1 e2
a2 b1 c2 e3
a2 b2 c2 e4
Ra Rb Rc

a1 e1 b1 e1 c1 e1
a2 e2 b1 e2 c1 e2
a2 e3 b1 e3 c2 e3
COMP231 a2 e4 b2 e4 c2 e4 49

Vous aimerez peut-être aussi