You are on page 1of 15

CSC343 -- Introduction to Databases CSC343 -- Introduction to Databases

Week 10: The Entity Relationship Model


The Entity-Relationship Model
„ The Entity-Relationship (ER) model is a
The Entity-Relationship Model conceptual data model, capable of describing
Entities, Relationships and Attributes the meaning of the data in a database in an
Cardinalities and Keys
Entity Hierarchies and Aggregation
easy-to-understand graphical notation.
Designing a Conceptual Schema, Design Choices „ The meaning is described in terms of a
conceptual (or, ER)
ER) schema.
schema
„ ER schemas are comparable to class diagrams
in UML.

The Entity-Relationship Model -- 1 The Entity-Relationship Model -- 2

CSC343 -- Introduction to Databases CSC343 -- Introduction to Databases

Entities and Entities Sets Examples of Entity Sets


„ An entity set represent classes of objects
(facts, things, people,...) that have
properties in common and an autonomous
existence, e.g., City, Department,
Employee, Purchase and Sale.
„ An entity is an instance/member of an
entity set, e.g., Stockholm, Helsinki, are
examples of instances of the entity City;
Peterson and Johanson are examples of
instances of the Employee entity set.

The Entity-Relationship Model -- 3 The Entity-Relationship Model -- 4

1
CSC343 -- Introduction to Databases CSC343 -- Introduction to Databases
Relationship Sets
„ Represent associations between 2+ entity sets. Examples of Relationship Sets
„ Residence is a relationship set that can exist
between entity sets City and Employee; Exam is
an example of a relationship that can exist
between the entities Student and Course.
„ An instance of a relationship set is an n-tuple
made up of entities, one for each of entity sets
involved.
„ The pair (Johanssen,Stockholm), or the pair
(Peterson,Oslo), are examples of relationships
that are instances of Residence.
„ Note: The collection of instances of a relationship is
by definition a set, not a bag; i.e., no duplicates!
The Entity-Relationship Model -- 5 The Entity-Relationship Model -- 6

CSC343 -- Introduction to Databases CSC343 -- Introduction to Databases

Example of Instances for Exam What Does A Schema Really Mean?

Course
Course Meets Room
Room

Meets
Meets instances
instances
Exam Course
Course instances
instances Room
Room instances
instances
Note: A student can’t take more than one exam for a
particular course!
The Entity-Relationship Model -- 7 The Entity-Relationship Model -- 8

2
CSC343 -- Introduction to Databases CSC343 -- Introduction to Databases

Recursive Relationships Ternary Relationships


Supply
„ Recursive relationships
are also possible, that is
relationships between an
entity and itself.

„ Note in the second example


that the relationship is not
symmetric. In this case it is
necessary to indicate the
two roles that the entity
involved plays in the
relationship.

The Entity-Relationship Model -- 9 The Entity-Relationship Model -- 10

CSC343 -- Introduction to Databases CSC343 -- Introduction to Databases

Attributes Attribute Examples


„ Describe elementary properties of entities
or relationships.
„ For example, Surname, Salary and Age
are attributes of Employee, while Date
and Mark are attributes of relationship
Exam between Student and Course.
„ An attribute associates with each instance
of an entity (or relationship) one or more
values belonging to its domain.
„ Attributes may be single-valued, or multi-
valued.

The Entity-Relationship Model -- 11 The Entity-Relationship Model -- 12

3
CSC343 -- Introduction to Databases CSC343 -- Introduction to Databases

Composite Attributes Schema with Attributes


„ It is sometimes convenient to group
attributes of the same entity or relationship
that have closely connected meanings or
uses. Such groupings are called composite
attributes.

The Entity-Relationship Model -- 13 The Entity-Relationship Model -- 14

CSC343 -- Introduction to Databases CSC343 -- Introduction to Databases

Cardinalities Cardinalities (cont’d)


