er design (1) - computing science - simon fraser university1).pdf · • how to annotate...

20

Click here to load reader

Upload: phungtruc

Post on 22-Jul-2018

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: ER Design (1) - Computing Science - Simon Fraser University1).pdf · • How to annotate cardinality constraints in ER diagram? ... the enterprise being modeled ... Database I --

ER Design (1)

Page 2: ER Design (1) - Computing Science - Simon Fraser University1).pdf · • How to annotate cardinality constraints in ER diagram? ... the enterprise being modeled ... Database I --

CMPT 354: Database I -- ER Design (1) 2

Entity-Relationship (ER) Model

• Elements in a database: data entries• Data entries represent

– Entities: data objects, e.g., students, courses, and instructors

– Relationships among entities: students take courses, instructors teach courses

• ER model: modeling data using entities and relationships

Page 3: ER Design (1) - Computing Science - Simon Fraser University1).pdf · • How to annotate cardinality constraints in ER diagram? ... the enterprise being modeled ... Database I --

CMPT 354: Database I -- ER Design (1) 3

E-R Diagrams

• Rectangles represent entity sets• Diamonds represent relationship sets• Lines link attributes to entity sets and entity

sets to relationship sets• Ellipses represent attributes

– Double ellipses represent multivalued attributes– Dashed ellipses denote derived attributes

• Underline indicates primary key attributes

Page 4: ER Design (1) - Computing Science - Simon Fraser University1).pdf · • How to annotate cardinality constraints in ER diagram? ... the enterprise being modeled ... Database I --

CMPT 354: Database I -- ER Design (1) 4

Example

Page 5: ER Design (1) - Computing Science - Simon Fraser University1).pdf · • How to annotate cardinality constraints in ER diagram? ... the enterprise being modeled ... Database I --

CMPT 354: Database I -- ER Design (1) 5

Involving One Entity Set Twice

• Ann and Bob are employees in SmallCompany– Two entities in entity set employee

• Ann is Bob’s supervisor– (Ann, Bob) ∈ supervision– Relationship set supervision involves entity set

employee twice– How can we call the first employee and the

second employee in a supervision relationship?

Page 6: ER Design (1) - Computing Science - Simon Fraser University1).pdf · • How to annotate cardinality constraints in ER diagram? ... the enterprise being modeled ... Database I --

CMPT 354: Database I -- ER Design (1) 6

Roles

• The labels “manager” and “worker” are called roles; they specify how employee entities interact via the works_for relationship set

• Role labels are optional, and are used to clarify semantics of the relationship

Page 7: ER Design (1) - Computing Science - Simon Fraser University1).pdf · • How to annotate cardinality constraints in ER diagram? ... the enterprise being modeled ... Database I --

CMPT 354: Database I -- ER Design (1) 7

Keys and Mapping Cardinality

• One to one relationship set– Use a candidate key in either entity set– University-president(university, president)

• Many to one relationship set– Use a candidate key in the one side entity set– Teaching (instructor, courses)

• Many to many relationship set– Use a candidate key in each participating entity set– Take-course(student, course)

• How to annotate cardinality constraints in ER diagram?

Page 8: ER Design (1) - Computing Science - Simon Fraser University1).pdf · • How to annotate cardinality constraints in ER diagram? ... the enterprise being modeled ... Database I --

CMPT 354: Database I -- ER Design (1) 8

Cardinality Constraints

• Drawing either a directed line (→), signifying “one,” or an undirected line (—), signifying “many,”between the relationship set and the entity set.

• One-to-one relationship– Each customer can have at most one loan– Each loan is associated with at most one customer

Page 9: ER Design (1) - Computing Science - Simon Fraser University1).pdf · • How to annotate cardinality constraints in ER diagram? ... the enterprise being modeled ... Database I --

CMPT 354: Database I -- ER Design (1) 9

One-To-Many Relationship

• In the one-to-many relationship a loan is associated with at most one customer via borrower, a customer is associated with several (including 0) loans via borrower

Page 10: ER Design (1) - Computing Science - Simon Fraser University1).pdf · • How to annotate cardinality constraints in ER diagram? ... the enterprise being modeled ... Database I --

CMPT 354: Database I -- ER Design (1) 10

