2.ER-2

15
1 Lecture 2 T h e E n t i t y - R e l a t i o n s h i p M o d e l by Davood Rafiei University of Alberta Fall 2000 2 D a t a b a s e D e s i g n P r o c e s s Real World E-R Modeling Functional Specifications Database Specifications Conceptual Schema Design Mapping to DBMS Data Model Scheme Refinement Physical Design Relational Model Normal Forms Tuning, index selection, ...

description

ER

Transcript of 2.ER-2

Page 1: 2.ER-2

1

Lecture 2

The Entity-Relationship Model

byDavood Rafiei

University of AlbertaFall 2000

2

Database Design Process

Real World

E-R Modeling

FunctionalSpecifications

DatabaseSpecifications

Conceptual SchemaDesign

Mapping to DBMSData Model

Scheme Refinement

Physical Design

Relational Model

Normal Forms

Tuning, index selection, ...

Page 2: 2.ER-2

3

ER Model Overview

� Develped by Peter Chen in the mid 70’s� Used for the design of conceptual schema.

� The “world” is described in terms of • entities• relationships• attributes

� The model is visualized by creating and ER diagram.

4

ER Model Basics

� Entity: a distinguishable object• e.g. person, thing, concept

� Entity set: a set of entities of the same type.� Examples of entity sets:

• students registered at UofA• cars currently registered in Alberta• flights offered by Air Canada

� Graphical representation:

students cars flights

Page 3: 2.ER-2

5

ER Model Basics� Relationship: represents the fact that certain

entities are related to each other.• e.g. John has taken CMPUT 291.

Relationship set: set of relationships of the same type.

Examples of relationship sets:• students enrolled in courses• cars registered to owners• passengers booked on flights

� Graphical representation:

enrolled registered booked

6

ER Model Basics

� Attribute: describes a property of an entity or a relationship.

Attributes of entities - examples• student: id, name, major, …• car: VIN, year, model, …• flight: No, source, destination, …

� Graphical representation:

students cars flights

id

name

major VIN year model No source

dest

Page 4: 2.ER-2

7

ER Model Basics

� Key: an attribute or a collection of attrinutes that uniquely identifies each entity in an entity set.

� Attributes of relationships - examples• student enrolled in a course: year, semester, grade• book on loan: loan date, due date

enrolled loanyear

semester gradeloandate

duedate

8

Examples

pname

budgetpno

sincename

Works_On ProjectsEmployees

sin

Reports_To

name

Employees

super-visor

sin

• Role: the function of an entity set in a relationship set.• Role lables are needed whenwever an entity set hasmultiple functions in a relationship set.

subor-dinate

Page 5: 2.ER-2

9

Constraints and Complications� Key constraints

• in binary relationships:binary relationship types

• in general relationships� Participation constraints� Weak entities� ISA hierarchies� Aggregations

10

Binary Relationship Types: Many-to-Many

� Constraint: none.

Works_On ProjectsEmployees

Works_On ProjectsEmployeesN N

• Each employee can be in relationships with many projects and vice versa.

• Alternative representation

Page 6: 2.ER-2

11

Binary Relationship Types: Many-to-One

� Constraint: each employee works in at most one department.

Works_In DepartmentsEmployees

Works_In DepartmentsEmployeesN 1

• Given an employee, we can uniquely identify the department he/she works in.

• Alternative representation:

12

Binary Relationship Types: One-to-One

� Constraint: each employee can manage at most one department and each department is managed by at most one employee.

Manages DepartmentsEmployees

Manages DepartmentsEmployees1 1

• Each employee can be in relationship with at most one department and vice versa.

• Alternative representation:

Page 7: 2.ER-2

13

Ternary Relationships

� Meaning: Supplier s supplies part p for project r.� Complication: add the Constraint “each part is supplied by a unique supplier for a unique project,”• i.e. each part is in relationship with at most one supplier and one

project.

Supplies ProjectsSuppliers

Parts

14

Participation Constraints� Does every project have a supervisor?

• If so, this is a participation constraint: the participation of Projects in Supervises is said to be total (vs. partial).�

Every pid value in Projects table must appear in a row of the Supervises table (with a non-null sin value!)

namebudget

sincename pname

budgetpid

since

Supervises

since

ProjectsEmployees

sin

Works_On

Page 8: 2.ER-2

15

Weak Entities

� A weak entity can be identified uniquely only by considering the primary key of another (owner) entity.• Owner entity set and weak entity set must participate in a one-to-

many relationship set (one owner, many weak entities).

• Weak entity set must have total participation in this identifying relationship set.

name

agedname

DependentsEmployees

sin

Policy

cost

16

Weak Entities

� Examples:

name

sno

SectionCourse

cid

sec_of

lno

lineInvoice

ino

line_of

Page 9: 2.ER-2

17

Generalization� forming a new entity set as the union of two or

more entity sets.

attributes common to all lower-level entity sets are moved to the higher-level entity set. Contract_Emps

namesin

Employees

hourly_wagesISA

Hourly_Emps

contractid

hours_worked

Contract_Emps and Hourly_Empscover Employees.

18

Specialization

! forming a derived entity set by taking a subset of a given entity set.

Undergrad_students

Students

ISA

major

namesid

" Differences between generalization and specialization: • in generalization, every higher-

level entity must be a lower-level entity too.

• this is not the case in specialization.

Page 10: 2.ER-2

19

ISA Constraints

