chapter 5 understanding entity relationship diagrams

34
Chapter 5 Chapter 5 Understanding Entity Relationship Diagrams

Upload: nelson-carroll

Post on 18-Dec-2015

249 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Chapter 5 Understanding Entity Relationship Diagrams

Chapter 5Chapter 5Understanding Entity Relationship Diagrams

Page 2: Chapter 5 Understanding Entity Relationship Diagrams

McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved.

Outline Outline

Notation basicsUnderstanding relationshipsGeneralization hierarchiesDiagram rules

Page 3: Chapter 5 Understanding Entity Relationship Diagrams

McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved.

Basic SymbolsBasic Symbols

Entity Typesymbol Relationship

symbol

Primary Key

AttributesRelationship

name

Entity Typename

CourseNoCrsDescCrsUnits

Course

OfferNoOffLocationOffTime

Offering

Has

Page 4: Chapter 5 Understanding Entity Relationship Diagrams

McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved.

CardinalitiesCardinalities

Course Offering

Course1 Offering1

Course2

Course3

Offering2

Offering3

Offering4

Page 5: Chapter 5 Understanding Entity Relationship Diagrams

McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved.

Cardinality NotationCardinality Notation

Inside symbol:minimum cardinality

CourseNoCrsDescCrsUnits

Course

OfferNoOffLocationOffTime

Offering

Has

Perpendicular line:one cardinality

Outside symbol:maximum cardinality

Circle: zerocardinality

Crow's foot: manycardinality

Page 6: Chapter 5 Understanding Entity Relationship Diagrams

McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved.

Classification of CardinalitiesClassification of Cardinalities

Minimum cardinality based– Mandatory: existence dependent– Optional

Maximum cardinality based– Functional– 1-M– M-N– 1-1

Page 7: Chapter 5 Understanding Entity Relationship Diagrams

McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved.

Summary of CardinalitiesSummary of CardinalitiesClassification Cardinality Restrictions

Mandatory Minimum cardinality 1

Optional Minimum cardinality = 0

Functional or single-valued

Maximum cardinality = 1

1-M Maximum cardinality = 1 in one direction and Maximum cardinality > 1 in the other direction.

M-N Maximum cardinality is > 1 in both directions.

1-1 Maximum cardinality = 1 in both directions.

Page 8: Chapter 5 Understanding Entity Relationship Diagrams

McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved.

More Relationship ExamplesMore Relationship Examples

FacSSNFacSalaryFacRankFacHireDate

Faculty

OfferNoOffLocationOffTime

Offering

TeamTeachesOfficeNoOffPhoneOffType

Office

WorksIn

Page 9: Chapter 5 Understanding Entity Relationship Diagrams

McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved.

Comparison to Access NotationComparison to Access Notation

CourseNoCrsDescCrsUnits

Course

OfferNoOffLocationOffTime

Offering

Has

ERD (Crow's Foot)

CourseCoursenoCrsDescCrsUnits

1 8OfferingOfferNo

CourseNoOffLocation

OffTime...

Access Relationship Diagram

Page 10: Chapter 5 Understanding Entity Relationship Diagrams

McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved.

Understanding RelationshipsUnderstanding Relationships

Identification dependencyM-N relationships with attributesSelf identifying relationshipsM-way relationshipsEquivalence between M-N and 1-M

relationships

Page 11: Chapter 5 Understanding Entity Relationship Diagrams

McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved.

Identification DependencyIdentification Dependency

BldgIDBldgNameBldgLocation

Building

RoomNoRoomCapacity

Room

Contains

Identification Dependency Symbols: Solid relationship line for identifying

relationships Diagonal lines in the corners denote

weak entities.

Page 12: Chapter 5 Understanding Entity Relationship Diagrams

McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved.

M-N Relationships M-N Relationships with Attributeswith Attributes

StdSSNStdName

StudentOfferNoOffLocationOffTime

Offering

EnrollsIn

EnrGrade

attribute of relationship

Page 13: Chapter 5 Understanding Entity Relationship Diagrams

McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved.

M-N Relationships M-N Relationships with Attributes (II)with Attributes (II)

AuthNoAuthName

AuthorISBNTitle

Book

AuthOrder

b) Writes relationship