Many-To-One Relationships

• In a 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

Page 11: ER Design (1) - Computing Science - Simon Fraser University1).pdf · • How to annotate cardinality constraints in ER diagram? ... the enterprise being modeled ... Database I --

CMPT 354: Database I -- ER Design (1) 11

Many-To-Many Relationship

• A customer is associated with several (possibly 0) loans via borrower

• A loan is associated with several (possibly 0) customers via borrower

Page 12: ER Design (1) - Computing Science - Simon Fraser University1).pdf · • How to annotate cardinality constraints in ER diagram? ... the enterprise being modeled ... Database I --

CMPT 354: Database I -- ER Design (1) 12

Participation of an Entity Set• Total participation (indicated by double line): every entity in

the entity set participates in at least one relationship in the relationship set– E.g., participation of loan in borrower is total: every loan must have

a customer associated to it via borrower

• Partial participation: some entities may not participate in any relationship in the relationship set– Example: participation of customer in borrower is partial

Page 13: ER Design (1) - Computing Science - Simon Fraser University1).pdf · • How to annotate cardinality constraints in ER diagram? ... the enterprise being modeled ... Database I --

CMPT 354: Database I -- ER Design (1) 13

Entity Sets or Attributes

• The structure of the enterprise being modeled

• The semantics associated with the attribute in question

Page 14: ER Design (1) - Computing Science - Simon Fraser University1).pdf · • How to annotate cardinality constraints in ER diagram? ... the enterprise being modeled ... Database I --

CMPT 354: Database I -- ER Design (1) 14

Entity Sets or Relationship Sets

• Designate a relationship set to describe an action that occurs between entities

Page 15: ER Design (1) - Computing Science - Simon Fraser University1).pdf · • How to annotate cardinality constraints in ER diagram? ... the enterprise being modeled ... Database I --

CMPT 354: Database I -- ER Design (1) 15

A Ternary Relationship

Page 16: ER Design (1) - Computing Science - Simon Fraser University1).pdf · • How to annotate cardinality constraints in ER diagram? ... the enterprise being modeled ... Database I --

CMPT 354: Database I -- ER Design (1) 16

Cardinality Constraints

• At most one arrow out of a ternary (or greater degree) relationship to indicate a cardinality constraint– An arrow from works_on to job indicates each employee

works on at most one job at any branch• Confusion with more than one arrow

– Each A entity is associated with a unique entity from B and C (i.e., a combination of B and C), or

– 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

A

B C

Page 17: ER Design (1) - Computing Science - Simon Fraser University1).pdf · • How to annotate cardinality constraints in ER diagram? ... the enterprise being modeled ... Database I --

CMPT 354: Database I -- ER Design (1) 17

Binary vs. Non-Binary Relationships

• Some non-binary relationships may be better represented using binary relationships– E.g. A ternary relationship parents, relating a

child to his/her father and mother, is best replaced by two binary relationships, father and mother

• Using two binary relationships allows partial information (e.g. only mother being know)

• But there are some relationships that are naturally non-binary, e.g., works_on

Page 18: ER Design (1) - Computing Science - Simon Fraser University1).pdf · • How to annotate cardinality constraints in ER diagram? ... the enterprise being modeled ... Database I --

CMPT 354: Database I -- ER Design (1) 18

Converting Non-Binary Relationships

• A non-binary relationship can be represented using binary relationships by creating an artificial entity set– Replace R between entity sets A, B and C by an

entity set E, and three relationship sets:

Page 19: ER Design (1) - Computing Science - Simon Fraser University1).pdf · • How to annotate cardinality constraints in ER diagram? ... the enterprise being modeled ... Database I --

CMPT 354: Database I -- ER Design (1) 19

Summary

• Roles• Cardinality constraints• Participation• Choice of entity sets attributes, or

relationship sets• Binary and non-binary relationships

Page 20: ER Design (1) - Computing Science - Simon Fraser University1).pdf · • How to annotate cardinality constraints in ER diagram? ... the enterprise being modeled ... Database I --

CMPT 354: Database I -- ER Design (1) 20

To-Do-List

• Can you think about some situations where the facilities of ER design we have so far are still insufficient to model the application data?