Lecture 2: Entity/Relationship modelling

29
1 Lecture 2: Entity/Relationship modelling www.cl.cam.ac.uk/Teaching/current/ Databases/ Dr. Peter Chen http://bit.csc.lsu.edu/~chen/

description

Lecture 2: Entity/Relationship modelling. www.cl.cam.ac.uk/Teaching/current/Databases/. Dr. Peter Chen http://bit.csc.lsu.edu/~chen/. Database design lifecycle. Requirements analysis User needs; what must database do? Conceptual design High-level description; often using E/R model - PowerPoint PPT Presentation

Transcript of Lecture 2: Entity/Relationship modelling

Page 1: Lecture 2:  Entity/Relationship modelling

1

Lecture 2: Entity/Relationship modelling

www.cl.cam.ac.uk/Teaching/current/Databases/

Dr. Peter Chenhttp://bit.csc.lsu.edu/~chen/

Page 2: Lecture 2:  Entity/Relationship modelling

2

Database design lifecycle• Requirements analysis

– User needs; what must database do?• Conceptual design

– High-level description; often using E/R model• Logical design

– Translate E/R model into (typically) relational schema• Schema refinement

– Check schema for redundancies and anomalies• Physical design/tuning

– Consider typical workloads, and further optimise

TodayToday

Page 3: Lecture 2:  Entity/Relationship modelling

3

Today’s lecture

• E/R modelling– Entities– Attributes– Relationships– Constraints on relationships

• Extended E/R modelling– Object ideas

Page 4: Lecture 2:  Entity/Relationship modelling

4

Conceptual Design

• What are the entities and relationships in the enterprise?

• What information about these entities and relationships should we store in the database?

• What are the integrity constraints (business rules) that hold?

• We can represent this information pictorially in E/R diagrams (and then map these to a relational schema later).

Page 5: Lecture 2:  Entity/Relationship modelling

5

E/R basics• An entity is a real-world object that is

distinguishable from other objects• Each entity has attributes (with domains)• A particular entity will have a value for each of its

attributes• An entity type defines a set of entities that have

the same attributes• An entity set is the collection of all entities of a

particular entity type (at a particular point in time)

Page 6: Lecture 2:  Entity/Relationship modelling

6

Entities and attributes

• Entity types are drawn as rectangles, e.g.

• Attributes are drawn as ovals, and attached to the entity sets with lines, e.g.

Employees

NI dobName

Employees

Page 7: Lecture 2:  Entity/Relationship modelling

7

Key attributes

• A key attribute of an entity type is an attribute whose values are distinct for each entity

• Sometimes several attributes (a composite attribute) together form a key– NB: Such a composite should be minimal

• We underline key attributesNI dobName

Employees

Page 8: Lecture 2:  Entity/Relationship modelling

8

Relationships

• A relationship type among two or more entity types defines a set of associations between entities from those types– Mathematically, relationship type R

R E1 En.

• The set of instances of the relationship type is called the relationship set

Page 9: Lecture 2:  Entity/Relationship modelling

9

Relationships in E/R

• Relationship types are represented by diamonds

• They connect the participating entity types with straight lines, e.g.

Works_in

NI dobName

Employees

DID budgetdname

Departments

Page 10: Lecture 2:  Entity/Relationship modelling

10

Relationship set diagrams

• Sometimes its useful to represent the relationship set diagrammatically

e1e2

e3

e4

e5

d1

d2

d3

d4

d5

r1

r2

r3

r4

… ……

Page 11: Lecture 2:  Entity/Relationship modelling

11

Relationship attributes

• Relationships can also have attributes– NB: A relationship must be uniquely

determined by the entities, without reference to the relationship attributes

Works_in

NI dobName

Employees

DID budgetdname

Departments

since

Page 12: Lecture 2:  Entity/Relationship modelling

12

N-ary relationships

• Although relatively rare, we can have n-ary relationships, e.g.

Works_in2

NI dobName

Employees

DID budgetdname

Department

since

Locationsaddress capacity

Page 13: Lecture 2:  Entity/Relationship modelling

13

Recursive relationships• Each entity type in a relationship plays a

particular role, which is associated with a role name (this is usually suppressed)