Contract_Emps

namessn

Employees

hourly_wagesISA

Hourly_Emps

contractid

hours_worked

# Covering constraints: Does every Employees entity also have to be an Hourly_Emps or a Contract_Emps entity?(default:no)

$ Overlap constraints: Can Joe be an Hourly_Emps as well as a Contract_Emps entity? (default:disallowed)

Contract_Emps overlap Hourly_Emps.

Contract_Emps and Hourly_Empscover Employees.

20

ISA Hierarchies (Summary)

% Reasons for using ISA: • To add descriptive attributes specific to a subclass.

• To identify entities that participate in a relationship.

& Constraints:• Covering constraints: generalization vs specialization. • Overlap constraints

Page 11: 2.ER-2

21

Aggregation' Treating a

relationship as a higher-level entity.

( Aggregation allows modeling relationships between relationships.

*)+*,*.-0/1*,243658749;:,/<-=/

v >@?BABCEDGFIH6JKC+LNMPOQHRH6JSMPHTMUFGH=VWAXCZYKHRO4MPYKYS?ZHJTMP[.C\M]O4?ZVI^_F`C\MPOPO4?ZVIYKHRVIYKDaCXFbFcJKCBdcF`JKCe FICZYK^_?ZDfDaCBA e YgM]O4?ZVI^_FhCXi

v jkDGFb?Zl,O,MPYmFnMPopH=JTMPHRCBMPOQJqCZYK^_?ZDfDa>rCEYKHO4MPYsJSMP[RCtMPYmMPOQO,?ZVIYKHNi

CoursesStudents Enrolled

Course-account

Accounts

22

Conceptual Design Using the ER Model

u Design choices:• Should a concept be modeled as an entity or an attribute?• Should a concept be modeled as an entity or a relationship?• Identifying relationships: Binary or ternary? Aggregation?

Page 12: 2.ER-2

23

Entity vs. Attributev Should address be an attribute of Employees or an

entity (connected to Employees by a relationship)?w Depends upon on the use we want to make of

address information and the semantics of the data:• is it an object that we want to keep information about

(independent from employees)? • can an employee have several addresses? • does it make sense to have employees with no addresses?• can several employees share the same address?

x A positive answer to one or more of those questions implies address better be modeled as an entity.

24

Entity vs. Attribute (Contd.)

y Can an employee work in two or more periods?• first diagram: No

• second diagram: Yes

Works_In

from to

Departments

DepartmentsEmployees Works_In

Durationfrom to

Employees

Page 13: 2.ER-2

25

Entity vs. Relationshipz First ER diagram:

a manager gets a separate discretionary budget for each dept.

{ What if a manager gets a discretionary budget that covers all managed depts?• Redundancy of dbudget,

which is stored for each dept managed by the manager.

ManagesEmployees Departments

dbudgetsince

Employees

since

Departments

Mgr_Appts

Manages

dbudgetapptnum

|t}�~`���N�n�4}��Z���`~`���<�<�N~G��~��n���<�`�,�0���}��N�+�������n�Z�n�_�N���4�N�4���

26

Binary vs. Ternary Relationships

� Add the constraint: each part is supplied by a unique supplier.• not possible!

• a key constraint on Parts would also mean each part can only be used in a single project!  Solution:

Supplies ProjectsSuppliers

Parts

Used_in ProjectsPartsSuppliers Supplies

What is the additional constraint here?

qty

qty

Page 14: 2.ER-2

27

Binary vs. Ternary Relationships (Contd.)

¡ Previous example: two binary relationships were better than one ternary relationship.

¢ Consider the same example without the constraint:• No combination of binary relationships is an adequate substitute:£

supplier s “supplies” part p, ¤part p “used_in” project r, and ¥Supplier s “supplies_to” project r

• They do not imply that part p supplied by supplier s is used in project r.

• How do we record qty?

28

Summary of Conceptual Design

¦ Conceptual design follows requirements analysis, • Yields a high-level description of data to be stored

§ ER model popular for conceptual design• Constructs are expressive, close to the way people think

about their applications.¨ Basic constructs: entities, relationships, and

attributes (of entities and relationships).© Some additional constructs: weak entities, ISA

hierarchies, and aggregation.

Page 15: 2.ER-2

29

Summary of ER (Contd.)

ª Constraints play an important role in determining the best database design for an enterprise.• Several kinds of integrity constraints can be expressed in

the ER model.• Some constraints (notably, functional dependencies) cannot

be expressed in the ER model.« ER design is subjective. There are often many ways

to model a given scenario!

30

Exercise 2.3 Consider the following information about a university database:

• Professors have a SIN, a name, an age, a rank, and a research specialty.• Projects have a project number, a sponsor name (e.g. NSERC), a starting date, an

ending date, and a budget.• Graduate students have a SIN, a name, an age, and a degree program (e.g. M.S. or Ph.D.)• Each project is managed by one professor (principal investigator).• Each project is worked on by one or more professors (co-investigators).• Professors can manage and/or work on multiple projects.• Each project is worked on by one or more graduate students (research assistants).• When graduate students work on a project, a professor must supervise their work on the

project. Graduate students can work on multiple projects, in which case they will havea (potentially different) supervisor for each one.

• Departments have a department number, a department name, and a main office.• Departments have a professor (chairman) who runs the department.• Professors work in one or more departments, and for each department that they work in,

a time percentage is associated with their job.

Design an ER diagram ...