C20.0046: Database Management Systems Lecture #5

34
M.P. Johnson, DBMS, Stern/NYU, Sprin g 2005 1 C20.0046: Database Management Systems Lecture #5 Matthew P. Johnson Stern School of Business, NYU Spring, 2005

description

C20.0046: Database Management Systems Lecture #5. Matthew P. Johnson Stern School of Business, NYU Spring, 2005. Crew. StudioName. Crew_ID. Miramax. C1. Miramax. C2. Disney. C1. Converting weak ESs – differences. Atts of Crew Rel are: attributes of Crew - PowerPoint PPT Presentation

Transcript of C20.0046: Database Management Systems Lecture #5

Page 1: C20.0046: Database Management Systems Lecture #5

M.P. Johnson, DBMS, Stern/NYU, Spring 2005

1

C20.0046: Database Management SystemsLecture #5

Matthew P. JohnsonStern School of Business, NYUSpring, 2005

Page 2: C20.0046: Database Management Systems Lecture #5

M.P. Johnson, DBMS, Stern/NYU, Spring 2005

2

Converting weak ESs – differences

Atts of Crew Rel are: attributes of Crew key attributes of

supporting ESs

Crew Unit-of Studio

StudioNameCrew_ID address

C2MiramaxC1Disney

C1Miramax

Crew_IDStudioNameCrew

Supporting relships may be omitted (why?)

Page 3: C20.0046: Database Management Systems Lecture #5

M.P. Johnson, DBMS, Stern/NYU, Spring 2005

3

Weak entity sets - relationships

Crew Studio

StudioNameCrew_ID address

Insurance

IName

Address 1260 7th Av.NYBlueCross1250 6th Av.NYAetna

AddressINameInsuranceSubscribes

Unit-of

Page 4: C20.0046: Database Management Systems Lecture #5

M.P. Johnson, DBMS, Stern/NYU, Spring 2005

4

Weak entity sets - relationships Non-supporting relationships for weak ESs

are converted keys include entire weak ES key

C21C22C21

Crew_ID

AetnaBlueCrossAetna

Insurer

UniversalDisney

Universal

StudioNameSubscribes

Page 5: C20.0046: Database Management Systems Lecture #5

M.P. Johnson, DBMS, Stern/NYU, Spring 2005

5

Conversion example Video store rental example, plus some atts

Q: Conversion to relations?

Rental

VideoStore

Customer

Movie

date

yearMNameaddress

Cname

MID

Page 6: C20.0046: Database Management Systems Lecture #5

M.P. Johnson, DBMS, Stern/NYU, Spring 2005

6

Conversion example, continued Resulting binary-relationship version

Q: Conversion to relations?

Rental

Customer

Store

Movie

StoreOf

MovieOf

BuyerOf

dateyearMName

address

Cname

MID

Page 7: C20.0046: Database Management Systems Lecture #5

M.P. Johnson, DBMS, Stern/NYU, Spring 2005

7

Converting inheritance hierarchies No best way Several non-ideal methods:

E/R-style: each ES relation OO-style: each possible “object” relation nulls-style: each rooted hierarchy relation

non-applicable fields filled in with nulls Pros & cons

for each method, exist situations favoring it

Page 8: C20.0046: Database Management Systems Lecture #5

M.P. Johnson, DBMS, Stern/NYU, Spring 2005

8

Converting inheritance hierarchies

Movies

Cartoons Murder-Mysteries

isa isaVoices

Weapon

stars

length title year

Lion King

Component

Page 9: C20.0046: Database Management Systems Lecture #5

M.P. Johnson, DBMS, Stern/NYU, Spring 2005

9

Inheritance: E/R-style conversion Each ES relation

Root entity set: Movies(title, year, length)

1301993Lion King198819901980

Year

110115120

length

Roger RabbitScream

Star Wars

Title

Knife1990R. Rabbit1988

YearKnife

murderWeaponScream

TitleSubclass: MurderMysteries(title, year, murderWeapon)

Subclass: Cartoons(title, year)

1993Lion King1990

YearRoger Rabbit

Title

Page 10: C20.0046: Database Management Systems Lecture #5

M.P. Johnson, DBMS, Stern/NYU, Spring 2005

10

Subclasses: object-oriented approach Every possible “subtree” (what’s this?):

1. Movies2. Movies + Cartoons3. Movies + Murder-Mysteries4. Movies + Cartoons + Murder-Mysteries

Title Year length

Star Wars 1980 120

Title Year length Murder-Weapon

Scream 1988 110 Knife

Title Year length

Lion King 1990 115

Title Year length Murder-Weapon

Roger Rabbit 1988 110 Knife

1. 3.

2. 4.

Page 11: C20.0046: Database Management Systems Lecture #5

M.P. Johnson, DBMS, Stern/NYU, Spring 2005