„ These are specified for each entity participating „ In principle, a cardinality is any pair of non-
in a relationship and describe the maximum and negative integers (n,m) such that n≤m. or a pair
minimum number of relationship instances that of the form (n,N) where N means “any number”.
an entity instance can participate in.
„ If minimum cardinality is 0, entity participation in
a relationship is optional. If minimum cardinality
is 1, entity participation in a relationship is
„ In this example: mandatory.
9 an employee has at minimum 1 assignment „ If maximum cardinality is 1, each instance of the
task and no more than 5. entity is associated at most with a single
instance of the relationship; if maximum
9a task has a minimum of 0 employees
cardinality is N, then each instance of the entity
assigned and no more than 50.
is associated with an arbitrary number of
The Entity-Relationship Model -- 15 instances of the relationship. The Entity-Relationship Model -- 16

4
CSC343 -- Introduction to Databases CSC343 -- Introduction to Databases

Cardinality Examples Textbook Notation


„ …for (1,1) cardinalities (key constraints)

Person Residence City

The Entity-Relationship Model -- 17 The Entity-Relationship Model -- 18

CSC343 -- Introduction to Databases CSC343 -- Introduction to Databases

Cardinality Example Instantiating ER schemas


“A room
„ An ER schema specifies what states are
“A course
meets three
can have
up to 40
possible in the world being modeled
times a meetings
week”
week” per week”
week” (2,2) (0,40)
Course
Course Meets Room
Course (3,3) (0,40) Room
Course Meets Room
Room

“A day can
have an
unlimited (0,N)
number of
meetings”
meetings”

Day
Day

The Entity-Relationship Model -- 19 The Entity-Relationship Model -- 20

5
CSC343 -- Introduction to Databases CSC343 -- Introduction to Databases

Illegal Instantiations Cardinalities of Attributes


„ Describe the minimum and maximum
(2,2) (0,40)
number of values an attribute can have.
Course
Course Meets Room
Room
„ When the cardinality of an attribute is
(1,1), it can be omitted (single-valued
attributes)
„ The value of an attribute, however, may
also be null, or there may be several
values (multi-valued attributes)

The Entity-Relationship Model -- 21 The Entity-Relationship Model -- 22

CSC343 -- Introduction to Databases CSC343 -- Introduction to Databases

Keys
Cardinalities (cont’d) „ Keys consist of minimal sets of attributes which
„ Multi-valued attributes often represent identify uniquely instances of an entity set.
situations that can be modelled with „ For example, socialInsurance# may be a key
additional entities. for Person. Alternatively, firstName,
„ For example, the ER schema of the middleName, lastName, address may be a key.
previous slide can be revised into: „ In most cases, a key is formed by one or more
attributes of the entity itself (internal keys).
Surname
Person
(0,N)
Owns „ Sometimes, other entities are involved in the
License#
identification (foreign keys, weak entities).
(1,1) „ A key for a relationship consists of keys of
entities it relates.
Car Registration#

The Entity-Relationship Model -- 23 The Entity-Relationship Model -- 24

6
CSC343 -- Introduction to Databases CSC343 -- Introduction to Databases

Examples of Keys General Observations on Keys


„ A key may consist of one or more attributes,
internal, single-attribute
provided that each of them has (1,1) cardinality.
„ A foreign key can involve one or more entities,
provided that each of them is member of a
relationship to which the entity to be identified
internal, multi-attribute participates with cardinality equal to (1,1).
foreign, multi-attribute
„ A foreign key may involve an entity that has
itself a foreign key, as long as cycles are not
generated.
„ Each entity must have at least one (internal or
foreign) key. If there is more than one key, one
of them is labelled as primary.
The Entity-Relationship Model -- 25 The Entity-Relationship Model -- 26

CSC343 -- Introduction to Databases CSC343 -- Introduction to Databases

Schema with Keys Modeling an Application with Keys


Keys constitute a powerful mechanism for
modeling an application. Assume we want a
database storing information about course
lectures.
„ Suppose first that we use the key coursename,
coursename
day hour for the Lecture entity. What does
day,
this say about the application?
„ Suppose now we use only coursename as
identifier for Lecture.
Lecture What does this say about
the application??

The Entity-Relationship Model -- 27 The Entity-Relationship Model -- 28

7
CSC343 -- Introduction to Databases CSC343 -- Introduction to Databases

Entity (Class) Hierarchies


Consider...
(1,1) „ Entity hierarchies (called class hierachies in the
name textbook) organize a group of entity sets into a
Course
title
parent/child hierarchy using as criterion their
dept
year generality/specificity. (Also called
sem generalization hierarchies)
„ Suppose we want information on course
offerings over the years. What does each of the
following identifiers say about the application?
9 name; isA isA

