17
Chapter 16: Relational Database Design and Further Dependencies Ref: Elmasri, Navathe, Fundamentals of Database Systems, 6th, Addison Wesley, ISBN-10: 0-13-608620-9. TA: Zhe Jiang [email protected]

Chapter 16: Relational Database Design and Further Dependencies Ref: Elmasri, Navathe, Fundamentals of Database Systems, 6th, Addison Wesley, ISBN-10:

Embed Size (px)

Citation preview

Page 1: Chapter 16: Relational Database Design and Further Dependencies Ref: Elmasri, Navathe, Fundamentals of Database Systems, 6th, Addison Wesley, ISBN-10:

Chapter 16: Relational Database Design and Further

Dependencies

Ref: Elmasri, Navathe, Fundamentals of Database Systems, 6th, Addison Wesley, ISBN-10: 0-13-608620-9.

TA: Zhe Jiang

[email protected]

Page 2: Chapter 16: Relational Database Design and Further Dependencies Ref: Elmasri, Navathe, Fundamentals of Database Systems, 6th, Addison Wesley, ISBN-10:

Outline

• Big picture & motivation

• Simple case algorithm (part of 16.3.3)

• Formal algorithm –Basic concepts (16.1):

–General case algorithm (16.3.3)

Page 3: Chapter 16: Relational Database Design and Further Dependencies Ref: Elmasri, Navathe, Fundamentals of Database Systems, 6th, Addison Wesley, ISBN-10:

Big Picture:Database Design Phases

ER-Diagram

Relational TablesWhich choice is good?How to guarantee it?

Formal Norm Theory

Page 4: Chapter 16: Relational Database Design and Further Dependencies Ref: Elmasri, Navathe, Fundamentals of Database Systems, 6th, Addison Wesley, ISBN-10:

Motivation

We have:

•Universal relational schema U(A1,A2, … An).

•A set of functional dependencies (FDs) from domain knowledge.

Question:

How do we decompose U into sub-relations, so as to satisfy 3NF?

Page 5: Chapter 16: Relational Database Design and Further Dependencies Ref: Elmasri, Navathe, Fundamentals of Database Systems, 6th, Addison Wesley, ISBN-10:

Simple Case Decomposition Algorithm• Motivation:– Decompose universal relational schema into sub

relations which satisfy 3NF

• Properties:– Preserve dependencies (nonlossy design)– Non-additive join property (no spurious tuples)– Resulting relational schemas are in 3NF

• Problem Definition:– Input: Universal Relation R and a set of functional

dependencies F on the attributes of R– Output: Sub-relations, FDs.– Constraint: the three properties above

Page 6: Chapter 16: Relational Database Design and Further Dependencies Ref: Elmasri, Navathe, Fundamentals of Database Systems, 6th, Addison Wesley, ISBN-10:

Simple Case Decomposition Algorithm• Suppose the FD set given is already “good”

minimal cover (defined later)

• Approach:1. For each LHS X in F, create a relation schema

in D {X U {A1} U {A2} … U {Ak} }.where XAi only dependency with X as LHS.

2. If none of the relation schemas in D contains a key of R, create one relation with key. (How? Introduce later)

3. Eliminate redundant relations.

Page 7: Chapter 16: Relational Database Design and Further Dependencies Ref: Elmasri, Navathe, Fundamentals of Database Systems, 6th, Addison Wesley, ISBN-10:

Simple Case Decomposition Algorithm• Exercise:– Universal relation

– FD: {PLC, LCAP, AC}

– Q: Does it satisfy 1NF, 2NF, 3NF?

– Q: How to decompose the relation to satisfy 3NF?

• Solution:1.R1(P,L,C); R2(L,C,A,P); R3(A,C)

2.Already contains key.

3.Remove redundant relations R1 and R3, final answer is R2(L,C,A,P).

Page 8: Chapter 16: Relational Database Design and Further Dependencies Ref: Elmasri, Navathe, Fundamentals of Database Systems, 6th, Addison Wesley, ISBN-10:

General Case Decomposition Algorithm

• New info: Transform the given FD set into minimal cover

• New info: If no key exists, find key of U, then create a relation contain key

• We will introduce some basic concepts, then formal algorithm

Page 9: Chapter 16: Relational Database Design and Further Dependencies Ref: Elmasri, Navathe, Fundamentals of Database Systems, 6th, Addison Wesley, ISBN-10:

Basic Concept

• Inference rules: One FD could infer another – trivial: IR1: IR1 (reflexive rule) • If X Y, then X Y.

