cs2300: file structures and introduction to database...

43
CS2300: File Structures and Introduction to Database Systems Lecture 5: Entity-Relationship Model Chapter 3 Doug McGeehan 1

Upload: others

Post on 14-Mar-2020

7 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS2300: File Structures and Introduction to Database Systemsdjmvfb/courses/cs2300/static/media/cs2300 - 05... · • We want to elaborate on the model for Beers, Taverns, and Drinkers:

CS2300: File Structures and

Introduction to Database Systems

Lecture 5: Entity-Relationship Model

Chapter 3

Doug McGeehan

1

Page 2: CS2300: File Structures and Introduction to Database Systemsdjmvfb/courses/cs2300/static/media/cs2300 - 05... · • We want to elaborate on the model for Beers, Taverns, and Drinkers:

2

Overview of Database Design Process

• Two Main Activities

– Database design

– Applications design

• We focus on Database Design

• Applications Design

– Focuses on the programs and interfaces that

access a database

– Generally considered part of software engineering

Page 3: CS2300: File Structures and Introduction to Database Systemsdjmvfb/courses/cs2300/static/media/cs2300 - 05... · • We want to elaborate on the model for Beers, Taverns, and Drinkers:

33

Purpose of ER Model

• Entity-Relational (ER) model– Informal sketch of a database

– A type of conceptual schema

• ER Diagram:a high-levelvisualizationof a database

Page 4: CS2300: File Structures and Introduction to Database Systemsdjmvfb/courses/cs2300/static/media/cs2300 - 05... · • We want to elaborate on the model for Beers, Taverns, and Drinkers:

ER Diagram

• Three main components

– Entities

– Attributes

– Relationships

4

Page 5: CS2300: File Structures and Introduction to Database Systemsdjmvfb/courses/cs2300/static/media/cs2300 - 05... · • We want to elaborate on the model for Beers, Taverns, and Drinkers:

ER Diagram: Entity

• Recall:

– An Entity is a collection of attributes

defining a particular object

• In the ER model:

– Same definition for Entity

– Entity type: the template for an entity

– Entity set: the entity instances

in a database

5

Page 6: CS2300: File Structures and Introduction to Database Systemsdjmvfb/courses/cs2300/static/media/cs2300 - 05... · • We want to elaborate on the model for Beers, Taverns, and Drinkers:

ER Diagram: Entity

• Recall:

– An Entity is a collection of attributes

defining a particular object

• In an ER Diagram:

– A box enclosing an entity type name

6

Student

Page 7: CS2300: File Structures and Introduction to Database Systemsdjmvfb/courses/cs2300/static/media/cs2300 - 05... · • We want to elaborate on the model for Beers, Taverns, and Drinkers:

Student

name phone

ER Diagram: Attribute

• ER diagram representation of a

simple attribute

– An ellipse enclosing an attribute name

7

Page 8: CS2300: File Structures and Introduction to Database Systemsdjmvfb/courses/cs2300/static/media/cs2300 - 05... · • We want to elaborate on the model for Beers, Taverns, and Drinkers:

• Several types of attributes

– Simple (aka Atomic)

– Composite

– Multi-valued

– Derived

ER Diagram: Attribute

8

Student

age

name

first

middle

last

phone

date of

birth

Page 9: CS2300: File Structures and Introduction to Database Systemsdjmvfb/courses/cs2300/static/media/cs2300 - 05... · • We want to elaborate on the model for Beers, Taverns, and Drinkers:

• Several types of attributes

– Key

• Value must be unique for each and every

individual entity

• Can be used to uniquely identify an entity

• ER diagram: underlined

ER Diagram: Attribute

9

Student

SSN

Page 10: CS2300: File Structures and Introduction to Database Systemsdjmvfb/courses/cs2300/static/media/cs2300 - 05... · • We want to elaborate on the model for Beers, Taverns, and Drinkers:

• Several types of attributes

– Key

• Multiple keys permitted, but each must satisfy

uniqueness on its own

ER Diagram: Attribute

10

Student

SSN

Student ID

License

Number

Page 11: CS2300: File Structures and Introduction to Database Systemsdjmvfb/courses/cs2300/static/media/cs2300 - 05... · • We want to elaborate on the model for Beers, Taverns, and Drinkers:

• Several types of attributes