11

Subclasses: nulls approach One relation for entire hierarchy Any non-applicable fields are NULL

Q: How do we know if a movie is a MM? Q: How do we know if a movie is a cartoon?

Title Year length Murder-Weapon

Star Wars 1980 120 NULLLion King 1993 130 NULLScream 1988 110 Knife

Roger Rabbit 1990 115 Knife

Page 12: C20.0046: Database Management Systems Lecture #5

M.P. Johnson, DBMS, Stern/NYU, Spring 2005

12

Subclasses methods: considerations1. Query time ~ number of tables accessed in query

nulls: best, since each entity single row multi-node questions: “Find 1999 films with length > 150 mins”

E/R: just Movies, so fast OO: Movies AND cartoons, so slow

single-node questions: “Find weapons in >150-min. cartoons” E/R: Movies, Cartoons AND MMs, so slow OO: just MoviesCMM, so fast

2. Number of relations per entity set nulls: just one, so very few E/R: one per ES, so more OO: exponential in #ESs, so very many (how many?)

3. Number/size of rows per entity nulls: one “long” row per entity

But all non non-applicables become null OO: one (all-relevant) row per entity, so smallest E/R: several (tho all relevant) rows per entity

but keys are duplicated

Page 13: C20.0046: Database Management Systems Lecture #5

M.P. Johnson, DBMS, Stern/NYU, Spring 2005

13

E/R-style & quasi-redundancy Name and year of Roger Rabbit were listed in three different

rows (in different tables) Suppose title changes (“Roger” “Roget”)

must change all three places Q: Is this redundancy? A: No!

name and year are independent multiple movies may have same name

Real redundancy reqs. dependency

two rows agree on SSN must agree on rest conflicting hair colors in these rows is an error

two rows agree on movie title may still disagree conflicting years may be correct – or may not be

can forbid first case but not second Better: introduce “movie-id” key att

Page 14: C20.0046: Database Management Systems Lecture #5

M.P. Johnson, DBMS, Stern/NYU, Spring 2005

14

Combined isa/weak example Exercise 3.3.1

Convert from E/R to R, by E/R, OO and nulls

courses

Lab-courses

Depts

#machines

room

cnumber

givenBy

dname chair

isaplatform

cname

Page 15: C20.0046: Database Management Systems Lecture #5

M.P. Johnson, DBMS, Stern/NYU, Spring 2005

15

Agenda Last time: relational model This time:1. Functional dependencies

Keys and superkeys in terms of FDs Finding keys for relations

2. Rules for combining FDs Next time: anomalies & normalization

Page 16: C20.0046: Database Management Systems Lecture #5

M.P. Johnson, DBMS, Stern/NYU, Spring 2005

16

Next topic: Functional dependencies FDs are constraints

part of the schema can’t tell from particular relation instances FD may hold for some instances “accidentally”

Finding all FDs is part of DB design Used in normalization

Page 17: C20.0046: Database Management Systems Lecture #5

M.P. Johnson, DBMS, Stern/NYU, Spring 2005

17

Functional dependencies Definition:

Notation: Read: Ai functionally determines Bj

If two tuples agree on the attributes

A1, A2, …, An

then they must also agree on the attributes

B1, B2, …, Bm

A1, A2, …, An B1, B2, …, Bm

Page 18: C20.0046: Database Management Systems Lecture #5

M.P. Johnson, DBMS, Stern/NYU, Spring 2005

18

Typical Examples of FDs Product

name price, manufacturer

Person ssn name, age father’s/husband’s-name last-name zipcode state phone state (notwithstanding inter-state area codes)

Company name stockprice, president symbol name name symbol

Page 19: C20.0046: Database Management Systems Lecture #5

M.P. Johnson, DBMS, Stern/NYU, Spring 2005

19

To check A B, erase all other columns; for each rows t1, t2

i.e., check if remaining relation is many-one no “divergences” i.e., if AB is a well-defined function thus, functional dependency

Functional dependenciesBm...B1Am...A1

t1

t2

if t 1, t 2 agree here then t 1, t 2 agree here

Page 20: C20.0046: Database Management Systems Lecture #5

M.P. Johnson, DBMS, Stern/NYU, Spring 2005

20

FDs ExampleProduct(name, category, color, department, price)

name colorcategory departmentcolor, category price

Consider these FDs:

What do they say ?

Page 21: C20.0046: Database Management Systems Lecture #5

M.P. Johnson, DBMS, Stern/NYU, Spring 2005

21

FDs ExampleFDs are constraints:• On some instances they hold• On others they don’t

name category color department price

Gizmo Gadget Green Toys 49

Tweaker Gadget Green Toys 99

Does this instance satisfy all the FDs ?

name colorcategory departmentcolor, category price

Page 22: C20.0046: Database Management Systems Lecture #5