– non-trivial: IR2-IR4• {XY} |= XZYZ

• {XY, YZ} |= XZ

• {XYZ} |=XY

• Closure of set of dependencies– Closure of F: F+, set of all FDs could be inferred.

– Use IR1 to IR3;

Page 10: Chapter 16: Relational Database Design and Further Dependencies Ref: Elmasri, Navathe, Fundamentals of Database Systems, 6th, Addison Wesley, ISBN-10:

Basic Concepts• Closure of left-hand-side under dependency

setAlgorithm 16.1

1. Start: X+={X}

2. Grow X+ with new attributes determined by elements in X+

3. Repeat 2 until can’t grow any more.Exercise:Given: F={XYZ, XW, WU, YV}, U(X,Y,Z,W,U,V) Find: X+ ?

Page 11: Chapter 16: Relational Database Design and Further Dependencies Ref: Elmasri, Navathe, Fundamentals of Database Systems, 6th, Addison Wesley, ISBN-10:

Basic Concepts

• Equivalence of functional dependencies sets– Definition• Cover: F covers E if F+ contains E.

• Equivalent FD sets:

– Algorithm• Check if all left-hand-sides’ closures are same

• Minimal Cover of dependency set F– definition: Can’t find subset that is equivalent to F

Page 12: Chapter 16: Relational Database Design and Further Dependencies Ref: Elmasri, Navathe, Fundamentals of Database Systems, 6th, Addison Wesley, ISBN-10:

Basic Concept• Minimal Cover of dependency set F

1. break down right-hand-side, X{A1,A2,…An} to XA1, XA2, …XAn

2. Try reduce size of LHS X in F, e.g. changing X into {X-B} still equivalent to F?

3. Try reduce unnecessary FD in F, e.g. remove XA in F, if result still equivalent to F.

• Example: – F={PLCA, LCAP, AC}

– What is “minimal cover” of F?

Page 13: Chapter 16: Relational Database Design and Further Dependencies Ref: Elmasri, Navathe, Fundamentals of Database Systems, 6th, Addison Wesley, ISBN-10:

Basic Concepts

• Algorithm to find key of R&F– Start with K=R.– Find A in R such that (K-A)+ contain all attributes.– Repeat until size of K is as small as possible

• Example: – U(Emp_ssn, Pno, Esal, Ephone, Dno, Pname,

Plocation)– F={Emp_ssnEsal, Ephone, Dno; PnoPname,

plocation};– What is the key?

Page 14: Chapter 16: Relational Database Design and Further Dependencies Ref: Elmasri, Navathe, Fundamentals of Database Systems, 6th, Addison Wesley, ISBN-10:

Decomposition Algorithm: Exercise

FD3

FD1: Property_id Lot#, County, AreaFD2: Lot#, County Area, Property_idFD3: AreaCounty

Simpler Version:F={PLCA, LCAP, AC}

1. What is the minimal cover G?

2. Decompose G

Page 15: Chapter 16: Relational Database Design and Further Dependencies Ref: Elmasri, Navathe, Fundamentals of Database Systems, 6th, Addison Wesley, ISBN-10:

Decomposition Algorithm ExampleSimpler Version:F={PLCA, LCAP, AC}

First Case: Minimal cover GX:1.F: {PL, PC, PA, LCA, LCP,AC}2.Minimal cover GX: {PLC, LCAP, AC}

Design X: 3. R1(P,L,C), R2(L,C,A,P), and R3(A,C)4. R2(L,C,A,P)

Page 16: Chapter 16: Relational Database Design and Further Dependencies Ref: Elmasri, Navathe, Fundamentals of Database Systems, 6th, Addison Wesley, ISBN-10:

Decomposition Algorithm ExampleSimpler Version:F={PLCA, LCAP, AC}

Second Case: Minimal cover GX:1.F: {PL, PC, PA, LCA, LCP,AC}2.Minimal cover GX: {PLA, LCP, AC}

Design Y: 3. S1(P,A,L), S2(L,C,P), and S3(A,C)4. No redundant relations.

Page 17: Chapter 16: Relational Database Design and Further Dependencies Ref: Elmasri, Navathe, Fundamentals of Database Systems, 6th, Addison Wesley, ISBN-10:

Exercise

• Given:– Universal relation U(A,B,C,D,E,F,G,H,I,J)

– Functional dependencies F={ {A,B}{C}, {B,D}{E,F}, {A,D}{G,H}, {A}{I}, {H}{J} }.

– Decompose it into 3NF?