Writes

PartNoPartName

PartSuppNoSuppName

Supplier

Qty

Provides

a) Provides relationship

Page 14: Chapter 5 Understanding Entity Relationship Diagrams

McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved.

Instance Diagrams for Self-Instance Diagrams for Self-Referencing RelationshipsReferencing Relationships

Faculty1

Faculty2 Faculty3

Faculty4 Faculty5

IS300

IS320

IS480 IS460

IS461

(a) Supervises (b) PreReqTo

Page 15: Chapter 5 Understanding Entity Relationship Diagrams

McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved.

ERD Notation for Self-ERD Notation for Self-Referencing RelationshipsReferencing Relationships

FacSSNFacName

Faculty

a) manager-subordinate

Supervises

b) course prerequisites

CourseNoCrsDesc

Course PrereqTo

Page 16: Chapter 5 Understanding Entity Relationship Diagrams

McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved.

Associative Entity Types for Associative Entity Types for M-way RelationshipsM-way Relationships

PartNoPartName

PartSuppNoSuppName

Supplier

Associativeentity type

ProjNoProjName

Project

UsesPart-Uses

Supp-Uses

Proj-Uses

Page 17: Chapter 5 Understanding Entity Relationship Diagrams

McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved.

Relationship EquivalenceRelationship Equivalence

Replace M-N relationship – Associative entity type– Two identifying 1-M relationships

M-N relationship versus associative entity type– Largely preference– Associative entity type is more flexible in some

situations

Page 18: Chapter 5 Understanding Entity Relationship Diagrams

McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved.

Associative Entity Type ExampleAssociative Entity Type Example

StdSSNStdName

StudentOfferNoOffLocationOffTime

Offering

EnrGrade

EnrollmentRegisters GrantsAttDatePresent

Attendance

RecordedFor

Page 19: Chapter 5 Understanding Entity Relationship Diagrams

McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved.

Generalization HierarchiesGeneralization Hierarchies

EmployeeEmpNo

EmpNameEmpHireDate

...

SalaryEmpEmpSalary

HourlyEmpEmpRate

generalization hierarchysymbol

supertype

subtypes

Page 20: Chapter 5 Understanding Entity Relationship Diagrams

McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved.

InheritanceInheritance

Subtypes inherit attributes of supertypes (direct and indirect)

Allows abbreviation of attribute listApplies to code (methods) as well as

attributes (data)

Page 21: Chapter 5 Understanding Entity Relationship Diagrams

McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved.

Generalization ConstraintsGeneralization Constraints

BondRate

FaceValue

StockOutShares

IssuedShares

SecuritySymbol

SecNameLastClose

D,C

DisjointnessConstraint

CompletenessConstraint

Page 22: Chapter 5 Understanding Entity Relationship Diagrams

McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved.

Multiple Levels of Multiple Levels of GeneralizationGeneralization

SecuritySymbol

SecNameLastClose

StockOutShares

IssuedShares

BondRate

FaceValue

D,C

CommonPERatioDividend

PreferredCallPriceArrears

D,C

Page 23: Chapter 5 Understanding Entity Relationship Diagrams

McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved.

Comprehensive ExampleComprehensive Example

OfferingOfferNoOffLocationOffTime

EnrGrade

EnrollmentRegisters

Grants

CourseNoCrsDescCrsUnits

Course

FacultyFacSalaryFacRankFacHireHate

SSNNameCityStateZip

UnivPerson

Has

Teaches

Supervises

CStudentStdClassStdMajorStdGPA

Page 24: Chapter 5 Understanding Entity Relationship Diagrams

McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved.

Diagram RulesDiagram Rules

Ensure that ERD notation is correctly usedSimilar to syntax rules for a computer

languageCompleteness rules: no missing

specificationsConsistency rules: no conflicts among

specificationsSupported by the ER Assistant

Page 25: Chapter 5 Understanding Entity Relationship Diagrams

McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved.

