Lesson II. Entity-Relationship Model
Lesson II. Entity-Relationship Model
Contents
1. Concepts
2. Design decisions
3. Constrains
4. Keys
5. Entity-Relationship diagram
6. Weak entity sets
….. (and ER exercises)….
2
1. Concepts
1.1. Entity Sets
a) Entity Is a thing or an object, in the real world, distinguishable from all other You
are an
objects.
Is? It exists by itself. May be concrete or abstract. entity
Examples: client, book, course.
It has properties You have
Properties A set of features that allow to univocally identify one entity
features
E.g.: person (name, surname, ID, …), bank loan, book, car?
Example 2 4
3
1
7
6
10
6
A entity is …
… entity set
Attributes
(entity or entity sets)
Are Are 1,85 cm
Associated values to each entity Handsome
Have a domain, or value set: the set of all possible
values. 75 Kg
Each entity may have its own value for each attribute Smart
Used for It allows us to Charming
- Represent an entity using a set of attributes
- Describe each entity using its set of attribute-value pairs
Types:
Elena
Composite 1) Simple and Composite. María
• Simple: Not divided into subparts Isabel
E.g.: (SecondName, Díaz) Dominica
• Composite: Make up by several values. Can be divided into de Silos
subparts de
E.g.: (FullName: Ana Aller Pérez), (Address: C/ Ancha 22 1ª Borbón y
Grecia
Multivalued Esc. 3º A)
2) Single-valued and Multivalued
• Single-valued: It has a single value for a particular entity
E.g.: loan number, ID, age
• Multivalued: an attribute has a set of values for a specific entity
E.g.: phone-number, dependent-name (0, 1 or more)
7
… something …
… entity sets
Composite attributes
8
Example
5
2 3
1
4
6
7
8
10
9
… distinguishable
… Types:
3) NULL Phone #
It is not a value, it is a state. Means UNKNOWN
A field contains a NULL value when:
• it is missing (the value does exist, but we do not have that
NULL information)
Zero, blank? • it is not known
It is NOT: zero, blank or an “empty string”. Does not behave
like any of those values
Special treatment in queries, aggregates, etc.
Derived 4) Derived
Examples? Its value can be derived from the values of other related attributes Triennium
or entities
Sometimes they are physically stored into the database.
Some DBMS use a trick employing computed columns.
Relationship = ,,,
Participation
Association between entity sets
The entity sets E1, E2, …, En participate in relationship sets R
E.g.: buyers and cars participate in “carbuyer”
Relationship instance
Represents an specific relationship between two specific entities
Entity role
The function that an entity plays in a relationship
ü Generally, entity sets are distinct so roles are IMPLICIT and not specified.
ü The same entity set participates in a relationship set more than once
Labels
E.g.: recursive relationship set. Employee– work-for (worker & manager)
11
… Relationship sets
c) Features
Descriptive attributes
Attributes E.g.: depositor (customer, account) -> access-date
E.g.: enrolled-in (student, course) -> grade, grade-Term
2. Design decisions
2.1. Attributes or Entity sets?
Situations- What is phone?-
a) Small number of attributes
Given an Employees Set (entity) that is going to store personal information (ID, fullname,
address and phone number).
b) Several attributes of the same type. Similar situation but including, for each phone, the
color, monthly cost, and location
Solutions
a) As attribute
Just one phone number -> Simple attribute
Every employees “n” phone numbers -> “N” attributes (not elegant).
Indefinite number of phones-> Multivalued attributed.
A number of attributes related to the phone (#, location, cost) -> Composite attribute
b) As Entity sets
A lot of information related to the phone is stored
Several employees sharing the same phone (and typically the phone has some
attributes)
Relationship or entity?
Rule?
Relationship sets.: small number of attributes, low cardinality
Entity sets: lots of attributes, high cardinality (Many to many)
14
… relationship or entity?
1 2
3 4
15
Binary or N-ary?
No replaceable
There are some relationships that are NATURALLY
NON-BINARY
E.g. works-on
Entity sets employee, branch, and job
job entitles (manager, teller, auditor,
..)
–with attributes title and level
Ternary relationship:
Jose–Ordoño II branch- manager
Jose – Lancia branch - auditor
16
… binary or N-ary?
Rule
Sometimes it is possible to replace n-ary relationships by sets of binaries
In general
Any non-binary relationship can be represented using binary relationships
by creating an artificial entity set
But
Also need to translate constraints
And translating all constraints may not be possible
17
3. Constraints
3.1. Mapping Cardinalities
a) Express
The number of entities to which another entity can be associated via a relationship set
One-to-one 1:1
One-to-one An entity in A is associated with at most one entity in B, and an entity in B is
associated with at most one entity in A.
One-to-many 1:N
One-to-many An entity in A is associated with any number (zero or more) of entities in B. An
entity in B, however, can be associated with at most one entity in A.
Many-to-one N:1
Many-to-one An entity in A is associated with at most one entity in B. An entity in B, however,
can be associated with any number (zero or more) of entities in A.
Many-to-many N:M
An entity in A is associated with any number (zero or more) of entities in B, and
Many-to-many
an entity in B is associated with any number (zero or more) of entities in A.
18
Mapping Cardinalities
Note: Some elements in A and B may not be mapped to any elements in the other set
19
… mapping Cardinalities
Constraints
… 3. Constraints
c) Participation
Partial
Some entities may not participate in
any relationship in the relationship In the Extended ER diagram, we
set will specify the participation with
the mapping cardinality:
Total:
(1,1) or (1, n)
Partial:
(0,1) or (0, n)
21
4. Keys (Identifiers)
4.1. Entity Sets
a) Superkey
Is a set of one or more attributes that, taken collectively, allow us to identify uniquely an entity in
the entity set
Some attributes can not be required for identifying an entity
Must be unique but not necessarily irreducible
E.g..: ID, SSN (SocialSecurityNumber); DNI+CustomerName, SSN+PhoneNumber
b) Candidate keys
Are Superkeys for which no proper subset is a superkey (a minimal superkey)
Properties:
1. Unique: there is not two entities with the same candidate key values
2. Irreducible: the set is minimal because there is not proper (or strict) subset of these
attributes for which (1) holds
E.g..: DNI; name+DataBirth+address; CustomerID, SSN, PhoneNumber
c) Primary key
The candidate key chosen
as the principal means of identifying entities within an entity set.
Primary identifier = primary key
Alternative identifier = each of the other candidate keys
22
Keys
In both of the above cases, (1) forms a superkey for the relationship set
The primary key will be chosen depending on the cardinality of the relationship set
In an E-R Diagram, the relationships have not “filled circles” for denoting a Primary Key
23
5. Entity-Relationship Diagram
5.1. It Express
The overall logical structure of a database graphically.
a) Rectangles
represent Entity sets
Strong: §Attribute (descriptive) –empty circle-:
§ Primary identifier (primary key) -full:
Weak: § Candidate identifier:
§ Composite identifier:
c) Diamonds § Multivalued:
§ Composite attribute:
Represent relationship sets
§ Multivalued composite attribute:
Regular: § Optional attribute (dashed line):
§ Derivative Attribute: It has a label usually related to
ID EX
the derivative function used for compute it
Weak:
only in basic ER Diagram
24
E/R Diagram
... Components
d) Lines
- Associating attributes (undirected line)
- to an entity set
- to a relationship set
- Associating RS with ES
- undirected line: Cardinality 1
- directed line: Cardinality N
e) Other
e.1) Cardinality
(min, max) -> Entity sets
1:N -> Relationship sets
e.2) Attributes of relationship sets
Joined with a connecting line to the diamond
e.3) Labels for indicating roles
In recursive (or ring) relationships
e.4) Non binary relationships
Diamond with three (or more) lines connecting
with entity sets
25
E/R Diagram
Example
26
E/R Diagram
Notation
Example
Almost ours!
27
E/R Diagram
E/R Diagram
Notation
E/R Diagram
Notation
Example
30
E/R Diagram
Notation
31
E/R Diagram
Notation
32
E/R Diagram
Information
IDEF1X
Engineering
33
E/R Diagram
Example
34
Example only for illustrating the WEAK entity set. Relationship set representation is not right
6.2. Features
Other typical features of weak entity sets are:
- Relationship Cardinality
Will be 1:N
Do not have attributes
- Discriminator:
The set of attributes that allow us to distinguish among all those entities in the
weak entity set that depend on one particular strong entity
- Primary key:
Discriminator + primary key of identifying entity set
Example only for illustrating the WEAK entity set. Relationship set representation is not right
36
Example 3
1 4
6
8 7
2
10
37
Example