9 name, sem, year;


9 sem, year;
9 name, dept;
9 dept, year.
The Entity-Relationship Model -- 29 The Entity-Relationship Model -- 30

CSC343 -- Introduction to Databases CSC343 -- Introduction to Databases

Overlap Constraints
Covering Constraints
„ Sometimes we want to specify that the children of an
entity do/don’t overlap.
„ Now we want to be able to say that
„ For example in previous slide, Man/Woman don’t
overlap, neither do Lawyer/Engineer/Doctor. instances of the children of an entity
include all instances of their parent (I.e.,
„ To indicate overlap, we use separate isA links. E.g.,
cover it). We use a dark triangle notation
for this.

isA isA

isA isA isA

The Entity-Relationship Model -- 31 The Entity-Relationship Model -- 32

8
CSC343 -- Introduction to Databases CSC343 -- Introduction to Databases

Properties of Generalization An Entity Hierarchy


„ Every instance of a child is also an instance
of the parent.
isA
„ Every property of the parent entity isA isA

(attribute, key, relationship) is also a


property of a child entity. This property of
generalizations is known as inheritance. isA

isA

The Entity-Relationship Model -- 33 The Entity-Relationship Model -- 34

CSC343 -- Introduction to Databases CSC343 -- Introduction to Databases

Aggregation An Example
„ Used when we have to model a relationship name
involving (entity sets and) and a ssn lot

relationship set.
Employees
„ Aggregation allows us to treat a
relationship set as an entity set for
purposes of participation in other Monitors until

relationships.
started_on since
dname
pid pbudget did budget

Projects Sponsors Departments

The Entity-Relationship Model -- 35 The Entity-Relationship Model -- 36

9
CSC343 -- Introduction to Databases CSC343 -- Introduction to Databases
Some Rules of Thumb
Conceptual Design with the ER Model „ If a concept has significant properties and/or
describes classes of objects with an autonomous
„ Design choices: Should a concept be existence, it is appropriate to represent it as an
modeled as an entity, an attribute, or a entity.
relationship? „ If a concept has a simple structure, and no relevant
„ Constraints on the ER Model: A lot of properties associated with it, it can be represented
data semantics can (and should) be with attributes of other concepts to which it refers.
captured; but some constraints cannot be
captured by ER diagrams. „ If a concept provides a logical link between 2+
entities, it is convenient to represent it with a
relationship.
„ If 1+ concepts are particular cases of another
concept, it is convenient to represent them in terms
The Entity-Relationship Model -- 37 of a generalization relationship. The Entity-Relationship Model -- 38

CSC343 -- Introduction to Databases CSC343 -- Introduction to Databases

Entity vs. Attribute Entity vs. Relationship


WorksIn does not from to from to
allow an name dname name dname
ssn lot ssn lot
employee to did budget did budget

work in a Employee WorksIn Department


WorksIn can also Employee WorksIn Department
department for be turned into an
two or more entity to avoid
periods. the problem
from to
mentioned earlier
from to
How can this be
name dname name
ssn lot did dname
budget ssn lot
fixed? did budget
Employee WorksIn2 Department WorksIn3
Employee WorkPeriod WorkDept Department

from Duration to

The Entity-Relationship Model -- 39 The Entity-Relationship Model -- 40

10
CSC343 -- Introduction to Databases CSC343 -- Introduction to Databases
From Text to an ER Schema
Database Design -- Early Phases We wish to create a database for a company that runs training courses.
For this, we must store data about the trainees and the instructors. For
each course participant (about 5,000), identified by a code, we want to
„ To design a database, we need to identify store her social security number, surname, age, sex, place of birth,
employer’s name, address and telephone number, previous employers
requirements, generate a conceptual (and periods employed), the courses attended (there are about 200
design, using an ER schema, and from it courses) and the final assessment for each course. We need also to
represent the seminars that each participant is attending at present
generate a logical design using a relational and, for each day, the places and times the classes are held.
schema. Each course has a code and a title and any course can be given any
number of times. Each time a particular course is given, we will call it
„ In the following we discuss how can one go an ‘edition’ of the course. For each edition, we represent the start date,
the end date, and the number of participants. If a trainee is self-
from an informal description of employed, we need to know her area of expertise, and, if appropriate,
requirements to a conceptual design. her title. For somebody who works for a company, we store the level
and position held. For each instructor (about 300), we will show the
surname, age, place of birth, the edition of the course taught, those
taught in the past and the courses that the tutor is qualified to teach.
All the instructors’ telephone numbers are also stored. An instructor can
be permanently employed by the training company or freelance.
The Entity-Relationship Model -- 41 The Entity-Relationship Model -- 42