• An recursive relationship is when an entity type plays more than one role in the relationship type

• In this case the role name is required

Page 14: Lecture 2:  Entity/Relationship modelling

14

Recursive relationships in E/R

e.g.

Employees

Reports-to

NIname

dob

subordinatesupervisor

Page 15: Lecture 2:  Entity/Relationship modelling

15

Constraints on relationship types• For example:

– An employee can work in many departments; a department can have many employees

– In contrast, each department has at most one manager

• Thus we need to be able to specify the number of relationship instances that an entity can participate in.

• For binary relationships the possible ratios are: 1:1, 1:N, N:1, M:N

Page 16: Lecture 2:  Entity/Relationship modelling

16

Cardinality ratios1:1 1:N

M:N

Page 17: Lecture 2:  Entity/Relationship modelling

17

Cardinality ratios in E/R

M:N

N:1

1:1

M N

N 1

1 1

Note: Sometimes this is written using different arrowheads

Page 18: Lecture 2:  Entity/Relationship modelling

18

Participation constraints

Every department must have a manager

• This is an example of a participation constraint

• 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 not its participation is said to be partial)

Page 19: Lecture 2:  Entity/Relationship modelling

19

Participation in E/R diagrams

• Total participation is displayed as a bold line between the entity type and the relationship – NB. Sometimes this is written as a double line

Manages

NI dobName

Employees

DID budgetdname

Department

since

1 N

Page 20: Lecture 2:  Entity/Relationship modelling

20

Weak entity types

• An entity type may not have sufficient attributes to form a primary key

• Such an entity type is called a weak entity type

• A weak entity can only be identified uniquely by considering the primary key of another (owner) entity

Page 21: Lecture 2:  Entity/Relationship modelling

21

Weak entity types cont.

• Thus the owner and weak entity types must participate in a 1:N relationship

• Weak entity set must have total participation in this identifying relationship set.

Employees

NI Name

Policy Dependents

Cost pName age

1 N

Page 22: Lecture 2:  Entity/Relationship modelling

22

Extended E/R modelling

• What we’ve seen so far is “classic” E/R • Over the years a number of features have

been added to the model and the modelling process

• These features include:• Sub- and super-classes• Specialisation• Generalisation• Categories

• Higher/Lower-level entity sets• Attribute inheritance• Aggregation

Page 23: Lecture 2:  Entity/Relationship modelling

23

ISA hierarchies

• We can devise hierarchies for our entity types

• If we declareA ISA B, everyA entity is considered to be a B entity

Employees

ISA

Temp_Emp Contract_Emp

hoursrate cid

NI Name dob

Page 24: Lecture 2:  Entity/Relationship modelling

24

Attribute inheritance

• As we’d expect, attributes of superclasses are inherited by the subclasses.

• Thus: Temp_Emp also has attributes NI, Name and dob

• In fact, subclasses inherit relationships too

Page 25: Lecture 2:  Entity/Relationship modelling

25

Aggregation

• Suppose we have an entity set of Projects and that each project is sponsored by one or more departments; thus

Sponsors DepartmentsProjects

PIDstart

budget

since

budgetDIDdname

N M

Page 26: Lecture 2:  Entity/Relationship modelling

26

Aggregation cont.

• Suppose that employees are assigned to monitor a sponsorship

• Monitors should be a relationship between Employees and the Sponsors relationship

• Aggregation allows us to indicate that a relationship set participates in another relationship set

• Use dashed box

Page 27: Lecture 2:  Entity/Relationship modelling

27

Aggregation cont.

Sponsors DepartmentsProjects

PIDstart

budgetsince

budgetDIDdname

Employees

nameNI

Monitors until

N M

Page 28: Lecture 2:  Entity/Relationship modelling

28

A Data Model from the European Bioinformatics Institute (EBI)

See http://intact.sourceforge.net/uml/intactCore.gif

Page 29: Lecture 2:  Entity/Relationship modelling

29

Summary

You should now understand:• Database design lifecycle• Entities and attributes• Relationships

– Cardinality ratios– Participation constraints

• Weak entity types• ISA hierarchies & aggregationNext lecture: The relational model