Data modeling using the entity-relationship model Sept. 2012Yangjun Chen ACS-39021 Outline: Data...

45
Sept. 2012 Yangjun Chen ACS -3902 1 Data modeling using the entity-relationship model Outline: Data modeling using ER- model (Chapter 3 - 3rd, 4th, 5th ed.) • Data modeling • Entity-relationship model - Entity types - strong entities - weak entities - Relationships among entities - Attributes - attribute classification - Constraints - cardinality constraints - participation constraints • ER-to-Relation-mapping
  • date post

    19-Dec-2015
  • Category

    Documents

  • view

    214
  • download

    0

Transcript of Data modeling using the entity-relationship model Sept. 2012Yangjun Chen ACS-39021 Outline: Data...

Page 1: Data modeling using the entity-relationship model Sept. 2012Yangjun Chen ACS-39021 Outline: Data modeling using ER-model (Chapter 3 - 3rd, 4th, 5th ed.)

Sept. 2012 Yangjun Chen ACS-3902 1

Data modeling using the entity-relationship model

Outline: Data modeling using ER-model

(Chapter 3 - 3rd, 4th, 5th ed.)

• Data modeling • Entity-relationship model

- Entity types- strong entities- weak entities

- Relationships among entities- Attributes - attribute classification- Constraints

- cardinality constraints- participation constraints

• ER-to-Relation-mapping

Page 2: Data modeling using the entity-relationship model Sept. 2012Yangjun Chen ACS-39021 Outline: Data modeling using ER-model (Chapter 3 - 3rd, 4th, 5th ed.)

Sept. 2012 Yangjun Chen ACS-3902 2

Data modeling using the entity-relationship model

Entity-relationship model (ER model)

P. 41-2: “This model and its variations are frequently used for the conceptual design of database applications, and many database design tools employ its concepts.”

ER model:• is used to create a conceptual data model that reflects all the user data requirements. •It includes detailed descriptions of

•entity types, •relationships, and •constraints

•no implementation details so can be used for communication with non-technical users

Page 3: Data modeling using the entity-relationship model Sept. 2012Yangjun Chen ACS-39021 Outline: Data modeling using ER-model (Chapter 3 - 3rd, 4th, 5th ed.)

Sept. 2012 Yangjun Chen ACS-3902 3

Data modeling using the entity-relationship model

Example

The company database keeps track of a company’s employees, departments, and projects:

Requirements:

concerning the department:1. company is organized into departments2. a department has a unique name, a unique number, and a specific

employee is its’ manager3. we track the start date for the manager function4. a department may be in several locations5. a department controls a number of projects

concerning the project:6. a project has a unique name, a unique number, and is in a single

location

Page 4: Data modeling using the entity-relationship model Sept. 2012Yangjun Chen ACS-39021 Outline: Data modeling using ER-model (Chapter 3 - 3rd, 4th, 5th ed.)

Sept. 2012 Yangjun Chen ACS-3902 4

Data modeling using the entity-relationship model

example continued

concerning the employee:7. each employee has a name, social insurance number,

address, salary, sex, and birth date8. an employee is assigned to one department but may work on several

projects which are not necessarily controlled by the same department9. we track the number of hours per week that an employee works on

each project10. we keep track of the direct supervisor of each employee11. we track the dependents of each employee (for insurance purposes)

concerning the dependent:12. we record each dependent’s first name, sex, birth date,

and relationship to the employee

Page 5: Data modeling using the entity-relationship model Sept. 2012Yangjun Chen ACS-39021 Outline: Data modeling using ER-model (Chapter 3 - 3rd, 4th, 5th ed.)

Sept. 2012 Yangjun Chen ACS-3902 5

Data modeling using the entity-relationship model

The entities:

employee

department

project

dependent

Page 6: Data modeling using the entity-relationship model Sept. 2012Yangjun Chen ACS-39021 Outline: Data modeling using ER-model (Chapter 3 - 3rd, 4th, 5th ed.)

Sept. 2012 Yangjun Chen ACS-3902 6

Data modeling using the entity-relationship model

The entities:

bdate

ssn

name

lnameminitfname

sex address salary

birthdatename sex relationship

name number location

name number location

employee

department

project

dependent

