Entity-Relationship (ER) Model: EBA3420 Databases

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 27

Entity-Relationship (ER) Model

EBA3420 Databases

Magdalena Ivanovska (Jan Kudlicka)


Department of Data Science and Analytics

February 7, 2022
ENTITY-RELATIONSHIP MODEL (ER-MODEL, ER-DIAGRAM)

An ER-model is a conceptual data model that incorporates the requirements of data


modeling in the miniworld of interest.

It has three main components:

I Entities/entity types – objects for which we store data


I Attributes – properties of entities
I Relationships – relations between entities

Magdalena Ivanovska (Jan Kudlicka): Entity-Relationship (ER) Model 2


ENTITY-RELATIONSHIP (ER) MODEL – EXAMPLE
Last name
First name
Hour salary
Supervis
ee
Supervises Employee Works on Project

Supervisor Year
ID Hours spent ID Title
Note of birth

Works at

Number of emp. Department

ID Title

Magdalena Ivanovska (Jan Kudlicka): Entity-Relationship (ER) Model 3


WHY CONCEPTUAL MODEL?

I Comparing to the natural language:


I More formal
I Not ambiguous
I Comparing to the relational model:
I Neither implementation nor (R)DBMS specific details
I High-level description of the miniworld (a part of the real world we want to model).
I Easy to understand by people without technical background.
I Can be used as the documentation.
I Can be easily transformed to a relational model (and then implemented in a relational
database).

Magdalena Ivanovska (Jan Kudlicka): Entity-Relationship (ER) Model 4


ENTITY TYPE AND ENTITY

I Entity: a “thing” or “object” in the miniworld for which we want to store some data
Examples of entities in a university IT system: each student, teacher, course, department, building, etc.
I an entity is capable of independent existence
I does not have to exist physically E.g. a department, a course, or an order in an online store.
I It must be possible to distinguish entities from each other, i.e. each entity must have
some identity. E.g. the personal no. for teachers or students

I Attribute: a (relevant) property or feature of entities.


Examples of attributes for a teacher: the personal no., the first name, the last name, the birthdate, etc.

I Entity type: a set of entities having the same attributes.


Entity type Student is a set of all students.

Magdalena Ivanovska (Jan Kudlicka): Entity-Relationship (ER) Model 5


GRAPHICAL NOTATION FOR ENTITY TYPES AND ATTRIBUTES

In the ER diagram we use rectangles for entity types and ovals for attributes. The attributes
are connected to the entity type by lines.

Last name Birth date

Student

Personal no. First name

Magdalena Ivanovska (Jan Kudlicka): Entity-Relationship (ER) Model 6


TYPES OF ATTRIBUTES

I Simple: each entity has a single atomic (indivisible) value.


E.g. “ID”, “Age”, “First name”.
I Composite (or compound): the attribute can be divided into several components.

E.g. “Address” consists of the street name, the house no., the city and the postal no.
I Multi-valued: the attribute might have multiple values.
E.g. “Email address” for a person,
I Derived – the value of the attribute can be determined from the other attributes.

E.g. “Age” if there is an attribute for the birth date.

Magdalena Ivanovska (Jan Kudlicka): Entity-Relationship (ER) Model 7


EXAMPLE WITH SEVERAL TYPES OF ATTRIBUTES

middle name last name

birth date
first name full name

Person age

personal no.
email

Magdalena Ivanovska (Jan Kudlicka): Entity-Relationship (ER) Model 8


KEY ATTRIBUTES

I Key attribute: an attribute that uniquely identifies entities of a given type (i.e., each
entity has a unique value of this attribute).
I Key attribute is underlined in the ER-diagram.
I Several attributes together might be used as a key
I Each (strong) entity type has to have at least one key. If none of the attributes can be
used as a (natural) key, you can add a “surrogate” key attribute (e.g. an integer ID).

ssn id name

Employee

Magdalena Ivanovska (Jan Kudlicka): Entity-Relationship (ER) Model 9


RELATIONSHIPS

I Relationship: a relation* between entities of given entity types.


