Conceptual Design with ER Model - University of Wisconsin ...

61
1 Conceptual Design with ER Model Lecture #2 1/24/2012 Jeff Ballard CS564, Spring 2014, Database Management Systems

Transcript of Conceptual Design with ER Model - University of Wisconsin ...

Page 1: Conceptual Design with ER Model - University of Wisconsin ...

1

Conceptual Design with

ER Model

Lecture #2

1/24/2012

Jeff Ballard

CS564, Spring 2014, Database Management Systems

Page 2: Conceptual Design with ER Model - University of Wisconsin ...

Homework 1 is out

• See the Moodle page

• Due February 7

• Groups of 2-3 people

– Pick a team name

– Individual groups are acceptable

• If you choose to work alone, be warned, various phases of

this project are a lot of work and you will be expected to

complete all tasks

2

Page 3: Conceptual Design with ER Model - University of Wisconsin ...

3

Lecture Outline

• Steps in building a database application

• Conceptual design with ER model

Page 4: Conceptual Design with ER Model - University of Wisconsin ...

4

Steps in Building a DB Application

• Step 0: pick an application domain

• Step 1: conceptual design

– discuss what to model in the application domain

– need a modeling language to express what you want

– ER model is the most popular such language

– output: an ER diagram of the application domain

Page 5: Conceptual Design with ER Model - University of Wisconsin ...

5

Steps in Building a DB Application

• Step 2: pick a type of DBMS

– relational DBMS is most popular and is our focus

• Step 3: translate ER design to a relational schema

– use a set of rules to translate from ER to rel. schema

– use a set of schema refinement rules to transform the

above rel. schema into a good rel. schema

• At this point

– you have a good relational schema on paper

Page 6: Conceptual Design with ER Model - University of Wisconsin ...

6

Steps in Building a DB Application

• Subsequent steps include

– implement your relational DBMS using a "database

programming language" called SQL

– ordinary users cannot interact with the database

directly

– and the database also cannot do everything you want

– hence write your application program in C++, Java,

Perl, etc to handle the interaction and take care of

things that the database cannot do

• So, the first thing we should start with is to learn

ER model ...

Page 7: Conceptual Design with ER Model - University of Wisconsin ...

7

ER Model

• Gives us a language to specify

– what information the database must hold

– what are the relationships among components of that

information

• Proposed by Peter Chen in 1976

• What we will cover

– basic stuff

– constraints

– weak entity sets

– design principles

Page 8: Conceptual Design with ER Model - University of Wisconsin ...

8 address name ssn

Person

buys

makes

employs

Company

Product

name category

stockprice

name

price

Basic Concepts

Page 9: Conceptual Design with ER Model - University of Wisconsin ...

9

Entities and Attributes

• Entities

– real-world objects distinguishable from other objects

– described using a set of attributes

• Attributes

– each has an atomic domain: string, integers, reals, etc.

• Entity set: a collection of similar entities

Product

name category price

Company

stockprice

name

Page 10: Conceptual Design with ER Model - University of Wisconsin ...

10

Relations

• A mathematical definition:

– if A, B are sets, then a relation R is a subset of A × B

• A={1,2,3}, B={a,b,c,d},

R = {(1,a), (1,c), (3,b)}

makes is a subset of Product × Company:

1

2

3

a

b

c

d

A=

B=

makes Company Product

Page 11: Conceptual Design with ER Model - University of Wisconsin ...

11 address name ssn

Person

buys

makes

employs

Company

Product

name category

stockprice

name

price

ER Diagram

Page 12: Conceptual Design with ER Model - University of Wisconsin ...

12

More about relationships ...

Page 13: Conceptual Design with ER Model - University of Wisconsin ...

13

Multiplicity of E/R Relations

• one-one:

• many-one

• many-many

1

2

3

a

b

c

d

1

2

3

a

b

c

d

1

2

3

a

b

c

d

makes Company Product

Page 14: Conceptual Design with ER Model - University of Wisconsin ...

14

Important: Difference with the Book

• You should use the notation given in class, not