– Composite key

• Provides uniqueness to an entity

by grouping together several attributes

• Combined attributes must be minimal

• i.e. Uniqueness requires every attribute in

group

ER Diagram: Attribute

11

Vehicle

VINRegistration

State

Tag No.

Page 12: CS2300: File Structures and Introduction to Database Systemsdjmvfb/courses/cs2300/static/media/cs2300 - 05... · • We want to elaborate on the model for Beers, Taverns, and Drinkers:

Exercise

• Identify the entities

present in the

scenario hand out

• Identify attributes

for these entities

• Don’t worry about

relationships yet

Page 13: CS2300: File Structures and Introduction to Database Systemsdjmvfb/courses/cs2300/static/media/cs2300 - 05... · • We want to elaborate on the model for Beers, Taverns, and Drinkers:

Exercise

• Are there attributes that suggest an

intuitive relationship between entities?

13

Page 14: CS2300: File Structures and Introduction to Database Systemsdjmvfb/courses/cs2300/static/media/cs2300 - 05... · • We want to elaborate on the model for Beers, Taverns, and Drinkers:

ER Diagram: Relationship

• Relationships associate two or more

entities

• ER diagram:

– A diamond enclosing a relationship name

14

Instructor SectionTEACHES

Page 15: CS2300: File Structures and Introduction to Database Systemsdjmvfb/courses/cs2300/static/media/cs2300 - 05... · • We want to elaborate on the model for Beers, Taverns, and Drinkers:

ER Diagram: Relationship

• Relationship type:

– Instructor TEACHES Section

• Relationship instance:

– “Doug McGeehan” TEACHES “CS23001A”

15

Instructor SectionTEACHES

Page 16: CS2300: File Structures and Introduction to Database Systemsdjmvfb/courses/cs2300/static/media/cs2300 - 05... · • We want to elaborate on the model for Beers, Taverns, and Drinkers:

ER Diagram: Relationship

• Constraints

– Rules governing participation of entities

in a relationship

– Typically defined by the mini-world being

represented

– A dog can be a stray (no owner)

or owned by one person

– A person can only have two biological

parents 16

Page 17: CS2300: File Structures and Introduction to Database Systemsdjmvfb/courses/cs2300/static/media/cs2300 - 05... · • We want to elaborate on the model for Beers, Taverns, and Drinkers:

ER Diagram: Relationship

• Constraints: Cardinality ratio

– The maximum number of relationship

instances in which an entity can participate

– “How many sections can an instructor

teach?”

– “How many instructors are teaching one

particular section?”

17

Instructor SectionTEACHES

Page 18: CS2300: File Structures and Introduction to Database Systemsdjmvfb/courses/cs2300/static/media/cs2300 - 05... · • We want to elaborate on the model for Beers, Taverns, and Drinkers:

ER Diagram: Relationship

• Constraints: Cardinality ratio

– Instructor:Section would be 1:N

• “One-to-many”

• Each instructor can teach many distinct

sections

• A single section can only be taught by one

instructor

18

Instructor SectionTEACHES1 N

Page 19: CS2300: File Structures and Introduction to Database Systemsdjmvfb/courses/cs2300/static/media/cs2300 - 05... · • We want to elaborate on the model for Beers, Taverns, and Drinkers:

ER Diagram: Relationship

• Constraints: Cardinality ratio

– 1:1 “One-to-One”

– 1:N “One-to-Many”

– N:1 “Many-to-One”

– M:N “Many-to-Many”

19

Page 20: CS2300: File Structures and Introduction to Database Systemsdjmvfb/courses/cs2300/static/media/cs2300 - 05... · • We want to elaborate on the model for Beers, Taverns, and Drinkers:

Exercise

• Who is enrolled in which course?

• What would the cardinality ratio of this

relationship type be?

20

Student CourseENROLLED IN

Page 21: CS2300: File Structures and Introduction to Database Systemsdjmvfb/courses/cs2300/static/media/cs2300 - 05... · • We want to elaborate on the model for Beers, Taverns, and Drinkers:

ER Diagram: Relationship

• Constraints: Participation

– Does an entity’s existence depend on its

relationship to another entity?

21

Student CourseENROLLED IN

Page 22: CS2300: File Structures and Introduction to Database Systemsdjmvfb/courses/cs2300/static/media/cs2300 - 05... · • We want to elaborate on the model for Beers, Taverns, and Drinkers:

ER Diagram: Relationship

• Constraints: Participation

– Total participation:

• Every student must be enrolled

in at least one course

• Every course needs enrolled students

22

Student CourseENROLLED IN

Page 23: CS2300: File Structures and Introduction to Database Systemsdjmvfb/courses/cs2300/static/media/cs2300 - 05... · • We want to elaborate on the model for Beers, Taverns, and Drinkers:

ER Diagram: Relationship

• Constraints: Participation

– Partial participation

• Every instructor does not necessarily need to

teach any sections of any courses

• But every section needs an instructor

23

Instructor SectionTEACHES

Partial Total

Page 24: CS2300: File Structures and Introduction to Database Systemsdjmvfb/courses/cs2300/static/media/cs2300 - 05... · • We want to elaborate on the model for Beers, Taverns, and Drinkers:

ER Diagram: Relationship

• Relationship roles

– What would you name a member variable

of a C++ class that references another

class?

24

Page 25: CS2300: File Structures and Introduction to Database Systemsdjmvfb/courses/cs2300/static/media/cs2300 - 05... · • We want to elaborate on the model for Beers, Taverns, and Drinkers:

ER Diagram: Relationship

• Relationship roles

– What would you name a member variable

of a C++ class that references another

class?

25Instructor SectionTEACHES

sectioninstructor

Instructor SectionTEACHEScurrently

teachingtaught by

Page 26: CS2300: File Structures and Introduction to Database Systemsdjmvfb/courses/cs2300/static/media/cs2300 - 05... · • We want to elaborate on the model for Beers, Taverns, and Drinkers:

ER Diagram: Relationship

• Recursive relationship

– A relationship with another entity of the

same type

26

Person

RENTS FROM

landlord tenant

Page 27: CS2300: File Structures and Introduction to Database Systemsdjmvfb/courses/cs2300/static/media/cs2300 - 05... · • We want to elaborate on the model for Beers, Taverns, and Drinkers:

ER Diagram: Relationship

• Relationship degree

– The number of entity types

that participate in a relationship type

27

Page 28: CS2300: File Structures and Introduction to Database Systemsdjmvfb/courses/cs2300/static/media/cs2300 - 05... · • We want to elaborate on the model for Beers, Taverns, and Drinkers:

ER Diagram: Relationship

• Relationship degree: Binary

28

Instructor SectionTEACHES

currently

teachingtaught by

Page 29: CS2300: File Structures and Introduction to Database Systemsdjmvfb/courses/cs2300/static/media/cs2300 - 05... · • We want to elaborate on the model for Beers, Taverns, and Drinkers:

ER Diagram: Relationship

• Relationship degree: Ternary

– Entities:

• Person

• Beer

• Tavern

– Relationship:

• Some people prefer to drink a particular beer

at a particular bar

• Better prices? Available food pairings?

29

Page 30: CS2300: File Structures and Introduction to Database Systemsdjmvfb/courses/cs2300/static/media/cs2300 - 05... · • We want to elaborate on the model for Beers, Taverns, and Drinkers:

ER Diagram: Relationship

• Relationship degree: Ternary

30

Tavern Beer

Person

PREFERS

Page 31: CS2300: File Structures and Introduction to Database Systemsdjmvfb/courses/cs2300/static/media/cs2300 - 05... · • We want to elaborate on the model for Beers, Taverns, and Drinkers:

ER Diagram: Relationship

• Relationships with Attributes:

• Can this attribute be moved to one of

the participating entity types? 31

Person DogADOPTED

adoption

date

Page 32: CS2300: File Structures and Introduction to Database Systemsdjmvfb/courses/cs2300/static/media/cs2300 - 05... · • We want to elaborate on the model for Beers, Taverns, and Drinkers:

ER Diagram: Weak Entity

Dependent

• An entity type that does not have a key

attribute

– 1. Double box the weak entity type

32

Employee DependentSUPPORTS

Name Age

Page 33: CS2300: File Structures and Introduction to Database Systemsdjmvfb/courses/cs2300/static/media/cs2300 - 05... · • We want to elaborate on the model for Beers, Taverns, and Drinkers:

ER Diagram: Weak Entity

Dependent

• An entity type that does not have a key