Page 7: Data modeling using the entity-relationship model Sept. 2012Yangjun Chen ACS-39021 Outline: Data modeling using ER-model (Chapter 3 - 3rd, 4th, 5th ed.)

Sept. 2012 Yangjun Chen ACS-3902 7

Data modeling using the entity-relationship model

With relationships:

employee

department

project

dependent

works for

manages

works on

dependents of

controls

supervision 1

N

N

1N M

1

N

1

1

N

1

supervisorsupervisee

partial constrainttotal constraint

Page 8: Data modeling using the entity-relationship model Sept. 2012Yangjun Chen ACS-39021 Outline: Data modeling using ER-model (Chapter 3 - 3rd, 4th, 5th ed.)

Sept. 2012 Yangjun Chen ACS-3902 8

Data modeling using the entity-relationship model

employee

department

project

dependent

With attributes, etc:

works for

manages

works on

dependents of

controls

supervision

bdate

ssn

name

lnameminitfname

sex address salary

birthdatename sex relationship

name number location

name number location

number ofemployeesstartdate

hours

1

1

1

N

supervisor

supervisee

Figure 3.2

P.46

M

N

11

NNdegree

1 N

Page 9: Data modeling using the entity-relationship model Sept. 2012Yangjun Chen ACS-39021 Outline: Data modeling using ER-model (Chapter 3 - 3rd, 4th, 5th ed.)

Sept. 2012 Yangjun Chen ACS-3902 9

Data modeling using the entity-relationship model

Let’s consider a university environment:

At a high level, we need to:

•offer courses

•register students

•assign instructors to courses

•assign grades

•anything else?

Page 10: Data modeling using the entity-relationship model Sept. 2012Yangjun Chen ACS-39021 Outline: Data modeling using ER-model (Chapter 3 - 3rd, 4th, 5th ed.)

Sept. 2012 Yangjun Chen ACS-3902 10

Data modeling using the entity-relationship model

Concepts (you should understand)

Attribute classification

•simple attribute

•multivalued attribute

•composite attribute

•complex (nested) attributes

Attribute storage

•stored & derived attribute

•null values

not applicable, unknown, missing

•key attribute

•domain

Page 11: Data modeling using the entity-relationship model Sept. 2012Yangjun Chen ACS-39021 Outline: Data modeling using ER-model (Chapter 3 - 3rd, 4th, 5th ed.)

Sept. 2012 Yangjun Chen ACS-3902 11

Data modeling using the entity-relationship model

Relationships

•degree of a relationship

•recursive relationship

•role names

•constraints

cardinality: m-n, 1-n, 1-1

participation: partial, total (existence dependency)

Concepts (you should understand)

Page 12: Data modeling using the entity-relationship model Sept. 2012Yangjun Chen ACS-39021 Outline: Data modeling using ER-model (Chapter 3 - 3rd, 4th, 5th ed.)

Sept. 2012 Yangjun Chen ACS-3902 12

Data modeling using the entity-relationship model

Entities

•entity type•strong entity

- key attribute

- entity has a key attribute or a combination of attributes which can be used as a key.

•weak entity

No key attributes. Entities belonging to a weak entity type are identified by being related to specific entities from another entity type in combination with some of their attribute values.

- identifying owner- identifying relationship- partial key

Page 13: Data modeling using the entity-relationship model Sept. 2012Yangjun Chen ACS-39021 Outline: Data modeling using ER-model (Chapter 3 - 3rd, 4th, 5th ed.)

Sept. 2012 Yangjun Chen ACS-3902 13

Data modeling using the entity-relationship model

Instructors: let’s assume this classification includes instructors, professors, part-time people (at least for now). These people have SINs, employee numbers, names, addresses, offices, phones, ...

instructor

SIN

office

address

employee no

name

phonedegree

Page 14: Data modeling using the entity-relationship model Sept. 2012Yangjun Chen ACS-39021 Outline: Data modeling using ER-model (Chapter 3 - 3rd, 4th, 5th ed.)

Sept. 2012 Yangjun Chen ACS-3902 14

Data modeling using the entity-relationship model

instructor

SIN

office

address

employee no

name

phone

Is there a key attribute? What are the domains? Can any attribute be null? Is any attribute composite, derived, complex, multivalued?

Is this a weak entity or a strong entity?