CSC343 -- Introduction to Databases CSC343 -- Introduction to Databases

Example, Annotated More Annotations


We
We wish
wish to to create
create aa database
database for foraacompany
companythat thatruns
runstraining
trainingcourses.
courses.
For
For this,
this, we
we must
must store
store data
data about
aboutthe thetrainees
traineesand andthe
theinstructors.
instructors.For For We wish to to create
create aa database
database for for aacompany
companythat thatruns
runstraining
trainingcourses.
courses.For Forthis,
this,
each
each course
course participant
participant (about
(about 5,000),
5,000), identified
identified by byaacode,
code,we wewantwanttoto we must storestore data
data about
about the the trainees
trainees and and the
the instructors.
instructors. For For each
each course
course
store
store herher social
social security
security number,
number, surname,
surname, age, age, sex,
sex, place
place ofof birth,
birth, participant (about
(about 5,000),
5,000),identified
identifiedbyby a code, we want
a code, we wantto store to her
storesocial
her security
social
employer’s
employer’s name,name, address
address and and telephone
telephone number,
number,previous
previousemployers
employers number, number,
security surname, age, sex,
surname, age,place of birth,
sex, place employer’s
of birth, employer’s name, name,address
addressand
(and
(and periods
periods employed),
employed), the the courses
courses attended
attended (there
(there areare about
about 200 200 telephone
and telephonenumber,
number, previous
previous employers
employers(and (andperiods
periods employed),
employed), the the courses
courses
courses)
courses) and and the
the final
final assessment
assessment for for each
each course.
course. We We need
need alsoalsototo attended (there
(there areareabout
about 200 courses)
200 courses) andandthe final
the assessment
final assessment for each forcourse.
each
represent seminars that
represent the seminars thateach
eachparticipant
participant is attending
is attending at present
at present and, We needWe
course. alsoneedto represent the seminars
also to represent the that each participant
seminars that each is attending isat
participant
and, for each
for each day, day, the places
the places and times
and times the classes
the classes are held.
are held. present and,
attending for each and,
at present day, the
for places
each day, and thetimes the classes
places and timesare held.
the classes are
Each
Each course
course hashas aa code
code and
and aa title
title and
and any
any course
course cancanbe begiven
givenanyany held.
Each course has a code and a title and any course can be given any number of
number
number of times. Each Each time
time aa particular
particularcourse
courseisisgiven,
given,we wewill
willcall
call
it it
an times.course
Each Each time
has aacodeparticular
and a course
title andisany given, we can
course will call it an any
be given ‘edition’
number of the
of
an ‘edition’
‘edition’ of the
of the course.
course. ForForeacheach edition,
edition, wewe represent
represent thethe start
start date,
date, the course.Each
times. For time
each aedition,
particularwe course
represent the start
is given, date,
we will callthe end‘edition’
it an date, and of thethe
the
end enddate,date, andnumber
and the the number of participants.
of participants. If a is
If a trainee trainee is self-
self-employed, number of
course. Forparticipants.
each edition, If a we
trainee is self-employed,
represent the start date,we need
the end to know
date,her area
and theof
employed,
we need towe knowneedhertoarea
know of her area ofand,
expertise, expertise, and, if appropriate,
if appropriate, her title. For expertise,
number of and, if appropriate,
participants. her title.
If a trainee For somebody
is self-employed, wewhoneedworksto knowfor her
a company,
area of
her title. For
somebody whosomebody
works for who works for a
a company, wecompany,
store thewe store
level and theposition
level we store the
expertise, level
and, and positionher
if appropriate, held. For
title. Foreach instructor
somebody (about
who works 300),
for awecompany,
will show
and
held.position
For each held. For each
instructor instructor
(about 300), (about
we will 300),show wethe will show the
surname, age, the store
we surname,
the age,
level place of birth, held.
and position the edition
For eachof theinstructor
course taught,
(about those300), taught
we willin
surname, age, the
place of birth, place of birth,
edition of thethe edition
course of thethose
taught, course taught,
taught in thethose
past the past
show the and the courses
surname, that the
age, place tutor the
of birth, is qualified
edition of to the
teach. All the
course instructors’
taught, those
taught
and theincourses
the pastthatandthethetutor
courses that theto
is qualified tutor is qualified
teach. to teach.
All the instructors’ telephone
taught numbers
in the past and are the
alsocourses
stored. An thatinstructor
the tutor can be permanently
is qualified to teach.employed
All the
All the instructors’
telephone numbers telephone
are also numbers
stored. An are instructor
also stored. canAnbe instructor
permanentlycan by the training
instructors’ company
telephone or freelance.
numbers are also stored. An instructor can be permanently
be permanently
employed by theemployed by the training
training company company or freelance.
or freelance. employed by the training company or freelance.
The Entity-Relationship Model -- 43 The Entity-Relationship Model -- 44