the book!

Manages Department Employee

• We will use

Manages Department Employee

• Cow book uses (see Page 33, 3rd edition)

Page 15: Conceptual Design with ER Model - University of Wisconsin ...

15

Multiway Relationships

How do we model a purchase relationship between buyers,

products and stores?

Purchase

Product

Person

Store

Can still model as a mathematical set (how ?)

Page 16: Conceptual Design with ER Model - University of Wisconsin ...

16

Q: what does the arrow mean ?

A: if I know the store, person, invoice, I know the movie too

Rental

VideoStore

Person

Movie

Invoice

Arrows in Multiway Relationships

Page 17: Conceptual Design with ER Model - University of Wisconsin ...

17

Q: what do these arrows mean ?

A: store, person, invoice determines movie and store, invoice, movie determines person

Rental

VideoStore

Person

Movie

Invoice

Arrows in Multiway Relationships

Page 18: Conceptual Design with ER Model - University of Wisconsin ...

18

Q: how do I say: “invoice determines store” ?

A: no good way; best approximation:

Q: Why is this incomplete ?

Rental

VideoStore

Person

Movie

Invoice

Arrows in Multiway Relationships

Page 19: Conceptual Design with ER Model - University of Wisconsin ...

19

Roles in Relationships

Purchase

What if we need an entity set twice in one relationship?

Product

Person

Store

salesperson buyer

Person

Page 20: Conceptual Design with ER Model - University of Wisconsin ...

20

Roles in Relationships

Purchase

What if we need an entity set twice in one relationship?

Product

Person

Store

salesperson buyer

Page 21: Conceptual Design with ER Model - University of Wisconsin ...

21

Attributes on Relationships

Purchase

Product

Person

Store

date

Page 22: Conceptual Design with ER Model - University of Wisconsin ...

22

Converting Multiway

Relationships to Binary

Purchase

Person

Store

Product

StoreOf

ProductOf

BuyerOf

date

Page 23: Conceptual Design with ER Model - University of Wisconsin ...

23

Relationships: Summary

• Modeled as a mathematical set

• Binary and multiway relationships

• Converting a multiway one into many binary

ones

• Constraints on the degree of the relationship

– many-one, one-one, many-many

– limitations of arrows

• Attributes of relationships

– not necessary, but useful

Page 24: Conceptual Design with ER Model - University of Wisconsin ...

24

Product

name category

price

isa isa

Educational Product Software Product

Age Group platforms

Subclasses in ER Diagrams

Page 25: Conceptual Design with ER Model - University of Wisconsin ...

25

Subclasses

• Subclass = special case = fewer entities = more

properties.

• Example: Ales are a kind of beer.

– Not every beer is an ale, but some are.

– Let us suppose that in addition to all the properties

(attributes and relationships) of beers, ales also have

the attribute color.

Page 26: Conceptual Design with ER Model - University of Wisconsin ...

26

Subclasses in ER Diagrams

• Assume subclasses form a tree.

– I.e., no multiple inheritance.

• Isa triangles indicate the subclass relationship.

– Point to the superclass.

Page 27: Conceptual Design with ER Model - University of Wisconsin ...

27

Example

Beers

Ales

isa

name manf

color

Page 28: Conceptual Design with ER Model - University of Wisconsin ...

28

ER Vs. Object Oriented Subclasses

• In the object-oriented world, objects are in one

class only.

– Subclasses inherit properties from superclasses.

• In contrast, E/R entities have components in all

subclasses to which they belong.

– Matters when we convert to relations.

Page 29: Conceptual Design with ER Model - University of Wisconsin ...

29

Example

Beers

Ales

isa

name manf

color

Pete’s Ale

Page 30: Conceptual Design with ER Model - University of Wisconsin ...

30

Constraints

• A constraint = an assertion about the database

that must be true at all times

• Part of the database schema

• Very important in database design

Page 31: Conceptual Design with ER Model - University of Wisconsin ...

31

Modeling Constraints Finding constraints is part of the modeling process.

Commonly used constraints:

Keys: Student ID number uniquely identifies a person.

Single-value constraints: a person can have only one biological father.

Referential integrity constraints: if you work for a company, it

must exist in the database.

Domain constraints: peoples’ ages are between 0 and 150.

General constraints: all others (at most 75 students enroll in a class)

Page 32: Conceptual Design with ER Model - University of Wisconsin ...

32

Why Constraints are Important?

• Give more semantics to the data

– help us better understand it

• Allow us to refer to entities (e.g, using keys)

• Enable efficient storage, data lookup, etc.

Page 33: Conceptual Design with ER Model - University of Wisconsin ...

33

Keys in E/R Diagrams

address name ssn

Person

Product

name category

price

No formal way

to specify multiple

keys in E/R diagrams

Underline:

Page 34: Conceptual Design with ER Model - University of Wisconsin ...

34

More about Keys

• Every entity set must have a key

– why?

• A key can consist of more than one attribute

• There can be more than one key for an entity set

– one key will be designated as primary key

• Requirement for key in an isa hierarchy

– not covered in this lecture

Page 35: Conceptual Design with ER Model - University of Wisconsin ...

35

Important: Difference with the Book

• And interprets this as a key constraint

Manages Department Employee

• Recall that when we have:

Manages Department Employee

• Cow book will write (see Page 33, 3rd edition):

Page 36: Conceptual Design with ER Model - University of Wisconsin ...

36

Important: Difference with the Book

• Because given a department ID, we can uniquely

identify the manage relationship it participates in

– (emp1, dept1)

(emp1, dept2)

(emp2, dept3)

(emp3, dept1)

Manages Department Employee

• Why?

Page 37: Conceptual Design with ER Model - University of Wisconsin ...

37

Important: Difference with the Book

• For us, key constraint = set of attributes that

uniquely identify entities in an entity set

Manages Department Employee

• However, we view this as many-one constraint

Manages Department Employee

Page 38: Conceptual Design with ER Model - University of Wisconsin ...

38

Single Value Constraint

• At most one value plays a particular role

• An attribute of a entity set has a single value

– we can specify if the value must be present or can be

missing (represented with say NULL or -1)

– example in real-estate domain

• price vs. house-style

• A many-one relation implies single value

constraint

Page 39: Conceptual Design with ER Model - University of Wisconsin ...

39

Referential Integrity Constraint

• Single value constraint: at most one value exists

in a given role

• Referential integrity constraint: exactly one value

exists in a given role

• An attribute has a non-NULL, single value

– this can be considered a kind of referential integrity

constraint

• However, we more commonly use such

constraints to refer to relationships

Page 40: Conceptual Design with ER Model - University of Wisconsin ...

40

Referential Integrity Constraints

• In some formalisms we may refer to another

object but get garbage instead

– e.g. a dangling pointer in C/C++

• the Referential Integrity Constraint on

relationships explicitly requires a reference to

exist

Page 41: Conceptual Design with ER Model - University of Wisconsin ...

41

Referential Integrity Constraints

Company Product makes

Company Product makes

• This will be even clearer once

we get to relational databases

Page 42: Conceptual Design with ER Model - University of Wisconsin ...

42

Other Kinds of Constraints

• Domain constraints

• Constraints on degree of a relationship

• Other more general constraints

• See the book

Page 43: Conceptual Design with ER Model - University of Wisconsin ...

43

Weak Entity Sets

Entity sets are weak when their key attributes come from other

classes to which they are related.

This happens if:

- part-of hierarchies

- splitting n-ary relations to binary.

University Team affiliation

number sport name

)

Page 44: Conceptual Design with ER Model - University of Wisconsin ...

44

Weak Entity Sets

• Occasionally, entities of an entity set need “help”

to identify them uniquely.

• Entity set E is said to be weak if in order to

identify entities of E uniquely, we need to follow

one or more many-one relationships from E and

include the key of the related entities from the

connected entity sets.

Page 45: Conceptual Design with ER Model - University of Wisconsin ...

45

Now, about design techniques ...

Page 46: Conceptual Design with ER Model - University of Wisconsin ...