Completeness RulesCompleteness Rules Primary Key Rule: all entity types have a PK (direct,

indirect, or inherited) Naming Rule: all entity types, relationships, and

attributes have a name Cardinality Rule: cardinality is specified in both

directions for each relationship Entity Participation Rule: all entity types participate in an

at least one relationship except for entity types in a generalization hierarchy

Generalization Hierarchy Participation Rule: at least one entity type in a generalization hierarchy participates in a relationship

Page 26: Chapter 5 Understanding Entity Relationship Diagrams

McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved.

Primary Key Rule IssuePrimary Key Rule Issue

Primary key rule is simple in most casesFor some weak entities, the PK rule is

subtle

– Weak entity with only one 1-M identifying relationship

– Weak entity must have a local key to augment the borrowed PK from the parent entity type

– Violation of PK rule if local key is missing

Page 27: Chapter 5 Understanding Entity Relationship Diagrams

McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved.

PK Rule Violation ExamplePK Rule Violation Example

BldgIDBldgNameBldgLocation

BuildingRoomRoomNoRoomCapacity

Contains

PK rule violation A single 1-M identifying relationship Room does not have a local key.

Page 28: Chapter 5 Understanding Entity Relationship Diagrams

McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved.

Naming Consistency RulesNaming Consistency Rules

Entity Name Rule: entity type names must be unique

Attribute Name Rule: attribute names must be unique within each entity type and relationship

Inherited Attribute Rule: attribute names in a subtype do not match inherited (direct or indirect) attribute names.

Page 29: Chapter 5 Understanding Entity Relationship Diagrams

McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved.

Connection Consistency RulesConnection Consistency Rules

Relationship/Entity Connection Rule: relationships connect two entity types (not necessarily distinct)

Relationship/Relationship Connection Rule: relationships are not connected to other relationships

Redundant Foreign Key Rule: foreign keys are not used.

Page 30: Chapter 5 Understanding Entity Relationship Diagrams

McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved.

Identification Dependency RulesIdentification Dependency Rules

Weak entity rule: weak entities have at least one identifying relationship

Identifying relationship rule: at least one participating entity type must be weak for each identifying relationship

Identification dependency cardinality rule: the minimum and maximum cardinality must equal 1 for a weak entity in all identifying relationships

Page 31: Chapter 5 Understanding Entity Relationship Diagrams

McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved.

Example of Diagram ErrorsExample of Diagram Errors

OfferingOfferNoOffLocationOffTimeCourseNo

EnrGrade

EnrollmentRegisters Grants

CourseNoCrsDescCrsUnits

Course

FacultyFacSalaryFacRankFacHireHate

SSNNameCityStateZip

UnivPerson

Has

Teaches

Supervises

CStudentStdClassStdMajorStdGPA

Rule 6 Violation(Weak Entity)

Rule 7 Violation(Identifying Relationship)Rule 8 Violation

(Id Dependency Cardinality)

Rule 9 Violation(Redundant FK)

Page 32: Chapter 5 Understanding Entity Relationship Diagrams

McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved.

Corrected ERDCorrected ERD

OfferingOfferNoOffLocationOffTime

EnrGrade

EnrollmentRegisters

Grants

CourseNoCrsDescCrsUnits

Course

FacultyFacSalaryFacRankFacHireHate

SSNNameCityStateZip

UnivPerson

Has

Teaches

Supervises

CStudentStdClassStdMajorStdGPA

Page 33: Chapter 5 Understanding Entity Relationship Diagrams

McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved.

Support in the ER AssistantSupport in the ER Assistant

Version 2 of the ER Assistant supports the diagram rules

Relationship formation rules are supported by diagram construction

Other rules are supported by the Check Diagram feature

For the Redundant Foreign Key rule, the ER Assistant detects FKs that have the same name as the associated PKs

Page 34: Chapter 5 Understanding Entity Relationship Diagrams

McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved.

SummarySummary

Data modeling is an important skillCrow’s Foot ERD notation is widely usedUse notation preciselyUse the diagram rules to ensure structural

consistency and completenessUnderstanding the ERD notation is a

prerequisite to applying the notation on business problems