Sunteți pe pagina 1din 41

Entity/Relationship Modelling

Database Systems Lecture


Dr Mohammad Shahid
In This Lecture
• Entity/Relationship models
• Entities and Attributes
• Relationships
• Attributes
• E/R Diagrams

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,
of, an entity Name, and Course,...
and could have
• Relationships - links relationships with
between entities Modules (enrolment)
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


Initial Design of Entity
Types:
EMPLOYEE, DEPARTMENT, PROJECT,
DEPENDENT

Slide 3- 8
Entity Type CAR with two keys and a
corresponding Entity Set

Slide 3- 9
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


Example of a composite
attribute

Slide 3- 12
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


Many-to-one (N:1) Relationship

Slide 3- 15
Many-to-many (M:N)
Relationship

Slide 3- 16
A Recursive Relationship
Supervision`

Slide 3- 17
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


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


Summary of notation for ER
diagrams

Slide 3- 20
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


FIGURE 7.1
The ER conceptual schema diagram for the COMPANY database.

Slide 7- 39
FIGURE 7.2
Result of mapping the COMPANY ER schema into a relational
schema.

Slide 7- 40
Next Lecture
• SQL
• The SQL language
• SQL, the relational model, and E/R diagrams
• CREATE TABLE
• Columns
• Primary Keys
• Foreign Keys

Entity Relationship Modelling

S-ar putea să vă placă și