16
Databases Databases Entity-Relationship (E-R) Entity-Relationship (E-R) Diagrams Diagrams

Databases Entity-Relationship (E-R) Diagrams. Learning Objectives Draw entity-relationship (E-R) diagrams to represent data models diagrammatically. Design

Embed Size (px)

Citation preview

Page 1: Databases Entity-Relationship (E-R) Diagrams. Learning Objectives Draw entity-relationship (E-R) diagrams to represent data models diagrammatically. Design

DatabasesDatabases

Entity-Relationship (E-R) Entity-Relationship (E-R) DiagramsDiagrams

Page 2: Databases Entity-Relationship (E-R) Diagrams. Learning Objectives Draw entity-relationship (E-R) diagrams to represent data models diagrammatically. Design

Learning ObjectivesLearning Objectives

Draw entity-relationship (E-R) diagrams to represent data models diagrammatically.

Design simple relational databases to the third normal form (3NF).

Page 3: Databases Entity-Relationship (E-R) Diagrams. Learning Objectives Draw entity-relationship (E-R) diagrams to represent data models diagrammatically. Design

Data Modelling termsData Modelling terms

Entity:Entity: An object, person, event or ‘thing’ a data An object, person, event or ‘thing’ a data

model requires data to be recorded about. model requires data to be recorded about.

AttributeAttribute A property / characteristic of an entity.A property / characteristic of an entity. A field (column) in a database table.A field (column) in a database table.

RelationshipRelationship An association or link between two entities.An association or link between two entities.

Page 4: Databases Entity-Relationship (E-R) Diagrams. Learning Objectives Draw entity-relationship (E-R) diagrams to represent data models diagrammatically. Design

Entity-Relationship (E-R) Entity-Relationship (E-R) DiagramsDiagrams (Entity or data model)(Entity or data model)

A diagrammatic means of showing a data A diagrammatic means of showing a data model / database.model / database.

Illustrates the relationships between Illustrates the relationships between entities in the database and attributes of entities in the database and attributes of each entity.each entity.

Page 5: Databases Entity-Relationship (E-R) Diagrams. Learning Objectives Draw entity-relationship (E-R) diagrams to represent data models diagrammatically. Design

Example Data ModelExample Data Model

Every student in a school belongs to a Every student in a school belongs to a form.form.

Every student achieves many exam Every student achieves many exam grades.grades.

Every form has a form tutor and all the Every form has a form tutor and all the form tutors are members of the teaching form tutors are members of the teaching body.body.

Page 6: Databases Entity-Relationship (E-R) Diagrams. Learning Objectives Draw entity-relationship (E-R) diagrams to represent data models diagrammatically. Design

Entities & their relationships in this Entities & their relationships in this example data modelexample data model

STUDENTSTUDENT Many students can be in a FORM.Many students can be in a FORM. Many STUDENTs can achieve many grades.Many STUDENTs can achieve many grades.

GRADEGRADE Many GRADEs can be achieved by many STUDENTs.Many GRADEs can be achieved by many STUDENTs.

FORMFORM One form for many students (students cannot be members of more One form for many students (students cannot be members of more

than one form).than one form). A FORM can only have one TUTOR.A FORM can only have one TUTOR.

TUTORTUTOR A TUTOR can only be in one FORM.A TUTOR can only be in one FORM. Many TUTORS can be in the teaching body. Many TUTORS can be in the teaching body.

TEACHING BODYTEACHING BODY There is only one There is only one TEACHING BODYTEACHING BODY(teaching body) for all TUTORs.(teaching body) for all TUTORs.

N.B. All entities are written in the singular.

Page 7: Databases Entity-Relationship (E-R) Diagrams. Learning Objectives Draw entity-relationship (E-R) diagrams to represent data models diagrammatically. Design

Symbols for relationships between Symbols for relationships between entitiesentities

one-to-one one-to-one represented byrepresented by

one-to-manyone-to-many represented byrepresented by

many-to-onemany-to-one represented byrepresented by

many-to-manymany-to-many represented byrepresented by

Page 8: Databases Entity-Relationship (E-R) Diagrams. Learning Objectives Draw entity-relationship (E-R) diagrams to represent data models diagrammatically. Design

ER diagram for example data model ER diagram for example data model (not in 3NF (not in 3NF – see next slides for an explanation– see next slides for an explanation))

STUDENT FORM

TUTORTEACHING

BODY

GRADEachieved

by

achieves members of

has

has

has responsibility for

is a member

contains

Page 9: Databases Entity-Relationship (E-R) Diagrams. Learning Objectives Draw entity-relationship (E-R) diagrams to represent data models diagrammatically. Design

