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

IT221 Database

Lecture 4

Entity/Relationship Modelling
• Entities and Attributes
• Relationships
• Attributes
• E/R Diagram
• Participation constraints
• Cardinality ratios
Entity Relationship Models
• A relationship is an association among several
entities
• Mapping cardinalities, or cardinality ratios,
express the number of entities to which another
entity can be associated via a relationship set.
• Mandatory Relationships
• Optional Relationships
• Many-to-Many Relationships
• One-to-Many Relationships
• One-to-One Relationships
• Recursive Relationships
IT221: Entity Relationship Modelling
Mandatory, Many-to-Many

INSTRUCTOR STUDENT

INSTRUCTOR STUDENT

IT221: Entity Relationship Modelling


Optional, Many-to-Many

DEPARTMENT STUDENT

DEPARTMENT STUDENT

IT221: Entity Relationship Modelling


Optional/Mandatory,
Many-to-Many
Many-to-many. An entity in A is associated with any number
(zero or more) of entities in B, and an entity in B is associated with
any number (zero or more) of entities in A.

INSTRUCTOR SKILL

INSTRUCTOR SKILL

IT221: Entity Relationship Modelling


Optional/Mandatory,
One-to-Many
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. 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.

PRODUCT VENDOR

PRODUCT VENDOR

IT221: Entity Relationship Modelling


Mandatory, One-to-One
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

AUTOMOBILE ENGINE

AUTOMOBILE ENGINE

IT221: Entity Relationship Modelling


Recursive

EMPLOYEE
supervises

is supervised by

IT221: Entity Relationship Modelling


Participation Constraints

• The participation of an entity set E in a


relationship set R is said to be total if every
entity in E participates in at least one
relationship in R. If only some entities in E
participate in relationships in R, the
participation of entity set E in relationship R is
said to be partial.
• In figure (a), the participation of B in the
relationship set is total while the participation
of A in the relationship set is partial. In Figure
(b), the participation of both A and B in the
relationship
IT221: set
Entity Relationship are total.
Modelling
IT221: Entity Relationship Modelling
Entity/Relationship Modelling
• E/R Modelling is • Example
used for conceptual • In a University
design database we might
have entities for
• Entities - objects or Students, Modules and
items of interest Lecturers. Students
• Attributes - facts might have attributes
about, or properties such as their ID,
of, an entity Name, and Course, and
could have
• Relationships - links relationships with
between entities Modules (enrolment)
and Lecturers
(tutor/tutee)
IT221: Entity Relationship Modelling
Entity/Relationship Diagrams
• E/R Models are often Lecturer ID

represented as E/R Name Course


diagrams that
• Give a conceptual
view of the database Tutors Student
• Are independent of
the choice of DBMS
• Can identify some
problems in a design Module Studies

IT221: Entity Relationship Modelling


Entities
• Entities represent • Entities have
objects or things of • A general type or
interest class, such as
• Physical things like Lecturer or Module
students, lecturers, • Instances of that
employees, products particular type, such
• More abstract things as Lilian Michael,
like modules, orders, Adam Malima are
courses, projects instances of Lecturer
• Attributes (such as
name, email address)

IT221: Entity Relationship Modelling


Diagramming Entities
• In an E/R Diagram, Lecturer ID

an entity is usually Name Course


drawn as a box with
rounded corners
Tutors Student
• The box is labelled
with the name of the
class of objects
represented by that Module Studies
entity

IT221: Entity Relationship Modelling


Attributes
• Attributes are facts, • Attributes have
aspects, properties, • A name
or details about an • An associated entity
entity • Domains of possible
• Students have IDs, values
names, courses, • Values from the
addresses, … domain for each
• Modules have codes, instance of the entity
titles, credit weights, they are belong to
levels, …

IT221: Entity Relationship Modelling


Diagramming Attributes
• In an E/R Diagram Lecturer ID

attributes may be Name Course


drawn as ovals
• Each attribute is
Tutors Student
linked to its entity by
a line
• The name of the
attribute is written in Module Studies
the oval