46

Design Principles 1: Be Faithful

Purchase Product Person

President Person Country

Teaches Course Instructor

Page 47: Conceptual Design with ER Model - University of Wisconsin ...

47

Design Principles 2:

Avoid Redundancy

Purchase

Product

Store

date

personName personAddr

Page 48: Conceptual Design with ER Model - University of Wisconsin ...

48

Design Principles 3: KISS

Purchase

Product

Person

Store

date Dates

• Read the book for more design principles

Page 49: Conceptual Design with ER Model - University of Wisconsin ...

49

More on Design Techniques

(from Ullman's slides)

1. Avoid redundancy.

2. Limit the use of weak entity sets.

3. Don’t use an entity set when an attribute will

do.

Page 50: Conceptual Design with ER Model - University of Wisconsin ...

50

Avoiding Redundancy

• Redundancy occurs when we say the same thing

in two different ways.

• Redundancy wastes space and (more importantly)

encourages inconsistency.

– The two instances of the same fact may become

inconsistent if we change one and forget to change the

other, related version.

Page 51: Conceptual Design with ER Model - University of Wisconsin ...

51

Example: Good

Beers Manfs ManfBy

name

This design gives the address of each manufacturer exactly once.

name addr

Page 52: Conceptual Design with ER Model - University of Wisconsin ...

52

Example: Bad

Beers Manfs ManfBy

name

This design states the manufacturer of a beer twice: as an attribute and as a related entity.

name

manf

addr

Page 53: Conceptual Design with ER Model - University of Wisconsin ...

53

Example: Bad

Beers

name

This design repeats the manufacturer’s address once for each beer; loses the address if there are temporarily no beers for a manufacturer.

manf manfAddr

Page 54: Conceptual Design with ER Model - University of Wisconsin ...

54

Entity Sets Versus Attributes

• An entity set should satisfy at least one of the

following conditions:

– It is more than the name of something; it has at least

one non-key attribute.

or

– It is the “many” in a many-one or many-many

relationship.

Page 55: Conceptual Design with ER Model - University of Wisconsin ...

55

Example: Good

Beers Manfs ManfBy

name

•Manfs deserves to be an entity set because of the nonkey attribute addr.

•Beers deserves to be an entity set because it is the “many” of the many-one relationship ManfBy.

name addr

Page 56: Conceptual Design with ER Model - University of Wisconsin ...

56

Example: Good

Beers

name

There is no need to make the manufacturer an entity set, because we record nothing about manufacturers besides their name.

manf

Page 57: Conceptual Design with ER Model - University of Wisconsin ...

57

Example: Bad

Beers Manfs ManfBy

name

Since the manufacturer is nothing but a name, and is not at the “many” end of any relationship, it should not be an entity set.

name

Page 58: Conceptual Design with ER Model - University of Wisconsin ...

58

Don’t Overuse Weak Entity Sets

• Beginning database designers often doubt that

anything could be a key by itself.

– They make all entity sets weak, supported by all

other entity sets to which they are linked.

• In reality, we usually create unique ID’s for

entity sets.

– Examples include social-security numbers,

student-ID numbers, automobile VIN’s, etc.

Page 59: Conceptual Design with ER Model - University of Wisconsin ...

59

When Do We Need Weak Entity Sets?

• The usual reason is that there is no global

authority capable of creating unique ID’s.

• Example: it is unlikely that there could be an

agreement to assign unique player numbers

across all football teams in the world.

Page 60: Conceptual Design with ER Model - University of Wisconsin ...

60

ER Review

• Basic stuff

– entity, attribute, entity set

– relation: binary, multiway, converting from multiway

– relationship roles, attributes on relationships

– subclasses (is-a)

• Constraints

– on relations

• many-one, one-one, many-many

• limitations of arrows

– keys, single-valued, ref integrity, domain & general

constraints

Page 61: Conceptual Design with ER Model - University of Wisconsin ...

61

ER Review

• Weak entity sets

• Design principles

– be faithful

– avoid redundancy

– KISS