Should department be an attribute?

degree

Page 15: Data modeling using the entity-relationship model Sept. 2012Yangjun Chen ACS-39021 Outline: Data modeling using ER-model (Chapter 3 - 3rd, 4th, 5th ed.)

Sept. 2012 Yangjun Chen ACS-3902 15

Data modeling using the entity-relationship model

Departments: obviously instructors are employed by the University and associated with a department

A department has a name, number, office, chair, ...

department

Dnumber

chair

phone

name

office

Page 16: Data modeling using the entity-relationship model Sept. 2012Yangjun Chen ACS-39021 Outline: Data modeling using ER-model (Chapter 3 - 3rd, 4th, 5th ed.)

Sept. 2012 Yangjun Chen ACS-3902 16

Data modeling using the entity-relationship model

department

Dnumber

chair

phone

name

office

Should chair be an attribute, or is there a relationship between two entity types?

Is there a key attribute? What are the domains? Can any attribute be null? Is any attribute composite, derived, complex, multivalued?

Page 17: Data modeling using the entity-relationship model Sept. 2012Yangjun Chen ACS-39021 Outline: Data modeling using ER-model (Chapter 3 - 3rd, 4th, 5th ed.)

Sept. 2012 Yangjun Chen ACS-3902 17

Data modeling using the entity-relationship model

department

Employs relationship: If we assume the relationship between department and instructor is 1:N then we only associate each department with a single instructor, but we associate any number of instructors with a single department

instructoremploys1 N

1:N is the cardinality of the relationship

the relationship is of degree 2; it is a binary relationship

Both entities are considered strong entities

Page 18: Data modeling using the entity-relationship model Sept. 2012Yangjun Chen ACS-39021 Outline: Data modeling using ER-model (Chapter 3 - 3rd, 4th, 5th ed.)

Sept. 2012 Yangjun Chen ACS-3902 18

Data modeling using the entity-relationship model

Consider some instances

department employs instructor

d1

d2

d3

e1e2e3e4e5e6e7

Page 19: Data modeling using the entity-relationship model Sept. 2012Yangjun Chen ACS-39021 Outline: Data modeling using ER-model (Chapter 3 - 3rd, 4th, 5th ed.)

Sept. 2012 Yangjun Chen ACS-3902 19

Data modeling using the entity-relationship model

department

Chair relationship: A department has a chair who has special responsibilities. One person (instructor) is designated as such.

instructorchair1 1

1:1 is the cardinality of the relationship

the relationship is of degree 2; it is a binary relationship.

Page 20: Data modeling using the entity-relationship model Sept. 2012Yangjun Chen ACS-39021 Outline: Data modeling using ER-model (Chapter 3 - 3rd, 4th, 5th ed.)

Sept. 2012 Yangjun Chen ACS-3902 20

Data modeling using the entity-relationship model

Continuing the discussion on data modeling

•Weak entity again

•Data analysis (at entity instance level)

•Mapping ER to relational database schema

•ER model comparison

Page 21: Data modeling using the entity-relationship model Sept. 2012Yangjun Chen ACS-39021 Outline: Data modeling using ER-model (Chapter 3 - 3rd, 4th, 5th ed.)

Sept. 2012 Yangjun Chen ACS-3902 21

Data modeling using the entity-relationship model

Weak entity types

a weak entity does not have a key of its own - may have a partial key

the identifying relationship will have total participation for the weak entity

e.g. consider courses and sections at UWinnipeg

Page 22: Data modeling using the entity-relationship model Sept. 2012Yangjun Chen ACS-39021 Outline: Data modeling using ER-model (Chapter 3 - 3rd, 4th, 5th ed.)

Sept. 2012 Yangjun Chen ACS-3902 22

Data modeling using the entity-relationship model

Consider courses and course sections

In the fall and winter we have:

91.1453/3-001 F Intro Computers staff MW 16:30-17:45 3C13 ..

91.1453/3-002 W Intro Computers staff MW 16:30-17:45 3C13 ..

91.1453/3-050 F Intro Computers staff T 18:00-21:00 3C13 ..

91.1453/3-051 W Intro Computers staff T 18:00-21:00 3C13 ..

Section numbers are 001, 002, 050, 051, …

Sections have a section number, a term, days and times, …

