25
Data Modelling – ERD Data Modelling – ERD Entity Relationship Entity Relationship Diagram’s Diagram’s Entity Relationship Diagrams and how to create them. 1

Data Modelling – ERD Entity Relationship Diagram’s Entity Relationship Diagrams and how to create them. 1

Embed Size (px)

Citation preview

Page 1: Data Modelling – ERD Entity Relationship Diagram’s Entity Relationship Diagrams and how to create them. 1

Data Modelling – ERDData Modelling – ERDEntity Relationship Entity Relationship Diagram’sDiagram’s

Entity Relationship Diagrams and how to create them.

1

Page 2: Data Modelling – ERD Entity Relationship Diagram’s Entity Relationship Diagrams and how to create them. 1

What is an Entity Relationship What is an Entity Relationship Diagram?Diagram?Drawing technique used to analyse the

logical structure of an organisations information (e.g. database).

An entity is an object or concept about which the system needs to hold information eg ..............................................

Each entity must be involved in at least one relationship, and may be involved in more than one relationship.

2

Page 3: Data Modelling – ERD Entity Relationship Diagram’s Entity Relationship Diagrams and how to create them. 1

RelationshipsRelationshipsWhat information will be kept in the

system? The entities and their attributes.

Eg of attributes .............................................

For example, how do we represent a relationship between a book copy and a borrower in a library?Do we wish to say:• “A book copy may be borrowed by only one

borrower” or • “A book copy may be borrowed by one or

more borrowers” ?3

Page 4: Data Modelling – ERD Entity Relationship Diagram’s Entity Relationship Diagrams and how to create them. 1

Entity Relationship Entity Relationship Diagram’sDiagram’sPairs of entities are linked.All entities in the system relate

directly or indirectly with all the others.

How many relationships may exist between linked entities?

This consideration is called multiplicity or cardinality.

4

Page 5: Data Modelling – ERD Entity Relationship Diagram’s Entity Relationship Diagrams and how to create them. 1

Types of RelationshipsTypes of Relationships

There are three ways that entities can be linked together:One-to-oneOne-to-manyMany-to-many

5

Page 6: Data Modelling – ERD Entity Relationship Diagram’s Entity Relationship Diagrams and how to create them. 1

One-to-One RelationshipOne-to-One RelationshipOne occurrence of an entity ‘A’ is

related to one and only one occurrence of the entity ‘B’ and

One occurrence of an entity ‘B’ is related to one and only one occurrence of the entity ‘A’

6

Page 7: Data Modelling – ERD Entity Relationship Diagram’s Entity Relationship Diagrams and how to create them. 1

One-to-One continuedOne-to-One continuedThere are two link phrases used on the

relationship.Use the phrase ‘one and only one’ rather than

‘one’ to emphasise the cardinality existing at the both ends of this relationship.

Consider joining the two entities.We might have one entity called School that

would include all the attributes of the Head Teacher.

This would have the advantage of reducing the number of entities.

But it might make the model produced less clear.7

Page 8: Data Modelling – ERD Entity Relationship Diagram’s Entity Relationship Diagrams and how to create them. 1

One-to-Many RelationshipOne-to-Many RelationshipOne occurrence of an entity ‘A’ is

linked to one or more occurrences of another entity ‘B’ and

One occurrence of entity ‘B’ is linked to one and only one occurrence of entity ‘A’

8

Page 9: Data Modelling – ERD Entity Relationship Diagram’s Entity Relationship Diagrams and how to create them. 1

Many-to-Many Many-to-Many RelationshipsRelationshipsOne occurrence of an entity ‘A’ is

linked to one or more occurrences of another entity ‘B’ and

One occurrence of entity ‘B’ is linked to one or more occurrences of entity ‘A’

9

Page 10: Data Modelling – ERD Entity Relationship Diagram’s Entity Relationship Diagrams and how to create them. 1

Deciding on RelationshipsDeciding on RelationshipsWhich pairs of entities have a direct

relationship?Looking out for verbs – ‘A stockholder

of the company owns shares in that company’

Sometimes we might not want to include a direct relationship between two entities because an indirect relationship already exists - ‘Customer buys Products’

We may already have - ‘Customer places Order’ and ‘Order lists Products’

10

Page 11: Data Modelling – ERD Entity Relationship Diagram’s Entity Relationship Diagrams and how to create them. 1

The Problem with Many-The Problem with Many-to-Manyto-Many

11

Page 12: Data Modelling – ERD Entity Relationship Diagram’s Entity Relationship Diagrams and how to create them. 1

The Problem with Many-The Problem with Many-to-Manyto-ManyProject Code is included in the

engineer table to provide a link between the two.

