Functional Dependencies, Normalization Rose-Hulman Institute of - - PowerPoint PPT Presentation

functional dependencies normalization
SMART_READER_LITE
LIVE PREVIEW

Functional Dependencies, Normalization Rose-Hulman Institute of - - PowerPoint PPT Presentation

Functional Dependencies, Normalization Rose-Hulman Institute of Technology Curt Clifton Or Fixing Broken Database Designs This material will almost certainly appear on Exam II next week. Outline Functional Dependencies Keys


slide-1
SLIDE 1

Functional Dependencies, Normalization

Rose-Hulman Institute of Technology Curt Clifton

slide-2
SLIDE 2

Or…

slide-3
SLIDE 3

Fixing Broken Database Designs

This material will almost certainly appear on Exam II next week.

slide-4
SLIDE 4

Outline

 Functional Dependencies  Keys Revisited  Redundancy and Anomalies  Normalization

slide-5
SLIDE 5

Functional Dependencies (FD)

 Let X be a set of attributes of a relation R  Let A be a single attribute of R  X → A holds for R if:

 whenever two tuples of R agree on all the

attributes of X,

 then they must also agree on the attribute A.

 We say X “uniquely determines” A in R

slide-6
SLIDE 6

Example

 Customer(Name, Addr, SodaLiked, Manf,

FavSoda), with name identifying a unique person

 Lots of redundancy here…

Coke PepsiCo Pepsi Enterprise Spock Coke CocaCola Sprite Voyager Janeway Coke PepsiCo Pepsi Voyager Janeway FavSoda Manf SodaLiked Addr Name

slide-7
SLIDE 7

FDs from Data

 Does Name → Addr?

Coke PepsiCo Pepsi Enterprise Spock Coke CocaCola Sprite Voyager Janeway Coke PepsiCo Pepsi Voyager Janeway FavSoda Manf SodaLiked Addr Name

slide-8
SLIDE 8

FDs from Data

 Does Name → Addr?  Yes, since we assumed unique names

Coke PepsiCo Pepsi Enterprise Spock Coke CocaCola Sprite Voyager Janeway Coke PepsiCo Pepsi Voyager Janeway FavSoda Manf SodaLiked Addr Name

slide-9
SLIDE 9

FDs from Data

 Does Name → FavSoda?

Coke PepsiCo Pepsi Enterprise Spock Coke CocaCola Sprite Voyager Janeway Coke PepsiCo Pepsi Voyager Janeway FavSoda Manf SodaLiked Addr Name

slide-10
SLIDE 10

FDs from Data

 Does Name → FavSoda?  Yes, we want just one favorite per person

Coke PepsiCo Pepsi Enterprise Spock Coke CocaCola Sprite Voyager Janeway Coke PepsiCo Pepsi Voyager Janeway FavSoda Manf SodaLiked Addr Name

slide-11
SLIDE 11

FDs from Data

 Does SodaLiked → Manf?

Coke PepsiCo Pepsi Enterprise Spock Coke CocaCola Sprite Voyager Janeway Coke PepsiCo Pepsi Voyager Janeway FavSoda Manf SodaLiked Addr Name

slide-12
SLIDE 12

FDs from Data

 Does SodaLiked → Manf?  Yes, since each soda has just one manf.

Coke PepsiCo Pepsi Enterprise Spock Coke CocaCola Sprite Voyager Janeway Coke PepsiCo Pepsi Voyager Janeway FavSoda Manf SodaLiked Addr Name

slide-13
SLIDE 13

FDs from Data

 Does FavSoda → Name?

Coke PepsiCo Pepsi Enterprise Spock Coke CocaCola Sprite Voyager Janeway Coke PepsiCo Pepsi Voyager Janeway FavSoda Manf SodaLiked Addr Name

slide-14
SLIDE 14

FDs from Data

 Does FavSoda → Name?  No, two people might have the same favorite

