Lesson III Entity- Relationship Model - XTEC · Entity-Relationship Model The entity-relationship...

Post on 21-Apr-2020

44 views 3 download

Transcript of Lesson III Entity- Relationship Model - XTEC · Entity-Relationship Model The entity-relationship...

Lesson III

Entity-RelationshipModel

IN THIS LESSON YOU WILL LEARN…

� What a ER diagram is (ERD) and what it is used for.

� To identify the main constructs in a ERD.

� To understand the difference between entities and entity-types and how to represent them.

� Definition of attributes, types and their representation.

� The meaning of relationship between entities and its representation.

� Attributes in a relationship.

� Maximum and minimum cardinality, types and representation.

2

INTRODUCTIONEntity-Relationship Model

� The entity-relationship model (ERM) is an abstract and conceptual representation of data.

� Diagrams created using this process are called entity-relationship diagrams (ERD).

� The ER modelling method was proposed by Peter Chen in 1976.

3

INTRODUCTIONEntity-Relationship Model

Graphical technique to model databases.

Entity-Relationship ModelIt is based on a perception of a real world that consists of a collection of objects called entities and relationships among these objects.

4

ER DIAGRAMS (ERD)Example I

5

Why do we want to draw a model for a part of the real world?

ER DIAGRAMS (ERD)

We’ll start with thesimplest version of ER

and then extend it later on

7

ER DIAGRAMS (ERD)Main Constructs

� The basic ER notation uses three mainconstructs:

– data entities (entity-types)

– attributes

– relationships

8

ER DIAGRAMS (ERD)Exemple II

entity

relationshipattributes

9

DATA ENTITYDefinition

� Entities are the main objects of informationalinterest.

� A person, animal, place, object or event aboutwhich data is to be maintained.

e.g. students, subjects, departments, meetings, ...

o Represents a set or collection of objects in the real world that share the same properties.

10

DATA ENTITYEntity Instance

entity instances

data entity

11

DATA ENTITYEntity Instance

� A particular occurrence of a data entity is called an entity instance or an entityoccurrence .e.g. TEACHER is an entity.

“Xavier Morera” is an entity instance.

BUT…

12

DATA ENTITYRepresentation

� An entity is represented by a rectangle withthe name of the entity-type inside.

TEACHER

STUDENT

DEPARTMENT

SUBJECT

The type of the entity must be written in singular!13

ENTITYData Entity vs Entity Instance

Business

Computing

Modern languages

DEPARTMENT

entityentity instances

14

DATA ENTITYGuideline for Naming Entities

� An entity type name is a singular noun.

� An entity type should be descriptive and specific.

� An entity name should be concise.

� Event entity types should be named for theresult of the event, not the activity or process.

15

ATTRIBUTESDefinition

� Named characteristics or properties of anentity-type.

16

e.g. CAR � number_plate, brand, price, speed...

EMPLOYEE � id, name, address, course...

PRODUCT � code, name, price...

SALE � number, amount, method_payment...

Attributes can only take up a valueat any particular point in time!!

ATTRIBUTESDefinition

� An entity-type is described by a set of attributes that provide details about it.

17

All entity instances of the same type have the same attributes!!

ATTRIBUTESGuideline for Naming Attributes

� An attribute name is a noun.

� An attribute name should be descriptive.

� An attribute name should be unique.

� Similar attributes of different entity types should use similar but distinguishing names.

18

ATTRIBUTESRepresentation

� Attributes can be denoted by an ellipse withthe name of the property inside.

19

ATTRIBUTESRepresentation

� There’s a simplier way to represent attributes.

� We’ll use single lines to connect the entitity to its attributes.

20

Activity 3.1Indentifying Entities and Attributes

Is this entity-type correct?

What’s the difference between an entity and a table?

ATTRIBUTESTypes

� There are two types of attributes:

- descriptors

- identifiers (or keys)

24

ATTRIBUTESDescriptors vs Keys

o A descriptor is used to specify a nonuniquecharacteristic of a particular entity instance.

o An identifier (or key) is used to uniquelydetermine an instance of an entity.

25

KEYDefinition

� A key is an attribute (or combination of attributes) that uniquely identifies each instance of an entity type.

� An entity-type has at least one key.

� Some entities may have more than one key.

26

KEYDefinition

Which attribute/s make/s teachers different?

27

KEY Example

teacher with id=’34’

key

descriptors28

KEYDefinition

� Some entities may have more than one key.

29

key

key

IDENTIFIERDefinition

� A key that has been selected as the unique identifying characteristic for an entity type.

OR…

30

identifier

identifier

IDENTIFIERExamples

31

e.g. An identifier for CAR may be number_plate

An identifier for EMPLOYEE may be id

An identifier for PRODUCT may be code

An identifier for SALE may be number

How many identifiers can an entity have?

IDENTIFIER ATTRIBUTESTo Take Into Account…

� A unique identifier can be an attribute or a combination of attributes.

� Only one identifier has to be chosen for each entity.

33

IDENTIFIER ATTRIBUTESRepresentation

� Each attribute in the key is underlined in theER diagram, as shown:

identifier

descriptors

34

Activity 3.2Choosing Identifiers

Is an identifier a primary key in a table?

RELATIONSHIPSDefinition

� Relationships represent real-world associationsamong instances of one or more entity-types.

e.g. A TEACHER teaches SUBJECTS

A teacher teaches subjects

37

RELATIONSHIPSGuideline for Naming Relationships

� Relationships name should be an active or a passive verb.

teacher subjects

38

A teacher teaches subjects.A subject is taught by teachers.

