Lesson III Entity- Relationship Model
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 entitytypes 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
INTRODUCTION Entity-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
INTRODUCTION Entity-Relationship Model Graphical technique to model databases. Entity-Relationship Model It 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 the simplest version of ER and then extend it later on 7
ER DIAGRAMS (ERD) Main Constructs The basic ER notation uses three main constructs: data entities (entity-types) attributes relationships 8
ER DIAGRAMS (ERD) Exemple II entity attributes relationship 9
DATA ENTITY Definition Entities are the main objects of informational interest. A person, animal, place, object or event about which 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 ENTITY Entity Instance entity instances data entity 11
DATA ENTITY Entity Instance A particular occurrence of a data entity is called an entity instance or an entity occurrence. e.g. TEACHER is an entity. BUT Xavier Morera is an entity instance. 12
DATA ENTITY Representation An entity is represented by a rectangle with the name of the entity-type inside. STUDENT TEACHER SUBJECT DEPARTMENT The type of the entity must be written in singular! 13
ENTITY Data Entity vs Entity Instance Business entity 14 Modern languages Computing DEPARTMENT entity instances
DATA ENTITY Guideline 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 the result of the event, not the activity or process. 15
ATTRIBUTES Definition Named characteristics or properties of an entity-type. 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 value at any particular point in time!! 16
ATTRIBUTES Definition An entity-type is described by a set of attributes that provide details about it. All entity instances of the same type have the same attributes!! 17
ATTRIBUTES Guideline 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
ATTRIBUTES Representation Attributes can be denoted by an ellipse with the name of the property inside. 19
ATTRIBUTES Representation There s a simplier way to represent attributes. We ll use single lines to connect the entitity to its attributes. 20
Activity 3.1 Indentifying Entities and Attributes
Is this entitytype correct?
What s the difference between an entity and a table?
ATTRIBUTES Types There are two types of attributes: - descriptors - identifiers (or keys) 24
ATTRIBUTES Descriptors vs Keys o A descriptor is used to specify a nonunique characteristic of a particular entity instance. o An identifier (or key) is used to uniquely determine an instance of an entity. 25
KEY Definition 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
KEY Definition 27 Which attribute/s make/s teachers different?
KEY Example key teacher with id= 34 descriptors 28
KEY Definition Some entities may have more than one key. key key 29
IDENTIFIER Definition A key that has been selected as the unique identifying characteristic for an entity type. identifier OR identifier 30
IDENTIFIER Examples 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 31
How many identifiers can an entity have?
IDENTIFIER ATTRIBUTES To 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 ATTRIBUTES Representation Each attribute in the key is underlined in the ER diagram, as shown: identifier descriptors 34
Activity 3.2 Choosing Identifiers
Is an identifier a primary key in a table?
RELATIONSHIPS Definition Relationships represent real-world associations among instances of one or more entity-types. e.g. A TEACHER teaches SUBJECTS A teacher teaches subjects 37
RELATIONSHIPS Guideline for Naming Relationships Relationships name should be an active or a passive verb. teacher subjects A teacher teaches subjects. A subject is taught by teachers. 38
RELATIONSHIPS Visualization A relationship includes entity instances from each participating entity-type. teacher subjects 39
RELATIONSHIPS Visualization Xavi Database Joan Programming Encarna TEACHER teaches Networks SUBJECT 40
How many instances can be involved in a relationship?
RELATIONSHIPS Introduction to Cardinality teacher subjects A teacher teaches subjects. A subject can be taught by teachers. 42 How many??
RELATIONSHIPS Representation A relationship is depicted by a diamond on the line that joins the entitity-types together. 43
How many relationships may there be between two entities?
RELATIONSHIPS Example 45
ER DIAGRAMS (ERD) Indentifying the Objects An entity normally is the subject or the object in 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.3 Identifying Relationships
How many entity-types can be involved in a relationship?
RELATIONSHIPS Degree 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
RELATIONSHIPS Degree 50
Activity 3.4 Attributes and Relationships
RELATIONSHIPS Attributes 123, Steven, Barcelona,, 8 In what subject? 52
RELATIONSHIPS Attributes We want to know the grade that each student has got for each subject. Where do we have to put the attribute? 53
RELATIONSHIPS Attributes C3, Database analysis,,8 Of what student? 54
RELATIONSHIPS Attributes 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
RELATIONSHIPS Attributes 12, Steve 12, Steven, 44, Karen, 3 8 5 Database Programming Networks 56 student enrols subject
Activity 3.5 The Vendors Database
THE VENDORS DATABASE 58
RELATIONSHIPS Cardinality It specifies maximum and minimum number of instances of an entity related to one instance of another entity. How many? 59 A teacher teaches subjects. A subject can be taught by teachers.
RELATIONSHIPS Cardinality Given a teacher... how many subjects? Given a subject... how many teachers? 60
RELATIONSHIPS Cardinality Xavi Database Joan Programming Encarna TEACHER teaches Networks SUBJECT 61
RELATIONSHIPS Cardinality There are two types of cardinality: Maximum cardinality (connectivity) Minimum cardinality 62
CARDINALITY Example 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? 63
MAXIMUM CARDINALITY Definition It specifies the maximum number of instances of an entity related to one instance of another entity. one or many 64
MAXIMUM CARDINALITY Example Given a teacher... how many departments at most? oneor or many? Given a department... how many teachers at most? 65
MAXIMUM CARDINALITY Example many one Given a teacher... how many departments? Given a department... how many teachers? 66
MAXIMUM CARDINALITY To Remember The maximum cardinality is represented on the opposite side of the entity we are working with. one 67
MAXIMUM CARDINALITY ER Representation many N one 1 N 1 many one 68
MAXIMUM CARDINALITY Types 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 CARDINALITY Examples 70
Activity 3.6 Maximum Cardinality
Activity 3.7 Entities vs Attributes
MINIMUM CARDINALITY Definition It specifies the minimum number of instances of an entity related to one instance of another entity. one or none 73
MINIMUM CARDINALITY Example Given a teacher... how many departments at least? oneor or none? Given a department... how many teachers at least? 74
MINIMUM CARDINALITY To Remember The minimum cardinality is represented by the entity we are working with. one 75
MINIMUM CARDINALITY Example none one Given a teacher... how many departments? Given a department... how many teachers? 76
MINIMUM CARDINALITY ER Representation one double line none simple line none one 77
MINIMUM CARDINALITY Examples 78
MINIMUM CARDINALITY Existence Contraint / Mandatory existence constraint = TOTAL PARTICIPATION A department can t exist without an employee who manages it. 79
MINIMUM CARDINALITY Optional existence constraint Partial Participation (optional) Total Participation (mandatory) 80
CARDINALITY Representation II (max, min) e.g. given a teacher......how many subjects at most?...how many subjects at least? 81
Activity 3.8 Minimum Cardinality
CONSTRUCTING ERD Guidelines 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 another entity! Mark the primary keys. Define the relationships. Describe the cardinality and optionality of the relationships. Remove redundant relationships. 83
Activity 3.9 Bank
Activity 3.10 Nursery
Activity 3.11 Garage
Activity 3.12 Model Agencies
HISTORICAL RECORDS What s the difference between both 88 diagrams?
Activity 3.13 General Elections