IT221: Entity Relationship Modelling


Relationships
• Relationships are an • Relationships have
association between • A name
two or more entities • A set of entities that
• Each Student takes participate in them
several Modules • A degree - the
• Each Module is taught number of entities
by a Lecturer that participate (most
• Each Employee works have degree 2)
for a single • A cardinality ratio
Department

IT221: Entity Relationship Modelling


Cardinality Ratios
• Each entity in a • One to one (1:1)
relationship can • An order can generate
only one invoice
participate in zero,
• One to many (1:M)
one, or more than • A lecturer may tutor
one instances of that many students, but each
relationship student has just one
tutor
• This leads to 3 types • Many to many (M:M)
of relationship… • Each student takes
several modules, and
each module is taken by
several students

IT221: Entity Relationship Modelling


Diagramming Relationships
• Relationships are Lecturer ID

links between two Name Course


entities
• The name is given in
Tutors Student
a diamond box
• The ends of the link
show cardinality
Module Studies

One Many

IT221: Entity Relationship Modelling


Removing M:M Relationships
• Many to many Student
relationships are
difficult to represent Student
Has
• We can split a many
to many relationship
Studies Enrolment
into two one to
many relationships
• An entity represents Module
In

the M:M relationship


Module

IT221: Entity Relationship Modelling


Making E/R Models
• To make an E/R • General guidelines
model you need to • Since entities are
identify things or objects they
• Enitities are often nouns in the
description
• Attributes
• Attributes are facts or
• Relationships properties, and so are
• Cardinality ratios often nouns also
• from a description • Verbs often describe
relationships between
entities

IT221: Entity Relationship Modelling


Example
A university consists of a number of
departments. Each department offers several
courses. A number of modules make up each
course. Students enrol in a particular course
and take modules towards the completion of
that course. Each module is taught by a
lecturer from the appropriate department, and
each lecturer tutors a group of students

IT221: Entity Relationship Modelling


Example - Entities
A university consists of a number of
departments. Each department offers several
courses. A number of modules make up
each course. Students enrol in a particular
course and take modules towards the
completion of that course. Each module is
taught by a lecturer from the appropriate
department, and each lecturer tutors a group
of students

IT221: Entity Relationship Modelling


Example - Relationships
A university consists of a number of
departments. Each department offers several
courses. A number of modules make up each
course. Students enrol in a particular course
and take modules towards the completion of
that course. Each module is taught by a
lecturer from the appropriate department, and
each lecturer tutors a group of students

IT221: Entity Relationship Modelling


Example - E/R Diagram
Entities: Department, Course, Module, Lecturer, Student
Department

Course Module Lecturer

Student

IT221: Entity Relationship Modelling


Example - E/R Diagram
Each department offers several courses
Offers Department

Course Module Lecturer

Student

IT221: Entity Relationship Modelling


Example - E/R Diagram
A number of modules make up each courses
Offers Department

Course Includes Module Lecturer

Student

IT221: Entity Relationship Modelling


Example - E/R Diagram
Students enrol in a particular course
Offers Department

Course Includes Module Lecturer

Enrols In Student

IT221: Entity Relationship Modelling


Example - E/R Diagram
Students … take modules
Offers Department

Course Includes Module Lecturer

Takes

Enrols In Student

IT221: Entity Relationship Modelling


Example - E/R Diagram
Each module is taught by a lecturer
Offers Department

Course Includes Module Teaches Lecturer

Takes

Enrols In Student

IT221: Entity Relationship Modelling


Example - E/R Diagram
a lecturer from the appropriate department
Offers Department Employs

Course Includes Module Teaches Lecturer

Takes

Enrols In Student

IT221: Entity Relationship Modelling


Example - E/R Diagram
each lecturer tutors a group of students
Offers Department Employs

Course Includes Module Teaches Lecturer

Takes

Enrols In Student Tutors

IT221: Entity Relationship Modelling


Example - E/R Diagram

Offers Department Employs

Course Includes Module Teaches Lecturer

Takes

Enrols In Student Tutors

IT221: Entity Relationship Modelling