Page 23: Data modeling using the entity-relationship model Sept. 2012Yangjun Chen ACS-39021 Outline: Data modeling using ER-model (Chapter 3 - 3rd, 4th, 5th ed.)

Sept. 2012 Yangjun Chen ACS-3902 23

Data modeling using the entity-relationship model

Consider courses and course sections

course 1 NOffered-in

Section noterm

meeting

Section is a weak entity - it has a discriminator (partial key), section number.

Section totally participates in the offered in relationship

PK (primary key) of Section is … (offered_in is an identifying relationship)

Is meeting multivalued?

course noname

credit hoursdescription

section

Page 24: Data modeling using the entity-relationship model Sept. 2012Yangjun Chen ACS-39021 Outline: Data modeling using ER-model (Chapter 3 - 3rd, 4th, 5th ed.)

Sept. 2012 Yangjun Chen ACS-3902 24

Data modeling using the entity-relationship model

Data analysis:

instructor course

textbook uses

teachesm

mn

n

Note that teaches and uses are both binary relationships:

•we expect situations where a specific Instructor teaches a specific Course, and where

•a specific Course uses a specific text

Page 25: Data modeling using the entity-relationship model Sept. 2012Yangjun Chen ACS-39021 Outline: Data modeling using ER-model (Chapter 3 - 3rd, 4th, 5th ed.)

Sept. 2012 Yangjun Chen ACS-3902 25

Data modeling using the entity-relationship model

Consider instances

instructor course textbook

Page 26: Data modeling using the entity-relationship model Sept. 2012Yangjun Chen ACS-39021 Outline: Data modeling using ER-model (Chapter 3 - 3rd, 4th, 5th ed.)

Sept. 2012 Yangjun Chen ACS-3902 26

Data modeling using the entity-relationship model

Jones and Smith

are Instructors

instructor course

Courses offered are Intro to X, Intro to Y, Z, Advanced X, Advanced Y

jones

smith

Intro to X

Z

Intro to Y

Advanced X

Advanced Y

Page 27: Data modeling using the entity-relationship model Sept. 2012Yangjun Chen ACS-39021 Outline: Data modeling using ER-model (Chapter 3 - 3rd, 4th, 5th ed.)

Sept. 2012 Yangjun Chen ACS-3902 27

Data modeling using the entity-relationship model

instructor teaches course

•Jones teaches Intro to Xjones

smith

Intro to X

Z

Intro to Y

Advanced X

Advanced Y

Consider instances of Instructor teaches Course

Page 28: Data modeling using the entity-relationship model Sept. 2012Yangjun Chen ACS-39021 Outline: Data modeling using ER-model (Chapter 3 - 3rd, 4th, 5th ed.)

Sept. 2012 Yangjun Chen ACS-3902 28

Data modeling using the entity-relationship model

instructor teaches course

•Jones teaches Intro to Xjones

smith

Intro to X

Z

Intro to Y

Advanced X

Advanced Y

There is a relationship between Instructor Jones and Course Intro to X

Page 29: Data modeling using the entity-relationship model Sept. 2012Yangjun Chen ACS-39021 Outline: Data modeling using ER-model (Chapter 3 - 3rd, 4th, 5th ed.)

Sept. 2012 Yangjun Chen ACS-3902 29

Data modeling using the entity-relationship model

instructor teaches course

•Jones teaches Intro to Xjones

smith

Intro to X

Z

Intro to Y

Advanced X

Advanced Y

This line connects the three dots

Page 30: Data modeling using the entity-relationship model Sept. 2012Yangjun Chen ACS-39021 Outline: Data modeling using ER-model (Chapter 3 - 3rd, 4th, 5th ed.)

Sept. 2012 Yangjun Chen ACS-3902 30

Data modeling using the entity-relationship model

instructor teaches course

•Jones teaches Intro to Xjones

smith

Intro to X

Z

Intro to Y

Advanced X

Advanced Y

•Jones teaches Advanced X

Page 31: Data modeling using the entity-relationship model Sept. 2012Yangjun Chen ACS-39021 Outline: Data modeling using ER-model (Chapter 3 - 3rd, 4th, 5th ed.)

Sept. 2012 Yangjun Chen ACS-3902 31

Data modeling using the entity-relationship model