attribute

– 2. Underline partial key with dotted line

33

Employee DependentSUPPORTS

Name Age

Page 34: CS2300: File Structures and Introduction to Database Systemsdjmvfb/courses/cs2300/static/media/cs2300 - 05... · • We want to elaborate on the model for Beers, Taverns, and Drinkers:

SUPPORTS

ER Diagram: Weak Entity

Dependent

• An entity type that does not have a key

attribute

– 3. Identifying relationship becomes a

double diamond

34

Employee DependentSUPPORTS

Name Age

Page 35: CS2300: File Structures and Introduction to Database Systemsdjmvfb/courses/cs2300/static/media/cs2300 - 05... · • We want to elaborate on the model for Beers, Taverns, and Drinkers:

SUPPORTS

ER Diagram: Weak Entity

Dependent

• An entity type that does not have a key

attribute

– Can a dependent exist without its

associated employee?

– 4. Total participation of the

weak entity

35

Employee DependentSUPPORTS

Name Age

Page 36: CS2300: File Structures and Introduction to Database Systemsdjmvfb/courses/cs2300/static/media/cs2300 - 05... · • We want to elaborate on the model for Beers, Taverns, and Drinkers:

SUPPORTS

ER Diagram: Weak Entity

Dependent

• Composite attribute or Weak entity?

– Rule of thumb: weak entity if it participates

in other relationship types

36

Employee DependentSUPPORTS

Name Age

COVERSInsurance

Page 37: CS2300: File Structures and Introduction to Database Systemsdjmvfb/courses/cs2300/static/media/cs2300 - 05... · • We want to elaborate on the model for Beers, Taverns, and Drinkers:

ER Diagram: Weak Entity

• Composite attribute or Weak entity?

– Otherwise, composite attribute

37

Employee Dependent

Name Age

Page 38: CS2300: File Structures and Introduction to Database Systemsdjmvfb/courses/cs2300/static/media/cs2300 - 05... · • We want to elaborate on the model for Beers, Taverns, and Drinkers:

Exercise

• Incorporate relationships into our

database scenario’s diagram

38

Page 39: CS2300: File Structures and Introduction to Database Systemsdjmvfb/courses/cs2300/static/media/cs2300 - 05... · • We want to elaborate on the model for Beers, Taverns, and Drinkers:

39

Another Exercise

• We want to elaborate on the model for Beers,

Taverns, and Drinkers:

– A beer has a name, which is its identifier.

– A beer also has a manufacturer.

– A bar has a name, address and license number. Either

address or license number can be used as its identifier.

– A drinker has a name and a customer ID

– Taverns sell some beers.

– Drinkers enjoy some beers.

– Drinkers frequent some taverns

Page 40: CS2300: File Structures and Introduction to Database Systemsdjmvfb/courses/cs2300/static/media/cs2300 - 05... · • We want to elaborate on the model for Beers, Taverns, and Drinkers:

4040

Another Exercise

Drinker IDname

Beer

manfname

SELLS Bars sell somebeers.

ENJOYS

Drinkers enjoysome beers.FREQUENTS

Drinkers frequentsome taverns.

Tavern

name

license

addr

phone

Page 41: CS2300: File Structures and Introduction to Database Systemsdjmvfb/courses/cs2300/static/media/cs2300 - 05... · • We want to elaborate on the model for Beers, Taverns, and Drinkers:

Announcement

• Group formation

– Those not in a group will be grouped at

random

– Random groups should choose their team

leader themselves

– I’ll email random groups for their leader

• Homework #1 is coming soon

41

Page 42: CS2300: File Structures and Introduction to Database Systemsdjmvfb/courses/cs2300/static/media/cs2300 - 05... · • We want to elaborate on the model for Beers, Taverns, and Drinkers:

Announcement

• Quiz #1 is also coming soon

– Same day as homework is due

• Quiz and Homework will cover all

lectures up to the due date

• Closed book, 1 page crib sheet

• 15 minutes

42

Page 43: CS2300: File Structures and Introduction to Database Systemsdjmvfb/courses/cs2300/static/media/cs2300 - 05... · • We want to elaborate on the model for Beers, Taverns, and Drinkers:

43

What’s Next …

• More about Entity-Relationship (ER)

Model (continued)

• Good Design Principles

43