lecture 2: e/r diagrams and the relational model thursday, january 4, 2001

54
Lecture 2: E/R Diagrams and the Relational Model Thursday, January 4, 2001

Upload: cory-ezra-greene

Post on 30-Dec-2015

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lecture 2: E/R Diagrams and the Relational Model Thursday, January 4, 2001

Lecture 2: E/R Diagrams and the Relational Model

Thursday, January 4, 2001

Page 2: Lecture 2: E/R Diagrams and the Relational Model Thursday, January 4, 2001

Outline

• E/R Diagrams: 2.4 (except 2.4.2, 2.4.5), 2.5 (except 2.5.4)

• The Relational Model: 3.1, 3.2, 3.3

• E/R to Relational: 3.5

Page 3: Lecture 2: E/R Diagrams and the Relational Model Thursday, January 4, 2001

Entity / Relationship Diagrams in Summary

Product

address

buys

Entity sets:

Properties:

Relationships:

Page 4: Lecture 2: E/R Diagrams and the Relational Model Thursday, January 4, 2001

What is a Relation ?

• A mathematical definition:– if A, B are sets, then a relation R is a subset of

A x B

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

- makes is a subset of Product x Company:

1

2

3

a

b

c

d

A=

B=

makes CompanyProduct

Page 5: Lecture 2: E/R Diagrams and the Relational Model Thursday, January 4, 2001

Multiplicity of E/R Relations

• one-one:

• many-one

• many-many

123

abcd

123

abcd

123

abcd

Note: the book uses a non-standard notation

Page 6: Lecture 2: E/R Diagrams and the Relational Model Thursday, January 4, 2001

E/R Relationships and Relations in Mathematics

makes CompanyProduct

prod1prod2prod3

Company-aCompany-bCompany-cCompany-d

Product CompanyMakes

Makes = a subset of Product x Company

Page 7: Lecture 2: E/R Diagrams and the Relational Model Thursday, January 4, 2001

Attributes on Relationshiops

makes CompanyProduct

prod1prod2prod3

Company-aCompany-bCompany-cCompany-d

Product CompanyMakes

price

$1

$200

$50

$50

Makes = still a subset of Product x Company,not of Product x Company x Price (what does this imply ?)

Page 8: Lecture 2: E/R Diagrams and the Relational Model Thursday, January 4, 2001

Multi-way RelationshipsHow do we model a purchase relationship between buyers,products and stores?

Purchase

Product

Person

Store

Can still model as a mathematical set (how ?)

Page 9: Lecture 2: E/R Diagrams and the Relational Model Thursday, January 4, 2001

Arrows in Multi-way Relationships (not in the book)

Purchase

Product

Person

Store

“A person buys aproduct at most once”

Limited expressive power.Cannot say: “a person buys at most one product”

Page 10: Lecture 2: E/R Diagrams and the Relational Model Thursday, January 4, 2001

• 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 11: Lecture 2: E/R Diagrams and the Relational Model Thursday, January 4, 2001

• Q: what do these arrow mean ?

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

Rental

VideoStore

Person

Movie

Invoice

Arrows in Multiway Relationships

Page 12: Lecture 2: E/R Diagrams and the Relational Model Thursday, January 4, 2001

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

• A: no good way; best approximation:

• Why is this incomplete ?

Rental

VideoStore

Person

Movie

Invoice

Arrows in Multiway Relationships

Page 13: Lecture 2: E/R Diagrams and the Relational Model Thursday, January 4, 2001

Roles in Relationships

Purchase

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

Product

Person

Store

salesperson buyer

Page 14: Lecture 2: E/R Diagrams and the Relational Model Thursday, January 4, 2001

Converting Multi-way Relationships to Binary

Purchase

Product

Person

Store

date

Page 15: Lecture 2: E/R Diagrams and the Relational Model Thursday, January 4, 2001

Converting Multi-way Relationships to Binary

Purchase

Person

Store

Product

StoreOf

ProductOf

BuyerOf

Moral: Find a nice way to say things.

date

Page 16: Lecture 2: E/R Diagrams and the Relational Model Thursday, January 4, 2001

Design Principles

PurchaseProduct Person

What’s wrong?

President PersonCountry

Moral: be faithful!

Page 17: Lecture 2: E/R Diagrams and the Relational Model Thursday, January 4, 2001

What’s Wrong?

Purchase

Product

Store

date

personNamepersonAddr

Moral: pick the right kind of elements.

Page 18: Lecture 2: E/R Diagrams and the Relational Model Thursday, January 4, 2001

What’s Wrong?

Purchase

Product

Person

Store

dateDates

Moral: don’t complicate life more than it already is.

Page 19: Lecture 2: E/R Diagrams and the Relational Model Thursday, January 4, 2001

Class Hierarchies

Some objects (entities) in a class may be special•define a new class•better: define a subclass

Products

Software products

Educational products

We define subclasses in E/R

Page 20: Lecture 2: E/R Diagrams and the Relational Model Thursday, January 4, 2001

Understanding Subclasses

• Think in terms of records:– Product

– SoftwareProduct

– EducationalProduct