Entities and Attributes
• Sometimes it is hard • General guidelines
to tell if something • Entities can have
should be an entity attributes but
or an attribute attributes have no
smaller parts
• They both represent
objects or facts about • Entities can have
the world relationships between
them, but an attribute
• They are both often belongs to a single
represented by nouns entity
in descriptions

IT221: Entity Relationship Modelling


Example
We want to represent information about
products in a database. Each product
has a description, a price and a
supplier. Suppliers have addresses,
phone numbers, and names. Each
address is made up of a street address,
a city, and a postcode.

IT221: Entity Relationship Modelling


Example - Entities/Attributes
• Entities or • Products, suppliers,
attributes: and addresses all
• product
have smaller parts
• description
• price
so we can make
• supplier them entities
• address • The others have no
• phone number
• name
smaller parts and
• street address belong to a single
• city entity
• postcode

IT221: Entity Relationship Modelling


Example - E/R Diagram
Price

Description Product

Street address

Name Supplier Address City

Phone number Postcode

IT221: Entity Relationship Modelling


Example - Relationships
• Each product has a • Each supplier has an
supplier address
• Each product has a • A supplier has a single
single supplier but address
there is nothing to • It does not seem
stop a supplier sensible for two
supplying many different suppliers to
products have the same
• A many to one address
relationship • A one to one
relationship

IT221: Entity Relationship Modelling


Example - E/R Diagram
Price

Description Product

Has A Street address

Name Supplier Has A Address City

Phone number Postcode

IT221: Entity Relationship Modelling


One to One Relationships
• Some relationships • Example - the
between entities, A supplier-address
and B, might be relationship
redundant if • Is one to one
• It is a 1:1 relationship • Every supplier has an
between A and B address
• Every A is related to a • We don’t need
B and every B is addresses that are not
related to an A related to a supplier

IT221: Entity Relationship Modelling


Redundant Relationships
• We can merge the a x

two entities that


b A B y
take part in a
redundant
c z
relationship together
• They become a single
entity a x
• The new entity has all
the attributes of the b AB y
old one
c z

IT221: Entity Relationship Modelling


Example - E/R Diagram
Price

Description Product

Has A

Name Supplier City

Phone number Postcode Street address

IT221: Entity Relationship Modelling


Making E/R Diagrams
• From a description of • Draw the E/R
the requirements diagram and then
identify the • Look at one to one
• Entities relationships as they
• Attributes might be redundant
• Relationships • Look at many to
many relationships as
• Cardinality ratios of they might need to be
the relationships split into two one to
many links

IT221: Entity Relationship Modelling


Debugging Designs
• With a bit of practice
Student
E/R diagrams can be
used to plan queries How can you
Has
• You can look at the
find a list of
diagram and figure
out how to find useful students who
Enrolment are enrolled
information
• If you can’t find the in Database
information you need, In systems?
you may need to
change the design
Module

IT221: Entity Relationship Modelling


Debugging Designs
ID (3) For each instance of Enrolment in the
Student
Name
result of (2) find the corresponding Student

Has

ID
(2) Find instances of the Enrolment entity
Enrolment
Code with the same Code as the result of (1)

In

Code
(1) Find the instance of the Module
Module
Title entity with title ‘Database Systems’
IT221: Entity Relationship Modelling
Home work
A database will be made to store information
about patients in a hospital. On arrival, each
patient’s personal details (name, address, and
telephone number) are recorded where possible,
and they are given an admission number. They
are then assigned to a particular ward (Accident
and Emergency, Cardiology, Oncology, etc.). In
each ward there are a number of doctors and
nurses. A patient will be treated by one doctor
and several nurses over the course of their stay,
and each doctor and nurse may be involved with
several patients at any given time.

IT221: Entity Relationship Modelling


Questions

1. Identify the entities, attributes,


relationships, and cardinality ratios
from the description.
2. Draw a rough entity-relationship
diagram showing the entities you
identified.
3. Resolve Many-to-many relationships
and draw the final key based ERD

IT221: Entity Relationship Modelling

You might also like