RELATIONSHIPSVisualization

39

teacher subjects

� A relationship includes entity instances fromeach participating entity-type.

RELATIONSHIPSVisualization

Xavi

Encarna

Joan

Database

Networks

Programming

TEACHER SUBJECT

teaches

40

Xavi

Encarna

Joan

How many instances can be involved in a relationship?

RELATIONSHIPSIntroduction to Cardinality

42

A teacher teaches subjects.A subject can be taught by teachers.

teacher subjects

How many??

RELATIONSHIPSRepresentation

� A relationship is depicted by a diamond on theline that joins the entitity-types together.

43

How many relationships may there be between two entities?

RELATIONSHIPSExample

45

ER DIAGRAMS (ERD)Indentifying the Objects

� An entity normally is the subject or the objectin the sentence.

e.g. A person has a car

A person has a car

� Relationships are usually denoted by verbs.

e.g. A person has a car

46

Activity 3.3Identifying Relationships

How many entity-types can be involved in a relationship?

RELATIONSHIPSDegree

� Degree of a relationship is the number of entity types that participate in it.

–Binary: between two entity types.–Ternary: among three entity types.–Unary: between an entity type.–Quaternary …

49

RELATIONSHIPSDegree

50

Activity 3.4Attributes and Relationships

RELATIONSHIPSAttributes

In what subject ?

123, Steven, Barcelona, …, 8

52

RELATIONSHIPSAttributes

We want to know the grade that each studenthas got for each subject.

Where do we have to put the attribute?

53

RELATIONSHIPSAttributes

Of what student ?

C3, Database analysis,… ,8

54

RELATIONSHIPSAttributes

123, Steven, … gets an 8… in C3, DB…

123, Steven, … gets a 5… in C5, Programming..

444, Karen, … gets a 3… in C3, Programming…

55

RELATIONSHIPSAttributes

… …

12, Steven, …

44, Karen,…

12, Steve… Database

Networks

Programming

student subjectenrols

58

3

56

Activity 3.5The Vendors Database

THE VENDORS DATABASE

58

RELATIONSHIPSCardinality

� It specifies maximum and minimum number of instances of an entity related to one instance of another entity.

59

A teacher teaches subjects.A subject can be taught by teachers.

How many?

RELATIONSHIPSCardinality

Given a teacher... how many subjects?

Given a subject... how many teachers?

60

RELATIONSHIPSCardinality

Xavi

Encarna

Joan

Database

Networks

Programming

TEACHER SUBJECT

teaches

61

RELATIONSHIPSCardinality

� There are two types of cardinality:

– Maximum cardinality (connectivity)

– Minimum cardinality

62

CARDINALITYExample

63

given a teacher... ...how many subjects at most and how many at least?

given a department... ...how many teachers at most and how many at least?

MAXIMUM CARDINALITYDefinition

� It specifies the maximum number of instancesof an entity related to one instance of anotherentity.

“ one” or “ many”

64

MAXIMUM CARDINALITYExample

Given a teacher... how many departments at most?

65

one or many?

Given a department... how many teachers at most?

one or many?

MAXIMUM CARDINALITYExample

Given a teacher... how many departments?

Given a department... how many teachers?

66

onemany

MAXIMUM CARDINALITYTo Remember

The maximum cardinality is represented on the oppositeside of the entity we areworking with.

67

one

MAXIMUM CARDINALITYER Representation

many � None � 1

many oneN 1

68

MAXIMUM CARDINALITYTypes

� According to its maximum cardinality, relationships can be classified as either.

– “one to many” � 1:N

– “many to many” � N:M

– “one to one” � 1:1

69

Relational modeling ideal

MAXIMUM CARDINALITYExamples

70

Activity 3.6Maximum Cardinality

Activity 3.7Entities vs Attributes

MINIMUM CARDINALITYDefinition

� It specifies the minimum number of instances of an entity related to one instance of anotherentity.

“ one” or “ none”

73

MINIMUM CARDINALITYExample

Given a teacher... how many departments at least?

74

one or none?

Given a department... how many teachers at least?

one or none?

MINIMUM CARDINALITYTo Remember

The minimum cardinality is represented by the entity weare working with.

75

one

MINIMUM CARDINALITYExample

Given a teacher... how many departments?

Given a department... how many teachers?

76

none one

MINIMUM CARDINALITYER Representation

none � simple lineone � double line

onenone

77

MINIMUM CARDINALITYExamples

78

MINIMUM CARDINALITYExistence Contraint / Mandatory

existence constraint =

A department can’t exist

without an employee who manages it.

79

TOTAL PARTICIPATION

MINIMUM CARDINALITYOptional

existence constraint

80

Total Participation(mandatory)

Partial Participation(optional)

CARDINALITYRepresentation II

81

e.g. given a teacher......how many subjects at most?...how many subjects at least?

(max, min)

Activity 3.8MinimumCardinality

CONSTRUCTING ERDGuidelines

� Before drawing the ER diagram, understand the requeriments.� Identify all potential entity-types.� Remove duplicate entities.

-Do not include the system as an entity-type!� List the attributtes of each entity.

-Ensure that the entity types are really needed.-Do not have attributes of one entity as attributes of anotherentity!

� Mark the primary keys.� Define the relationships.� Describe the cardinality and optionality of the relationships.� Remove redundant relationships.83

Activity 3.9Bank

Activity 3.10Nursery

Activity 3.11Garage

Activity 3.12Model Agencies

HISTORICAL RECORDS

What’s the difference between both diagrams?88

Activity 3.13General Elections