Forming Relationships between Forming Relationships between EntitiesEntities

1.1. Try many to many.Try many to many.

2.2. If that doesn’t work then try many to one.If that doesn’t work then try many to one.

3.3. If that doesn’t work try one to many.If that doesn’t work try one to many.

4.4. If that doesn’t work then it has to be one to If that doesn’t work then it has to be one to one.one.

Note: Note: I suggest this order of attempt as it helps avoid I suggest this order of attempt as it helps avoid

problems.problems.

Page 10: Databases Entity-Relationship (E-R) Diagrams. Learning Objectives Draw entity-relationship (E-R) diagrams to represent data models diagrammatically. Design

Database NormalizationDatabase Normalization

A technique for designing relational A technique for designing relational database tables to minimize duplication of database tables to minimize duplication of information which can lead to information which can lead to inconsistencies, leading to a loss of data inconsistencies, leading to a loss of data integrity.integrity.

Third Normal Form (Third Normal Form (3NF3NF) eradicates (or at ) eradicates (or at least minimizes) any data duplication.least minimizes) any data duplication.

Page 11: Databases Entity-Relationship (E-R) Diagrams. Learning Objectives Draw entity-relationship (E-R) diagrams to represent data models diagrammatically. Design

To change an ER diagram into To change an ER diagram into Third Normalised Form (Third Normalised Form (3NF3NF))

STUDENT FORM

TUTORTEACHING

BODY

GRADEachieved

by

achieves members of

has

has

has responsibility for

is a member

contains

One-to-one relationships should be removedOne-to-one relationships should be removed (but not all mark schemes appear to force this but I suggest you (but not all mark schemes appear to force this but I suggest you always do so anyway).always do so anyway).

•Removed by removing one of the entities and Removed by removing one of the entities and joining it to the entity that is left.joining it to the entity that is left.

ExampleExample

No many-to-many relationships.No many-to-many relationships.•Removed by placing a Removed by placing a link link in betweenin between which has a which has a many to onemany to one relationship to each of the other two entities.relationship to each of the other two entities.

Page 12: Databases Entity-Relationship (E-R) Diagrams. Learning Objectives Draw entity-relationship (E-R) diagrams to represent data models diagrammatically. Design

ER diagram for example data model ER diagram for example data model in 3NFin 3NF

STUDENT

FORM_TUTOR

GRADE STUDENT/GRADEachieved

by

achieves

has

has responsibility for

is a member

members of

TEACHINGBODY

LINK

Joint entity

Page 13: Databases Entity-Relationship (E-R) Diagrams. Learning Objectives Draw entity-relationship (E-R) diagrams to represent data models diagrammatically. Design

Third Normalised Form (Third Normalised Form (3NF3NF))

No many-to-many relationships.No many-to-many relationships. Removed by placing a Removed by placing a link link in betweenin between which which

has a has a many to onemany to one relationship to each of the relationship to each of the other two entities.other two entities.

One-to-one relationships should be One-to-one relationships should be removed removed (but not all mark schemes appear to force this but I (but not all mark schemes appear to force this but I

suggest you always do so anyway)suggest you always do so anyway).. Removed by removing one of the entities and Removed by removing one of the entities and

joining it to the entity that is left.joining it to the entity that is left.

Page 14: Databases Entity-Relationship (E-R) Diagrams. Learning Objectives Draw entity-relationship (E-R) diagrams to represent data models diagrammatically. Design

PlenaryPlenaryA sports club runs a number of sports teams.A sports club runs a number of sports teams.

Each team is made up of a number of members of Each team is made up of a number of members of the club and each member may play for more than the club and each member may play for more than one team. Each team has a number of coaches, one team. Each team has a number of coaches, but the coach’s job is so time consuming that each but the coach’s job is so time consuming that each coach can only coach one team.coach can only coach one team.

Represent the above information on an entity Represent the above information on an entity relation (ER) diagram, in 3rd normal form, stating relation (ER) diagram, in 3rd normal form, stating the primary key for each entity. the primary key for each entity.

You can either draw this on paper You can either draw this on paper oror use the relationship images and use the relationship images and text boxes on the next slide.text boxes on the next slide.

Page 15: Databases Entity-Relationship (E-R) Diagrams. Learning Objectives Draw entity-relationship (E-R) diagrams to represent data models diagrammatically. Design
Page 16: Databases Entity-Relationship (E-R) Diagrams. Learning Objectives Draw entity-relationship (E-R) diagrams to represent data models diagrammatically. Design

PlenaryPlenary