field1

field2

field1

field2

field1

field2

field3

field4field5

Page 21: Lecture 2: E/R Diagrams and the Relational Model Thursday, January 4, 2001

Product

name category

price

isa isa

Educational ProductSoftware Product

Age Groupplatforms

Subclasses in E/R Diagrams

Page 22: Lecture 2: E/R Diagrams and the Relational Model Thursday, January 4, 2001

• C++: classes are disjoint

p1 p2

p3sp1

sp2

ep1

ep2

ep3

Difference between C++ and E/R inheritance

Product

SoftwareProductEducationalProduct

Page 23: Lecture 2: E/R Diagrams and the Relational Model Thursday, January 4, 2001

• E/R: entity sets overlap

Difference between ODL and E/R inheritance

SoftwareProduct

EducationalProduct

p1 p2

p3sp1

sp2

ep1

ep2

ep3

Product

Page 24: Lecture 2: E/R Diagrams and the Relational Model Thursday, January 4, 2001

• No need for multiple inheritance in E/R

• we have three entity sets, but four different kinds of objects

SoftwareProduct

EducationalProduct

p1 p2

p3sp1

sp2

ep1

ep2

ep3

Product

esp1 esp2

Page 25: Lecture 2: E/R Diagrams and the Relational Model Thursday, January 4, 2001

Modeling Union Types With Subclasses

FurniturePiece

Person Company

Say: each piece of furniture is owned either by a person, or by a company

Page 26: Lecture 2: E/R Diagrams and the Relational Model Thursday, January 4, 2001

Modeling Union Types with Subclasses

Say: each piece of furniture is owned either by a person, or by a company

Solution 1. Acceptable, imperfect (What’s wrong ?)

FurniturePiecePerson Company

ownedByPerson ownedByPerson

Page 27: Lecture 2: E/R Diagrams and the Relational Model Thursday, January 4, 2001

Modeling Union Types with Subclasses

Solution 2: better, more laborious

isaFurniturePiece

Person Company

ownedBy

Owner isa

Page 28: Lecture 2: E/R Diagrams and the Relational Model Thursday, January 4, 2001

Constraints

• A constraint = an assertion about the database that must be true at all times

• part of the db schema

• types in programming languages do not have anything similar

• correspond to invariants in programming languages

Page 29: Lecture 2: E/R Diagrams and the Relational Model Thursday, January 4, 2001

Common Constraints in E/R

Keys: social security number uniquely identifies a person.

Single-value constraints: e.g. one-one, one-many, many-one

Participation constrain: total many-one

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

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

Page 30: Lecture 2: E/R Diagrams and the Relational Model Thursday, January 4, 2001

KeysA set of attributes that uniquely identify an object or entity:

Person: ssn name name + address name + address + age

Perfect keys are often hard to find, so organizations usuallyinvent something.

An object may have multiple keys:

employee number, ssn

Page 31: Lecture 2: E/R Diagrams and the Relational Model Thursday, January 4, 2001

Keys in E/R Diagrams

address name ssn

Person

Product

name category

price

No formal way to specify multiple keys in E/R diagrams

Page 32: Lecture 2: E/R Diagrams and the Relational Model Thursday, January 4, 2001

Single Value Constraints in E/R

makes CompanyProduct

A product is made by at most one company:

Notice: some products are not made by any company

Page 33: Lecture 2: E/R Diagrams and the Relational Model Thursday, January 4, 2001

Participation Constraint

makes CompanyProduct

makes CompanyProduct

Each product is made by a lest one company

(notation from the book)

Each product is made by exactly one company

This: also called referential integrity constraint

Page 34: Lecture 2: E/R Diagrams and the Relational Model Thursday, January 4, 2001

Referential Integrity Constraint

• Another notation (in Ullman’s book):

CompanyProduct makes

CompanyProduct makes

Page 35: Lecture 2: E/R Diagrams and the Relational Model Thursday, January 4, 2001

Weak Entity SetsEntity sets are weak when their key attributes come from otherclasses to which they are related.

This happens if:

- part-of hierarchies - splitting n-ary relations to binary.

UniversityTeam affiliation

numbersport name

Page 36: Lecture 2: E/R Diagrams and the Relational Model Thursday, January 4, 2001

The Relational Data ModelDatabase Model(E/R)

Relational Schema

Physicalstorage

Diagrams (E/R) Tables: column names: attributes rows: tuples

Complexfile organizationand index structures.

Page 37: Lecture 2: E/R Diagrams and the Relational Model Thursday, January 4, 2001

Terminology

Name Price Category Manufacturer

gizmo $19.99 gadgets GizmoWorks

Power gizmo $29.99 gadgets GizmoWorks

SingleTouch $149.99 photography Canon

MultiTouch $203.99 household Hitachi

tuples

Attribute namesTable name

Products:

Page 38: Lecture 2: E/R Diagrams and the Relational Model Thursday, January 4, 2001

Domains

• each attribute has a type• must be atomic type (why ? see later)• called domain • examples:

– Integer– String– Real– …

Page 39: Lecture 2: E/R Diagrams and the Relational Model Thursday, January 4, 2001