What happens if an engineer works on two projects at the same time? We would need more than one value in the Project Code? (engineer ID51 works on both project W45 and Y65).

Solution: form another table which links the two tables together.

12

Page 13: Data Modelling – ERD Entity Relationship Diagram’s Entity Relationship Diagrams and how to create them. 1

Solving Many-to-ManySolving Many-to-ManyAny engineer may be linked via an assignment table to a number of projects.

Any project may have more than one engineer working on it.

We need a link entity.Sometimes the name of this link entity is difficult to work out.

13

Page 14: Data Modelling – ERD Entity Relationship Diagram’s Entity Relationship Diagrams and how to create them. 1

The SolutionThe SolutionWe now have 2 one-to-many

relationships instead of 1 many-to-many relationship.

14

Page 15: Data Modelling – ERD Entity Relationship Diagram’s Entity Relationship Diagrams and how to create them. 1

Mandatory and Optional Mandatory and Optional RelationshipsRelationships

In this case we interpret the relationship as:A School employs one or more Teachers.A Teacher is employed at one and only one School.

15

Page 16: Data Modelling – ERD Entity Relationship Diagram’s Entity Relationship Diagrams and how to create them. 1

Mandatory and Optional Mandatory and Optional RelationshipsRelationships

Suppose some teachers are employed in more than one school - they may move around within an area and visit a number of schools.

It is possible, that a teacher is not currently employed - that particular teacher would not participate in this relationship.

16

Page 17: Data Modelling – ERD Entity Relationship Diagram’s Entity Relationship Diagrams and how to create them. 1

Mandatory and Optional Mandatory and Optional SyntaxSyntax

A [name of entity] may / must ‘link phrase’ (one and only one) / (one or more) [name of entity]

A School must employ many Teachers.A Teacher may be employed in one School.

17

Page 18: Data Modelling – ERD Entity Relationship Diagram’s Entity Relationship Diagrams and how to create them. 1

Referential IntegrityReferential Integrity

We insist on the mandatory part of the relationship between Order and Customer.

We do not want an Order created which is not related to a particular Customer.

This is known as referential integrity.All databases support this structure.

18

Page 19: Data Modelling – ERD Entity Relationship Diagram’s Entity Relationship Diagrams and how to create them. 1

Referential IntegrityReferential IntegrityWhen a user enters a new order

it must relate to an existing customer.

We cannot delete a customer for which there are existing orders.

We can enter a customer without requiring an order to be related to that customer.

19

Page 20: Data Modelling – ERD Entity Relationship Diagram’s Entity Relationship Diagrams and how to create them. 1

Attributes and Primary Attributes and Primary KeysKeysYou will assigned data types to

attributes.You must created primary keys for

tables.You can then link tables together.

20

Page 21: Data Modelling – ERD Entity Relationship Diagram’s Entity Relationship Diagrams and how to create them. 1

Foreign KeysForeign KeysAn Order may not be produced which is not

related to an existing customer. Therefore, every time we enter a new order we must identify an existing Customer ID, so Customer ID would be a field on the Order table.

This is called a Foreign key as Customer ID is a primary key on the Customer table.

Foreign keys are identified by an asterisk *If we relaxed this condition and did not insist

on referential integrity we would be allowed to enter no-value for this foreign key.

Wherever possible we will endeavour to use referential integrity.

21

Page 22: Data Modelling – ERD Entity Relationship Diagram’s Entity Relationship Diagrams and how to create them. 1

Entity Attribute Relationship Entity Attribute Relationship ModelModel Select the entities. Identify relationships between

entities (including decisions about mandatory and optional relationships).

Resolve any many-to-many relationships.

List the entities with their attributes. Identify suitable primary keys. Place the required foreign keys in the

detail entities.22

Page 23: Data Modelling – ERD Entity Relationship Diagram’s Entity Relationship Diagrams and how to create them. 1

Example – Steps 1 to 3Example – Steps 1 to 3

1. Select the entities. 2. Populate the entities with

attributes.3. Identify suitable primary keys.

23

Page 24: Data Modelling – ERD Entity Relationship Diagram’s Entity Relationship Diagrams and how to create them. 1

Example – Steps 4 to 6Example – Steps 4 to 64.Identify relationships between entities.5.Resolve any many-to-many relationships. 6.Place the required foreign keys in the detail

entities.

24

Page 25: Data Modelling – ERD Entity Relationship Diagram’s Entity Relationship Diagrams and how to create them. 1

Things to NoteThings to Note Course ID is a foreign key on Course Offering

table and Course ID is used as part of the primary key.

Employee Number is used as a foreign key on the Booking table.

Other foreign keys need to be included.

It is now possible to answer questions such as: Who made this Booking?

Which Course Offering is this booking for?

Which Courses is a Staff Member booked to study?

25