Functional Dependencies

22
Functional Dependencies n: If two tuples agree on the attributes A , A , … A 1 2 n then they must also agree on the attributes B , B , … B 1 2 m Formally: A , A , … A 1 2 n B , B , … B 1 2 m ing example for the study of functional dependencie ame Social Security Number Phone Num

description

Functional Dependencies. Definition: If two tuples agree on the attributes . A , A , … A . 1. 2. n. then they must also agree on the attributes. B , B , … B . 1. 2. m. Formally:. A , A , … A . B , B , … B . 1. 2. m. 1. 2. n. - PowerPoint PPT Presentation

Transcript of Functional Dependencies

Page 1: Functional Dependencies

Functional DependenciesDefinition:

If two tuples agree on the attributes

A , A , … A 1 2 n

then they must also agree on the attributesB , B , … B 1 2 m

Formally:

A , A , … A 1 2 n

B , B , … B 1 2 m

Motivating example for the study of functional dependencies:

Name Social Security Number Phone Number

Page 2: Functional Dependencies

Examples

Product: name price, manufacturerPerson: ssn name, ageCompany: name stock price, president

Key of a relation is a set of attributes that:

- functionally determines all the attributes of the relation - none of its subsets determines all the attributes.

Superkey: a set of attributes that contains a key.

Page 3: Functional Dependencies

Finding the Attributes of a Relation

Given a relation constructed from an E/R diagram, what is its key?

Rules:

1. If the relation comes from an entity set, the key of the relation is the set of attributes which is the key of the entity set.

address name ssn

Person

Page 4: Functional Dependencies

Rules for Binary Relationships

Several cases are possible for a binary relationship E1 - E2:

1. Many-many: the key includes the key of E1 together with the key of E2.

What happens for:

2. Many-one:

3. One-one:

PersonbuysProduct

name

price name ssn

Page 5: Functional Dependencies

Rules for Multiway Relationships

None, really.

Except: if there is an arrow from the relationship to E, then we don’t need the key of E as part of the relation key.

Purchase

Product

Person

Store

Payment Method

Page 6: Functional Dependencies

Some Properties of FD’sA , A , … A 1 2 n B , B , … B 1 2 m

A , A , … A 1 2 n 1

Is equivalent to

B

A , A , … A 1 2 n 2B

A , A , … A 1 2 n mB…

A , A , … A 1 2 n iA Always holds.

Splitting rule and Combing rule

Page 7: Functional Dependencies

Comparing Functional Dependencies

Functional dependencies: a statement about the set of allowable databases.Entailment and equivalence: comparing sets of functional dependencies

Entailment: a set of functional dependencies S1 entails a set S2 if: any database that satisfies S1 much also satisfy S2.

Example: {A B, B C} entails A CEquivalence: two sets of FD’s are equivalent if each entails the other.

{A B, B C } is equivalent to {A B, A C, B C}

Page 8: Functional Dependencies

Closure of a set of Attributes

Given a set of attributes A and a set of dependencies C, we want to find all the other attributes that are functionally determined by A.

In other words, we want to find the maximal set of attributes B, such that for every B in B,

C entails A B.

Page 9: Functional Dependencies

Closure AlgorithmStart with Closure=A.

Until closure doesn’t change do:

if is in C, and

B is not in Closure

then

add B to closure.

A , A , … A 1 2 nB

A , A , … A 1 2 nare all in the closure, and

Page 10: Functional Dependencies

ExampleA B CA D E B DA F B

Closure of {A,B}:

Closure of {A, F}:

Page 11: Functional Dependencies

Problems in Designing SchemaName SSN Phone Number

Fred 123-321-99 (201) 555-1234Fred 123-321-99 (206) 572-4312Joe 909-438-44 (908) 464-0028Joe 909-438-44 (212) 555-4000

Problems:

- redundancy - update anomalies - deletion anomalies

Page 12: Functional Dependencies

Relation Decomposition

Name SSN

Fred 123-321-99Joe 909-438-44

Name Phone Number

Fred (201) 555-1234Fred (206) 572-4312Joe (908) 464-0028Joe (212) 555-4000

Break the relation into two relations:

Page 13: Functional Dependencies

Decompositions in GeneralA , A , … A 1 2 n

Let R be a relation with attributes

Create two relations R1 and R2 with attributes

B , B , … B 1 2 m C , C , … C 1 2 l

Such that:B , B , … B 1 2 m C , C , … C 1 2 l

A , A , … A 1 2 n

And -- R1 is the projection of R on

-- R2 is the projection of R on

B , B , … B 1 2 m

C , C , … C 1 2 l

Page 14: Functional Dependencies

Boyce-Codd Normal FormA simple condition for removing anomalies from relations:

A relation R is in BCNF if and only if:

Whenever there is a nontrivial dependency for R , it is the case that { } a super-key for R.

A , A , … A 1 2 n

BA , A , … A 1 2 n

In English (though a bit vague):

Whenever a set of attributes of R is determining another attribute, should determine all the attributes of R.

Page 15: Functional Dependencies

ExampleName SSN Phone Number

Fred 123-321-99 (201) 555-1234Fred 123-321-99 (206) 572-4312Joe 909-438-44 (908) 464-0028Joe 909-438-44 (212) 555-4000

What are the dependencies?

What are the keys?

Is it in BCNF?

Page 16: Functional Dependencies

And Now?SSN Name

123-321-99 Fred909-438-44 Joe

SSN Phone Number

123-321-99 (201) 555-1234123-321-99 (206) 572-4312909-438-44 (908) 464-0028909-438-44 (212) 555-4000

Page 17: Functional Dependencies

What About This?

Name Price Category

Gizmo $19.99 gadgets

Question:

Find an example of a 2-attribute relation that is not in BCNF.

Page 18: Functional Dependencies

More DecompositionsName Address Move-Date

Name Address

Name Move-Date

What’s wrong?

Page 19: Functional Dependencies

More Careful StrategyFind a dependency that violates the BCNF condition:

A , A , … A 1 2 n B , B , … B 1 2 m

A’sOthers B’s

R1 R2

Page 20: Functional Dependencies

Example Decomposition

Name Social-security-number Age Eye Color Phone Number

Functional dependencies:

Name + Social-security-number Age, Eye Color

What if we also had an attribute Draft-worthy, and the FD: Age Draft-worthy

Page 21: Functional Dependencies

Decomposition Based on BCNF is Necessarily Correct

Attributes A, B, C. FD: A C

Relations R1[A,B] R2[A,C]

Tuples in R1: (a,b)

Tuples in R2: (a,c), (a,d)

Tuples in the join of R1 and R2: (a,b,c), (a,b,d)

Can (a,b,d) be a bogus tuple?

Page 22: Functional Dependencies

Multivalued Dependencies Name SSN Phone Number Course

Fred 123-321-99 (206) 572-4312 CSE-444Fred 123-321-99 (206) 572-4312 CSE-341Fred 123-321-99 (206) 432-8954 CSE-444Fred 123-321-99 (206) 432-8954 CSE-341

The multivalued dependencies are:

Name, SSN Phone Number Name, SSN Course

4th Normal form: replace FD by MVD.