11
CSC343 -- Introduction to Databases CSC343 -- Introduction to Databases

More Annotations
Glossary Example We wish to create a database for a company that runs training courses. For this,
we must store data about the trainees and the instructors. For each course
participant (about 5,000), identified by a code, we want to store her social security
Term Description Synonym Links number, surname, age, sex, place of birth, employer’s name, address and
Trainee Participant in a course. Can Participant Course, telephone number, previous employers (and periods employed), the courses
be an employee or self- Company attended (there are about 200 courses) and the final assessment for each course.
employed. We need also to represent the seminars that each participant is attending at
Instructor Course tutor. Can be Tutor Course present and, for each day, the places and times the classes are held.
freelance. Each course has a code and a title and any course can be given any number of
Course Course offered. Can have Seminar Instructor, times. Each time a particular course is given, we will call it an ‘edition’ of the
various editions. Trainee course. For each edition, we represent the start date, the end date, and the
Company Company by which a trainee Trainee number of participants. If a trainee is self-employed, we need to know her area of
is employed or has been expertise, and, if appropriate, her title. For somebody who works for a company,
employed. we store the level and position held. For each instructor (about 300), we will show
the surname, age, place of birth, the edition of the course taught, those taught in
the past and the courses that the tutor is qualified to teach. All the instructors’
telephone numbers are also stored. An instructor can be permanently employed
by the training company or freelance.
The Entity-Relationship Model -- 45 The Entity-Relationship Model -- 46

CSC343 -- Introduction to Databases CSC343 -- Introduction to Databases

Structuring of Requirements (I) Structuring of Requirements (II)


Phrases relating to courses
Phrases of a general nature
For each course (about 200), we will hold the name and code. Each
We wish to create a database for a company that runs training time a particular course is given, we will call it an “edition” of the
courses. We wish to maintain data for the trainees and the course. For each edition, we will hold the start date, the end date,
instructors. and the number of participants. For the editions currently in
Phrases relating to trainees progress, we will hold the dates, the classrooms and the times in
For each trainee (about 5000), identified by a code, the database which the classes are held.
will hold the social security number, surname, age, sex, town of Phrases relating to specific types of trainee
birth, current employer, previous employers (along with the start For a trainee who is a self-employed professional, we will hold the
date and the end date of the period employed), the editions of the area of expertise and, if appropriate, the professional title. For a
courses the trainee is attending at present and those he or she has trainee who is an employee, we will hold the level and position held.
attended in the past, with the final marks out of ten.
Phrases relating to instructors
Phrases relating to the employers of trainees For each instructor (about 300), we will hold surname, age, town of
For each employer of a trainee the database will store name, birth, all telephone numbers, the edition of courses taught, those
address and telephone number. taught in the past and the courses the instructor is qualified to
teach. The instructors can be permanently employed by the training
company or can be freelance.

The Entity-Relationship Model -- 47 The Entity-Relationship Model -- 48

12
CSC343 -- Introduction to Databases CSC343 -- Introduction to Databases