instructor teaches course

•Jones teaches Intro to Xjones

smith

Intro to X

Z

Intro to Y

Advanced X

Advanced Y

•Jones teaches Advanced X

•Smith teaches Intro to Y

Page 32: Data modeling using the entity-relationship model Sept. 2012Yangjun Chen ACS-39021 Outline: Data modeling using ER-model (Chapter 3 - 3rd, 4th, 5th ed.)

Sept. 2012 Yangjun Chen ACS-3902 32

Data modeling using the entity-relationship model

instructor teaches course

•Jones teaches Intro to Xjones

smith

Intro to X

Z

Intro to Y

Advanced X

Advanced Y

•Jones teaches Advanced X

•Smith teaches Intro to Y

•Smith teaches Advanced Y

Page 33: Data modeling using the entity-relationship model Sept. 2012Yangjun Chen ACS-39021 Outline: Data modeling using ER-model (Chapter 3 - 3rd, 4th, 5th ed.)

Sept. 2012 Yangjun Chen ACS-3902 33

Data modeling using the entity-relationship model

instructor teaches course

•Jones teaches Intro to Xjones

smith

Intro to X

Z

Intro to Y

Advanced X

Advanced Y

•Jones teaches Advanced X

•Smith teaches Intro to Y

•Smith teaches Advanced Y

•Smith and Jones teach Z together

There are two relationships: one between Jones and Z; the other between Smith and Z

Page 34: Data modeling using the entity-relationship model Sept. 2012Yangjun Chen ACS-39021 Outline: Data modeling using ER-model (Chapter 3 - 3rd, 4th, 5th ed.)

Sept. 2012 Yangjun Chen ACS-3902 34

Data modeling using the entity-relationship model

instructor teaches course uses textbook

jones

smith

Intro to X

Z

Intro to Y

Advanced X

Advanced Y

UML distilled

The mythical man-month

Now let us examine Course uses Textbook

Suppose we have two textbooks: The mythical man-month, and UML distilled

Page 35: Data modeling using the entity-relationship model Sept. 2012Yangjun Chen ACS-39021 Outline: Data modeling using ER-model (Chapter 3 - 3rd, 4th, 5th ed.)

Sept. 2012 Yangjun Chen ACS-3902 35

Data modeling using the entity-relationship model

instructor teaches course uses textbook

jones

smith

Intro to X

Z

Intro to Y

Advanced X

Advanced Y

UML distilled

The mythical man-month

Intro to Y uses The mythical man-month

Page 36: Data modeling using the entity-relationship model Sept. 2012Yangjun Chen ACS-39021 Outline: Data modeling using ER-model (Chapter 3 - 3rd, 4th, 5th ed.)

Sept. 2012 Yangjun Chen ACS-3902 36

Data modeling using the entity-relationship model

instructor teaches course uses textbook

jones

smith

Intro to X

Z

Intro to Y

Advanced X

Advanced Y

UML distilled

The mythical man-month

Z uses both texts

Page 37: Data modeling using the entity-relationship model Sept. 2012Yangjun Chen ACS-39021 Outline: Data modeling using ER-model (Chapter 3 - 3rd, 4th, 5th ed.)

Sept. 2012 Yangjun Chen ACS-3902 37

Data modeling using the entity-relationship model

Let’s jump ahead to see what relations we eventually create

Section 9.1: ER-to-Relational mapping

1. Create a relation for each strong entity type

2. Create a relation for each weak entity type•include primary key of owner (an FK - foreign key)•owner’s PK + partial key becomes PK

3. For each binary 1:1 relationship choose an entity and include the other’s PK in it as an FK. Include any

attributes of the relationship

4. For each binary 1:n relationship, choose the n-side entity and include an FK w.r.t the other entity. Include any

attributes of the relationship

Page 38: Data modeling using the entity-relationship model Sept. 2012Yangjun Chen ACS-39021 Outline: Data modeling using ER-model (Chapter 3 - 3rd, 4th, 5th ed.)

Sept. 2012 Yangjun Chen ACS-3902 38

Data modeling using the entity-relationship model

5. For each binary M:N relationship, create a relation for the relationship

•include PKs of both participating entities and any attributes of the relationship•PK is the concatenation of the participating entity PKs