Coke PepsiCo Pepsi Enterprise Spock Coke CocaCola Sprite Voyager Janeway Coke PepsiCo Pepsi Voyager Janeway FavSoda Manf SodaLiked Addr Name

slide-15
SLIDE 15

FDs from ER Diagrams

 From entity sets

 (Key of entity set) → other attributes of entity set

 From many-one relationship

 (Key of “many” set) → attributes of “one” set

slide-16
SLIDE 16

Drawing FDs

 Use arrows to indicate FDs on schemas:

Customer(Name, Addr, SodaLiked, Manf, FavSoda)

slide-17
SLIDE 17

Notation Shorthand

 Technically FDs go from sets to single

attributes

 { Name } → Addr  { Name } → FavSoda

 Often just combine to write:

 Name → Addr, FavSoda

 Usually omit set braces on left side also:

 Restaurant, Soda → Price

slide-18
SLIDE 18

Keys Revisited

 Let K be a set of attributes of a relation R  K is a super key for R if:

 For all attributes A in R, K → A

 K is a key for R if:

 No proper subset of K is a super key for R

 An attribute B is a prime attribute of R if:

 B is an element of some key of R

slide-19
SLIDE 19

Example

 What is the key here?  What are the prime attributes?

Customer(Name, Addr, SodaLiked, Manf, FavSoda)

slide-20
SLIDE 20

Two Ways to Find Keys

 Guess a superkey K:

 Show that K → A for all attributes A  Show that no subset of K is a superkey

 Find all functional dependencies

 Check all possible keys

slide-21
SLIDE 21

Why Talk About FDs?

 Let us formally identify redundancy  Tell us how to fix it!

slide-22
SLIDE 22

Redundancy Leads to Anomalies

 Update anomaly: one occurrence of a fact is

changed, but not all occurrences

 Deletion anomaly: valid fact is lost when a

tuple is deleted

slide-23
SLIDE 23

Example

Coke PepsiCo Pepsi Enterprise Spock Coke CocaCola Sprite Voyager Janeway Coke PepsiCo Pepsi Voyager Janeway FavSoda Manf SodaLiked Addr Name Redundant with first row since Name → Addr, FavSoda Redundant with first row since SodaLiked → Manf

slide-24
SLIDE 24

Normalization

 Using functional dependencies to eliminate

redundancy

 An extremely powerful technique

slide-25
SLIDE 25

Third Normal Form

 A relation R is in Third Normal Form (3NF) if

whenever X → A is a nontrivial functional dependency that holds in R, then either:

 X is a superkey for R, or  A is a prime attribute of R

slide-26
SLIDE 26

Normalization Algorithm

 To normalize a relation R:

 Find the functional dependencies for R  Check that whether each FD satisfies 3NF

If so, we’re done and R is normalized

 Otherwise let X → A be an FD that violates 3NF

Find the closure of X in R, denoted X+

Split R into new relations (R - X+ + X) and X+

 Repeat algorithm for each new relation

slide-27
SLIDE 27

Example: Grades Relation

 Grade(Term, Yr, C#, Sec#, IName,

SName, SAddr, S#, SSSN, Gr)

slide-28
SLIDE 28

Step 1: Find the FDs

slide-29
SLIDE 29

Step 2: Check for 3NF Violations

 A relation R is in Third Normal Form (3NF) if

whenever X → A is a nontrivial functional dependency that holds in R, then either:

 X is a superkey for R, or  A is a prime attribute of R

slide-30
SLIDE 30

Step 3: Pick a Violating FD, Find Closure

 For X → A the closure of X, denoted X+, is:

 The set of all attributes that can be reached from

any subset of X by following any FDs

 Or, just follow the arrows

slide-31
SLIDE 31

Step 4: Split R into Two Relations

R-X + X X +-X R2 R1 R

slide-32
SLIDE 32

Repeat for the New Relations

 Find FDs  Check for 3NF violations  …