Вы находитесь на странице: 1из 41

Entity/Relationship Modelling

Database Systems Lecture 4


Natasha Alechina
In This Lecture
• Entity/Relationship models
• Entities and Attributes
• Relationships
• Attributes
• E/R Diagrams
• For more information
• Connolly and Begg chapter 11
• Ullman and Widom chapter 2

Entity Relationship Modelling


Database Design
• Before we look at • Conceptual design
how to create and • Build a model
use a database we’ll independent of the
look at how to choice of DBMS
design one • Logical design
• Need to consider • Create the database
in a given DBMS
• What tables, keys,
and constraints are • Physical design
needed? • How the database is
• What is the database stored in hardware
going to be used for?
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, Name,
of, an entity and Course, and could
have relationships with
• Relationships - links Modules (enrolment)
between entities and Lecturers
(tutor/tutee)

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

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 Steve Mills,
like modules, orders, Natasha Alechina are
courses, projects instances of Lecturer
• Attributes (such as
name, email address)

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

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, …

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

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

Entity Relationship Modelling


Cardinality Ratios
• Each entity in a • One to one (1:1)
relationship can • Each lecturer has a
unique office
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

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

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
Enrolment
into two one to
Studies

many relationships
• An entity represents Module
In

the M:M relationship


Module

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

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

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

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

Entity Relationship Modelling


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

Course Module Lecturer

Student

Entity Relationship Modelling


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

Course Module Lecturer

Student

Entity Relationship Modelling


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

Course Includes Module Lecturer

Student

Entity Relationship Modelling


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

Course Includes Module Lecturer

Enrols In Student

Entity Relationship Modelling


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

Course Includes Module Lecturer

Takes

Enrols In Student

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

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

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

Entity Relationship Modelling


Example - E/R Diagram

Offers Department Employs

Course Includes Module Teaches Lecturer

Takes

Enrols In Student Tutors

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

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.

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
smaller parts and
• name
• street address belong to a single
• city entity
• postcode

Entity Relationship Modelling


Example - E/R Diagram
Price

Description Product

Street address

Name Supplier Address City

Phone number Postcode

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

Entity Relationship Modelling


Example - E/R Diagram
Price

Description Product

Has A Street address

Name Supplier Has A Address City

Phone number Postcode

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

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

Entity Relationship Modelling


Example - E/R Diagram
Price

Description Product

Has A

Name Supplier City

Phone number Postcode Street address

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 they
• Cardinality ratios of might need to be split
the relationships into two one to many
links

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
information are enrolled
• If you can’t find the in Database
information you need, In systems?
you may need to
change the design
Module

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’
Entity Relationship Modelling
This Lecture in Exams
(and coursework last year)
“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.”

Entity Relationship Modelling


This Lecture in Exams
Identify the entities, attributes, relationships, and cardinality
ratios from the description.
(4 marks)
Draw an entity-relationship diagram showing the items you
identified.
(4 marks)
Many-to-many relationships are hard to represent in SQL tables.
Explain why many-to-many relationships cause problems in
SQL tables, and show how these problems may be
overcome.
(4 marks)

Entity Relationship Modelling


Next Lecture
• SQL
• The SQL language
• SQL, the relational model, and E/R diagrams
• CREATE TABLE
• Columns
• Primary Keys
• Foreign Keys
• For more information
• Connolly and Begg chapter 6
• Ullman and Widom chapter 6.5, 6.6

Entity Relationship Modelling