M.P. Johnson, DBMS, Stern/NYU, Spring 2005

22

FDs Example

name category color department price

Gizmo Gadget Green Toys 49

Tweaker Gadget Black Toys 99

Gizmo Stationary Green Office-supp. 59

What about this one ?

name colorcategory departmentcolor, category price

Page 23: C20.0046: Database Management Systems Lecture #5

M.P. Johnson, DBMS, Stern/NYU, Spring 2005

23

Q: Is PositionPhone an FD here?

A: It is for this instance, but no, presumably not in general

Others FDs? EmpID Name, Phone, Position but Phone Position

Recognizing FDs

EmpID Name Phone PositionE0045 Smith 1234 ClerkE1847 John 9876 SalesrepE1111 Smith 9876 SalesrepE9999 Mary 1234 Lawyer

Page 24: C20.0046: Database Management Systems Lecture #5

M.P. Johnson, DBMS, Stern/NYU, Spring 2005

24

Keys of relations {A1A2A3…An} is a key for relation R if

1. A1A2A3…An functionally determine all other attributes

Usual notation: A1A2A3…An B1B2…Bk

rels = sets distinct rows can’t agree on all Ai

2. A1A2A3…An is minimal No proper subset of A1A2A3…An functionally determines

all other attributes of R Primary key: chosen if there are several

possible keys

Page 25: C20.0046: Database Management Systems Lecture #5

M.P. Johnson, DBMS, Stern/NYU, Spring 2005

25

Keys example Relation: Student(Name, Address, DoB,

Email, Credits) Which (/why) of the following are keys?

SSN Name, Address (on reasonable assumptions) Name, SSN Email, SSN Email

NB: minimal != smallest

Page 26: C20.0046: Database Management Systems Lecture #5

M.P. Johnson, DBMS, Stern/NYU, Spring 2005

26

Superkeys A set of attributes that contains a key Satisfies first condition:

functionally determines every other attribute in the relation

Might not satisfy the second condition: minimality may be possible to peel away some attributes

from the superkey keys are superkeys

key are special case of superkey superkey set is superset of key set

name;ssn is a superkey but not a key

Page 27: C20.0046: Database Management Systems Lecture #5

M.P. Johnson, DBMS, Stern/NYU, Spring 2005

27

Discovering keys for relations Relation entity set

Key of relation = (minimized) key of entity set

Relation binary relationship Many-many: union of keys of both entity sets Many(M)-one(O): only key of M (Why?) One-one: key of either entity set (but not both!)

Page 28: C20.0046: Database Management Systems Lecture #5

M.P. Johnson, DBMS, Stern/NYU, Spring 2005

28

Example – entity sets Key of entity set = (minimized) key of relation

Student(Name, Address, DoB, SSN, Email, Credits)

Student

Name

Address

DoB

SSN

Email

Credits

Page 29: C20.0046: Database Management Systems Lecture #5

M.P. Johnson, DBMS, Stern/NYU, Spring 2005

29

Example – many-many Many-many key: union of both ES keys

Student Enrolls Course

SSN Credits CourseID Name

Enrolls(SSN,CourseID)

Page 30: C20.0046: Database Management Systems Lecture #5

M.P. Johnson, DBMS, Stern/NYU, Spring 2005

30

Example – many-one Key of the many ES but not of the one ES

keys from both would be non-minimal

Course MeetsIn Room

CourseID Name RoomNo Capacity

MeetsIn(CourseID,RoomNo)

Page 31: C20.0046: Database Management Systems Lecture #5

M.P. Johnson, DBMS, Stern/NYU, Spring 2005

31

Example – one-one Keys of both ESs included in relation Key is key of either ES (but not both!)

Husbands Married Wives

SSN Name SSN Name

Married(HSSN, WSSN) or

Married(HSSN, WSSN)

Page 32: C20.0046: Database Management Systems Lecture #5

M.P. Johnson, DBMS, Stern/NYU, Spring 2005

32

Discovering keys: multiway Multiway relationships:

Multiple ways – may not be obvious R:F,G,HE is many-one E’s key is included

but not part of key Recall that relship atts are implicitly many-one

Course Enrolls Student

CourseID Name SSN NameSection

RoomNo CapacityEnrolls(CourseID,SSN,RoomNo)

Page 33: C20.0046: Database Management Systems Lecture #5

M.P. Johnson, DBMS, Stern/NYU, Spring 2005

33

Example Exercise 3.4.2 Relation relating particles in a box to

locations and velocitiesInPosition(id,x,y,z,vx,vy,vz)

Q: What FDs hold? Q: What are the keys?

Page 34: C20.0046: Database Management Systems Lecture #5

M.P. Johnson, DBMS, Stern/NYU, Spring 2005

34

Next time Do reading on web

Finish project part 1

Office hours now