Ola Nordmann (entity of type Employee) works at (relationship) the Sales dept. (entity of type Department).
The Sales dept. (entity of type Department) employs (relationship) Ola Nordmann (entity of type Employee).
I The relationships do not have a direction.
E.g. ”works at” and ”employs” in the above example is the same relationship between entity types Employee
and Department.
I Binary relationships, i.e. relationships involving two entity types, are the most common
(and we will only works with those).
I The relationships can involve more than 2 entity types.
E.g. A relationship among Employee, Manager, Department if a manager can manage employees from different
departments

*Note that ”relation” here is used in the sense of connection (link), not as a relation (table) in the relational model.

Magdalena Ivanovska (Jan Kudlicka): Entity-Relationship (ER) Model 10


GRAPHICAL NOTATION FOR RELATIONSHIPS

A diamond with lines to the involved entity types.

Teacher Teaches Course

Note: We usually use a verb for the name of a relationship, and we put it in an ”appropriate” form/tense for reading it
from left/upper to the right/lower entity type it relates: Teacher teaches a course.

Magdalena Ivanovska (Jan Kudlicka): Entity-Relationship (ER) Model 11


SPECIAL CASES

Relationships might also exist between entities of the same type.


Homer Simpson (entity of the type Person) is a father (relationship) of Lisa Simpson (entity of the type Person).

There might be several relationships between the same entity types.

Works at

Employee Department

Manages

Magdalena Ivanovska (Jan Kudlicka): Entity-Relationship (ER) Model 12


CARDINALITIES

An E1 entity can be related to one E2 An E1 entity can be related to several E2


entity: entities:

E1 R E2 E1 R E2

An E2 entity can be related to one E1 An E2 entity can be related to several E1


entity: entities:

E1 R E2 E1 R E2

Magdalena Ivanovska (Jan Kudlicka): Entity-Relationship (ER) Model 13


TYPES OF RELATIONSHIPS BASED ON CARDINALITIES

Three different types of relationships based on cardinalities:


I One-to-one (1:1)
I One-to-many (1:N)
I Many-to-many (N:N)

One-to-one (1:1)

Employee Is head of Department

No employee can be the head of several departments. No department can have several
heads.
Magdalena Ivanovska (Jan Kudlicka): Entity-Relationship (ER) Model 14
TYPES OF RELATIONSHIPS BASED ON CARDINALITIES

One-to-many (1:N)

Publisher Published Book

A publisher can publish many books. No book can have several publishers.
Many-to-many (N:N)

Author Wrote Book

An author can have written several books and a book can have several authors.

Magdalena Ivanovska (Jan Kudlicka): Entity-Relationship (ER) Model 15


RELATIONSHIP ATTRIBUTES

Relationships might have attributes too.


Example: Ola Nordmann (entity of the type Student) took (relationship) the EBA3420 course (entity of the type
Course) and got a grade A. The grade is an attribute of the relationship, not an attribute of a student, nor of a a
course.

Student Took Course

grade

Example: Oslo Nye Teater (entity of the type Theater) plays (relationship) Hamlet (entity of the type Play) this
Saturday at 7pm. The date and time is an attribute of the relationship.

Magdalena Ivanovska (Jan Kudlicka): Entity-Relationship (ER) Model 16


EXERCISE 1

? ?
Employee Works on Project

An employee can work on several projects, and a project can have several employees
working on it.

Employee Works on Project


1.

Employee Works on Project


2.

Employee Works on Project


3.

Employee Works on Project


4.
Magdalena Ivanovska (Jan Kudlicka): Entity-Relationship (ER) Model 17
EXERCISE 2

? ?
Employee Manages Department

No employee manages more than one department and each department can only have one
manager.

Employee Manages Department


1.

Employee Manages Department


2.

Employee Manages Department


3.

Employee Manages Department


4.
Magdalena Ivanovska (Jan Kudlicka): Entity-Relationship (ER) Model 18
PARTICIPATION

I Mandatory participation: each entity must be related to at least one entity of the
other type.
A book has to have at least one author.
I Optional participation: an entity does not have to be related to any entity of the
other type.
An employee does not have to be a manager of any project.