Operational Requirements
„ operation 1: insert new trainee, including her data (40/day);
Design Decisions
„ operation 2: assign a trainee to an edition of a course (50/day);
„ Consider address of a trainee. Is it an
„ operation 3: insert a new instructor, including all his or her data and
the courses he or she is qualified to teach (2/day); entity or relationship?
„ operation 4: assign an instructor to a course edition(15/day); „ Consider address for a telephone company
„ operation 5: display all information on past editions of a course with database, which has to keep track of how
title, class timetables and number of trainees (10/day); many and what type of phones are
„ operation 6: display all the courses offered, with information on the
instructors who are qualified to teach them (20 times a day);
available in any one household, who lives
„ operation 7: for each instructor, find the trainees for all the courses there (there may be several phone bills
he or she is teaching or has taught (5 times a week); going to the same address) etc.
„ operation 8: carry out a statistical analysis of all the trainees with all „ How do we represent employment of a
the information about them, about the editions of courses they have
attended and the marks obtained (10 times a month).
trainee by a particular employer?
„ How do we represent a course edition?
The Entity-Relationship Model -- 49 The Entity-Relationship Model -- 50

CSC343 -- Introduction to Databases CSC343 -- Introduction to Databases

Documentation of an E-R Schema —


...The Result! The Data Dictionary

„ An ER schema is rarely sufficient by itself to


represent all aspects of an application in detail.
„ It is therefore important to complement every E-
R schema with support documentation, that
isA
facilitates the interpretation of the schema and
isA
describes properties of the data that cannot be
expressed directly by the constructs of the
model.
„ A widely-used documentation concept for
conceptual schemas is the business rule.
The Entity-Relationship Model -- 51 The Entity-Relationship Model -- 52

13
CSC343 -- Introduction to Databases CSC343 -- Introduction to Databases

Business Rules The Data Dictionary


„ Business rules are used to describe the „ Descriptive business rules can be organized as a
properties of an application, e.g., the fact that data dictionary. This is made up of two tables:
an employee cannot earn more than his or her the first describes the entities of the schema, the
manager. others describes the relationships.
„ A business rule can be: „ Business rules that describe constraints can be
9the description of a concept relevant to the expressed in the following form:
application (also known as a business object), <concept> must/must not <expression on concepts>
9an integrity constraint on the data of the „ Business rules that describe derivations can be
application, expressed in the following form:
9a derivation rule, whereby information can <concept> is obtained by <operations on concepts>
be derived from other information within a
schema. The Entity-Relationship Model -- 53 The Entity-Relationship Model -- 54

CSC343 -- Introduction to Databases CSC343 -- Introduction to Databases

Example of a Data Dictionary


Examples of Business Rules
Entity Description Attributes Identifier
EMPLOYEE Employee working in the Code, Surname, Code Constraints
company. Salary, Age
(BR1) The manager of a department must belong to that department.
PROJECT Company project on which Name, Budget, Name (BR2) An employee must not have a salary greater than that of the manager
employees are working. ReleaseDate of the department to which he or she belongs.
(BR3) A department of the Rome branch must be managed by an employee
.... .... .... ....
with more than 10 years’ employment with the company.
(BR4) An employee who does not belong to a particular department must not
participate in any project.
Relationship Description Entities involved Attributes ....
MANAGEMENT Associate a manager with Employee (0,1), Derivations
a department. Department (1,1)
(BR5) The budget for a project is obtained by multiplying the sum of the
MEMBERSHIP Associate an employee Employee (0,1)
with a department. Department (1,N) StartDate salaries of the employees who are working on it by 3.
....
.... .... .... ....

The Entity-Relationship Model -- 55 The Entity-Relationship Model -- 56

14
CSC343 -- Introduction to Databases

ER Schemas vs UML Class Diagrams


„ The ER model allows N-ary relationships, N≥2;
Class diagrams only allow binary relationships.
„ ER schemas allow multi-valued attributes,
class diagrams do not.
„ ER schemas include keys (an often-
encountered type of constraint), class
diagrams do not.
„ Class diagrams allow dynamic classification,
but ER diagrams do not.
„ Class diagrams can have associated methods,
constraints and pre/post-conditions.
The Entity-Relationship Model -- 57

15

You might also like