6. For each multivalued attribute create a new relation•include the PK attributes of the entity type•PK is the PK of the entity type and the multivalued attribute

7. For each n-ary relationship, create a relation for the relationship

•include PKs of all participating entities and any attributes of the relationship•PK may be the concatenation of the participating entity PKs

Page 39: Data modeling using the entity-relationship model Sept. 2012Yangjun Chen ACS-39021 Outline: Data modeling using ER-model (Chapter 3 - 3rd, 4th, 5th ed.)

Sept. 2012 Yangjun Chen ACS-3902 39

Data modeling using the entity-relationship model

employee

department

project

dependent

works for

manages

works on

dependents of

controls

supervision 1

N

N

1N M

1

N

1

1

N

1

supervisorsupervisee

Page 40: Data modeling using the entity-relationship model Sept. 2012Yangjun Chen ACS-39021 Outline: Data modeling using ER-model (Chapter 3 - 3rd, 4th, 5th ed.)

Sept. 2012 Yangjun Chen ACS-3902 40

Data modeling using the entity-relationship model

division

group

c composed of

div composed of

dep composed of department

1

1

1n

n

n

Consider the following requirements (very typical situation)

A company may be composed of Divisions, each of which may be composed of Departments, each of which may be composed of Groups.

We could model this as follows:

company

Page 41: Data modeling using the entity-relationship model Sept. 2012Yangjun Chen ACS-39021 Outline: Data modeling using ER-model (Chapter 3 - 3rd, 4th, 5th ed.)

Sept. 2012 Yangjun Chen ACS-3902 41

Data modeling using the entity-relationship model

company

division

group

c composed of

div composed of

dep composed of department

1

1

1n

n

n

It fits the requirements, but•it is not flexible•it does not accommodate any new groupings•it does not allow the company to grow/change•it is very rigid => it is a static model

Page 42: Data modeling using the entity-relationship model Sept. 2012Yangjun Chen ACS-39021 Outline: Data modeling using ER-model (Chapter 3 - 3rd, 4th, 5th ed.)

Sept. 2012 Yangjun Chen ACS-3902 42

Data modeling using the entity-relationship model

Consider the following model•it is flexible•it does accommodate any new groupings•it does allow the company to grow/change•it is not rigid => it is a dynamic model•but not all constraints are implied in the model => we would need supporting application code. What are these constraints?

lower_level

higher_levelm

n

organization

composed of

higher_level

Page 43: Data modeling using the entity-relationship model Sept. 2012Yangjun Chen ACS-39021 Outline: Data modeling using ER-model (Chapter 3 - 3rd, 4th, 5th ed.)

Sept. 2012 Yangjun Chen ACS-3902 43

Data modeling using the entity-relationship model

Consider the following model•it is flexible, it does accommodate any new groupings, it does allow the company to grow/change, it is a dynamic model again•not all constraints are implied in the model => we would need supporting application code. What are these constraints?

m

n

organization

composed of

organizationtype

example of

org composed of

m

n

m n

lower_level

higher_level

higher_levellower_level

Constraint-des

Page 44: Data modeling using the entity-relationship model Sept. 2012Yangjun Chen ACS-39021 Outline: Data modeling using ER-model (Chapter 3 - 3rd, 4th, 5th ed.)

Sept. 2012 Yangjun Chen ACS-3902 44

Data modeling using the entity-relationship model

organizationorg-nameDellsell-division

... ...

... ...

... ...... ...

Composed-oforgDellDell

sub-orgSell-division

... ...Production-division

Example-oforgDellsell-division

org-typecompany

... ...division

organization-typeorg-typecompanydivision

... ...

org-composed-oforg-typecompanydivision

... ...

sub-org-typedivisiondepartment

constraint1:n1:n

Page 45: Data modeling using the entity-relationship model Sept. 2012Yangjun Chen ACS-39021 Outline: Data modeling using ER-model (Chapter 3 - 3rd, 4th, 5th ed.)

Sept. 2012 Yangjun Chen ACS-3902 45

Data modeling using the entity-relationship model

Other common situations:

•How would we model bill-of-materials?

•How would we model reporting relationships?

•How would we model geographical relationships?

•How would we model procedures and activities?

•How would we model purchase orders?

•How would we model documents? … with indexes? … with versions?