Schemas

• Relational Schema:

– Relation name plus attribute names

– E.g. Product(Name, Price, Category, Manufacturer)

– In practice we add the domain for each attribute

• Database Schema:

– Set of relational schemas

– E.g. Product(Name, Price, Category, Manufacturer) Vendor(Name, Address, Phone)

Page 40: Lecture 2: E/R Diagrams and the Relational Model Thursday, January 4, 2001

Instances

• An instance of a relational schema R(A1,…,Ak), is a relation with k attributes with values of corresponding domains

• An instance of a database schema R1(…), R2(…), …, Rn(…), consists of n relations, each an instance of the corresponding relational schema.

Page 41: Lecture 2: E/R Diagrams and the Relational Model Thursday, January 4, 2001

Example

Name Price Category Manufacturer

gizmo $19.99 gadgets GizmoWorks

Power gizmo $29.99 gadgets GizmoWorks

SingleTouch $149.99 photography Canon

MultiTouch $203.99 household Hitachi

Relational schema:Product(Name, Price, Category, Manufacturer)Instance:

Page 42: Lecture 2: E/R Diagrams and the Relational Model Thursday, January 4, 2001

Schemas and Instances

• Analogy with programming languages:– Schema = type– Instance = value

• Important distinction:– Database Schema = stable over long periods of time– Database Instance = changes constantly, as data is

inserted/updated/deleted

Page 43: Lecture 2: E/R Diagrams and the Relational Model Thursday, January 4, 2001

Two Mathematical Definitions of Relations

• Relation as cartesian product– tuple = element of string x int x string x string– E.g. t = (gizmo, 19, gadgets, GizmoWorks)– Product is subset of string x int x string x

string– Order in the tuple is important !

• (gizmo, 19, gadgets, GizmoWorks)

• (gizmo, 19 , GizmoWorks, gadgets)

– No attributes

Page 44: Lecture 2: E/R Diagrams and the Relational Model Thursday, January 4, 2001

Two Mathematical Definitions of Relations

• Relation as a set of functions– Fix the set of attributes

• A={name , price, category, manufacturer}

– A tuple is a function t:A Domains– Relation = set of tuples– E.g.

– Order in a tuple is not important– Attribute names are important

{name gizmo, price 19, category gadgets, manufacturer gizmoWorks}

Page 45: Lecture 2: E/R Diagrams and the Relational Model Thursday, January 4, 2001

Two Definitions of Relations

• We will switch back and forth between these two:– Relational schemas with attribute names– Positional tuples, without attribute names

Page 46: Lecture 2: E/R Diagrams and the Relational Model Thursday, January 4, 2001

From E/R Diagrams to Relational Schema

• Each entity set relation

• Each relationship relation

• Special cases:– one-one, many-one relationships– subclasses– weak entity sets

Page 47: Lecture 2: E/R Diagrams and the Relational Model Thursday, January 4, 2001

address name ssn

Person

buys

makes

employs

CompanyProduct

name category

Stock price

name

price

Page 48: Lecture 2: E/R Diagrams and the Relational Model Thursday, January 4, 2001

Entity Sets to Relations

Product

name category

price

Product:

Name Category Price

gizmo gadgets $19.99

Page 49: Lecture 2: E/R Diagrams and the Relational Model Thursday, January 4, 2001

Relationships to Relations

makes CompanyProduct

name category

Stock price

name

Relations: Product, Makes, Company:

Product-name Product-Category Company-name Starting-year

gizmo gadgets gizmoWorks 1963

Start Year

price

Page 50: Lecture 2: E/R Diagrams and the Relational Model Thursday, January 4, 2001

Many-one Relationships

makes CompanyProduct

name category

Stock price

name

No need for Makes. Just modify Product:

name category price StartYear companyName

gizmo gadgets 19.99 1963 gizmoWorks

Start Year

price

Page 51: Lecture 2: E/R Diagrams and the Relational Model Thursday, January 4, 2001

Handling Weak Entity Sets

UniversityTeam affiliation

numbersport name

Relation Team:

Sport Number Affiliated University

mud wrestling 15 Montezuma State U.

- need all the attributes that contribute to the key of Team - don’t need a separate relation for Affiliation. (why ?)

Page 52: Lecture 2: E/R Diagrams and the Relational Model Thursday, January 4, 2001

Modeling Subclass Structure

Product

Educational Product

SoftwareProduct

ageGrouptopic

Platformsrequired memory isaisa

Page 53: Lecture 2: E/R Diagrams and the Relational Model Thursday, January 4, 2001

The right way

Product(name, price, category, manufacturer)

EducationalProduct( name, ageGroup, topic)

SoftwareProduct( name, platforms, requiredMemory)

Notice: each subclass stores only the additional attributes

Page 54: Lecture 2: E/R Diagrams and the Relational Model Thursday, January 4, 2001

Option #2: The Null Value Approach

Have one table:

Product ( name, price, manufacturer, age-group, topic, platforms, required-memory, educational-method)

Some values in the table will be NULL, meaning that the attribute not make sense for the specific product.