Notation:

Mandatory participation Optional participation

E1 R E2 E1 R E2

Magdalena Ivanovska (Jan Kudlicka): Entity-Relationship (ER) Model 19


EXERCISE 3

Employee Works at Department

Each employee must work at some department and each department must have some
employees.

Employee Works at Department


1.

Employee Works at Department


2.

Employee Works at Department


3.

Employee Works at Department


4.
Magdalena Ivanovska (Jan Kudlicka): Entity-Relationship (ER) Model 20
EXERCISE 4

Employee Manages Department

Not every employee is a manager, but every department has a manager.

Employee Manages Department


1.

Employee Manages Department


2.

Employee Manages Department


3.

Employee Manages Department


4.
Magdalena Ivanovska (Jan Kudlicka): Entity-Relationship (ER) Model 21
ROLES

A role describes what function an entity has in a relationship.


Usually not specified, except for relationships between entities of the same type:

Employee

manager worker
Supervises

Magdalena Ivanovska (Jan Kudlicka): Entity-Relationship (ER) Model 22


WEAK ENTITY TYPES

I Weak entities: entities that cannot be uniquely identified by its own attributes and
must rely on the identity of an “owner” entity.
E.g. rooms in a building, departments in an organization.

I A relationship to the owner entity is called an identifying relationship.


I An identifying relationship can only be 1:1 or 1:N.
I In the 1:N case, the weak entity type needs a partial key—an attribute which together
with the owner’s key can uniquely identify the weak entities.
I Weak entities can be owners of other weak entities.

Magdalena Ivanovska (Jan Kudlicka): Entity-Relationship (ER) Model 23


GRAPHICAL NOTATION FOR WEAK ENTITIES

I Weak entity type is represented by a double-lined rectangle.


I Identifying relationship is represented by a double-lined diamond.
I Partial key is underlined using a dashed line.

Building Consists of Room

name room no.

Magdalena Ivanovska (Jan Kudlicka): Entity-Relationship (ER) Model 24


ER-MODEL – COMPANY DATABASE
Last name
First name
Hour salary
Supervis
ee
Supervises Employee Works on Project

Supervisor Year
ID Hours spent ID Title
Note of birth

Works at

Number of emp. Department

ID Title

Magdalena Ivanovska (Jan Kudlicka): Entity-Relationship (ER) Model 25


TRANSFORMING AN ER-MODEL INTO AN RDB

I (strong) entity type = table


I attributes = columns in the corresponding tables
I key attribute = primary key
I composite attribute = columns for all the components
I multi-valued attribute = more columns/separate table with primary key from the
corresponding entity type and the attribute itself as a column
I weak entity type = table with a column for the primary key of the owner entity type
I relationships = tables or foreign keys
I one-to-one = a foreign key in only one of the associated relations (tables)
I one-to-many = the relation (table) on the ”many” side must have a foreign key column to
the other table (employee table has as foreign key attribute (dept.id) from the department table)
I many-to-many = a new table where the primary key is consisting of the foreign keys to
the related tables (employee-project table in the Company database)

Magdalena Ivanovska (Jan Kudlicka): Entity-Relationship (ER) Model 26


TODAY’S PROBLEM

Create an ER-model for a simple online database for movies based on the following
specification (you can use erdplus.com to draw the diagram):

I Each movie has a title, release year, runtime in minutes, MPA rating (G, PG, PG-13,
R, or NC-17), one or more directors, one or more writers, one or more actors, and a
storyline (a description of the plot of the movie).
I Each movie can be assigned one or more genres, such as comedy, drama, action,
horror, etc.
I Each actor, writer or director has a name (consisting of the first, middle and last
name), date and place of birth, and a short biography.
I For each movie we wish to store names of all characters and who played them (e.g.
John McClane in Die Hard, played by Bruce Willis). Note that the same actor might
play several characters in the same movie.

Magdalena Ivanovska (Jan Kudlicka): Entity-Relationship (ER) Model 27

You might also like