l112_ppt_ivsem

19
Database Management System Lecture 12 Lecture 12 Normalization Normalization

Upload: rohit-tiwari

Post on 12-Jul-2016

212 views

Category:

Documents


0 download

DESCRIPTION

kl

TRANSCRIPT

Page 1: L112_PPT_IVSem

Database Management System

Lecture 12Lecture 12NormalizationNormalization

Page 2: L112_PPT_IVSem

NORMAL FORMS

• The normal forms based on FDs are rst normal form (1NF), second normal form (2NF), third normal form (3NF), and Boyce-Codd normal form (BCNF). Th f h i i l t i ti i t • These forms have increasingly restrictive requirements: Every relation in BCNF is also in 3NF,

• every relation in 3NF is also in 2NF, and every relation in 2NF is in 1NF 2NF is in 1NF.

• A relation• is in first normal form if every field contains only atomic

values that is not lists or sets values, that is, not lists or sets. • This requirement is implicit in our defition of the relational

model.• Although some of the ne er database s stems are • Although some of the newer database systems are

relaxing this requirement• 2NF is mainly of historical interest.

3NF d BCNF i t t f d t b d i

Slide No:L3-1

• 3NF and BCNF are important from a database design standpoint.

Page 3: L112_PPT_IVSem

Normal FormsNormal Forms

• Returning to the issue of schema refinement, the first g ,question to ask is whether any refinement is needed!

• If a relation is in a certain normal form (BCNF, 3NF etc.), it is known that certain kinds of problems are avoided/minimized known that certain kinds of problems are avoided/minimized. This can be used to help us decide whether decomposing the relation will help.R l f FD i d t ti d d• Role of FDs in detecting redundancy:– Consider a relation R with 3 attributes, ABC.

• No FDs hold: There is no redundancy here.• Given A B: Several tuples could have the same A value,

and if so, they’ll all have the same B value!→

Slide No. L3-2

Page 4: L112_PPT_IVSem

First Normal FormFirst Normal Form

1NF (First Normal Form) a relation R is in 1NF if and only if it has only single• a relation R is in 1NF if and only if it has only single-valued attributes (atomic values)

• EMP_PROJ (SSN, PNO, HOURS, ENAME, PNAME, PLOCATION)PLOCATION)PLOCATION is not in 1NF (multi-valued attrib.)

• solution: decompose the relationpEMP_PROJ2 (SSN, PNO, HOURS, ENAME, PNAME)LOC (PNO, PLOCATION)

Slide No. L3-3

Page 5: L112_PPT_IVSem

Second Normal FormSecond Normal FormSecond Normal FormSecond Normal Form

2NF (Second Normal Form) a relation R in 2NF if and only if it is in 1NF and • a relation R in 2NF if and only if it is in 1NF and every nonkey column depends on a key not a subset of a key

• all nonprime attributes of R must be fully functionally dependent on a whole key(s) of the relation, not a part of the key

• no violation: single-attribute key or no nonprime attribute

Slide No. L3-4

Page 6: L112_PPT_IVSem

Second Normal Form ( Contd)Second Normal Form ( Contd)Second Normal Form ( Contd)Second Normal Form ( Contd)2NF (Second Normal Form) • violation: part of a key → nonkey

EMP_PROJ2 (SSN, PNO, HOURS, ENAME, PNAME)SSN ENAMESSN → ENAMEPNO → PNAME

• solution: decompose the relationsolution: decompose the relationEMP_PROJ3 (SSN, PNO, HOURS)

EMP (SSN, ENAME)EMP (SSN, ENAME)PROJ (PNO, PNAME)

Slide No. L3-5

Page 7: L112_PPT_IVSem

Third Normal FormThird Normal FormThird Normal FormThird Normal Form

3NF (Third Normal Form) • a relation R in 3NF if and only if it is in 2NF and

every nonkey column does not depend on another every nonkey column does not depend on another nonkey column

• all nonprime attributes of R must be non-transitively functionally dependent on a key of the relation

• violation: nonkey → nonkeyy y• fig14.10: 2NF & 3NF normalization

Slide No. L3-6

Page 8: L112_PPT_IVSem

Third Normal Form (Contd) Third Normal Form (Contd) Third Normal Form (Contd) Third Normal Form (Contd)

3NF (Third Normal Form) • SUPPLIER (SNAME, STREET, CITY, STATE, TAX)

SNAME → STREET, CITY, STATESTATE → TAX (nonkey → nonkey)SNAME → STATE → TAX (transitive FD)SNAME → STATE → TAX (transitive FD)

• solution: decompose the relationSUPPLIER2 (SNAME, STREET, CITY, STATE)TAXINFO (STATE, TAX)

Slide No. L3-7

Page 9: L112_PPT_IVSem

BoyceBoyce--Codd Normal Form (BCNF)Codd Normal Form (BCNF)

• Reln R with FDs F is in BCNF if, for all X A in F+→,– A X (called a trivial FD), or– X contains a key for R.

I h d R i i BCNF if h l i i l FD h

• In other words, R is in BCNF if the only non-trivial FDs that hold over R are key constraints.– No dependency in R that can be predicted using FDs alone.y g– If we are shown two tuples that agree upon

the X value, we cannot infer the A value in one tuple from the A value in the other X Y Aone tuple from the A value in the other.

– If example relation is in BCNF, the 2 tuples must be identical (since X is a key).

x y1 ax y2 ?

Slide No. L3-8

x y2 ?

Page 10: L112_PPT_IVSem

Third Normal Form (3NF)Third Normal Form (3NF)

• Reln R with FDs F is in 3NF if, for all X A in F+→,– A X (called a trivial FD), or– X contains a key for R, or

A i f k f R

– A is part of some key for R. • Minimality of a key is crucial in third condition above! • If R is in BCNF, obviously in 3NF.If R is in BCNF, obviously in 3NF.• If R is in 3NF, some redundancy is possible. It is a

compromise, used when BCNF not achievable (e.g., no ``good’’ decomp or performance considerations)decomp, or performance considerations).– Lossless-join, dependency-preserving decomposition of R

into a collection of 3NF relations always possible.

Slide No. L3-9

Page 11: L112_PPT_IVSem

Decomposition of a Relation SchemeDecomposition of a Relation Scheme

• Suppose that relation R contains attributes A1 ... An. A ppdecomposition of R consists of replacing R by two or more relations such that:– Each new relation scheme contains a subset of the Each new relation scheme contains a subset of the

attributes of R (and no attributes that do not appear in R), andE tt ib t f R tt ib t f f th – Every attribute of R appears as an attribute of one of the new relations.

• Intuitively, decomposing R means we will store instances of the relation schemes produced by the decomposition, instead of instances of R.

• E.g., Can decompose SNLRWH into SNLRH and RW.

Slide No. L4-1

g , p

Page 12: L112_PPT_IVSem

Example DecompositionExample Decomposition

• Decompositions should be used only when needed.Decompositions should be used only when needed.– SNLRWH has FDs S SNLRWH and R W– Second FD causes violation of 3NF; W values repeatedly

i d i h R l E i fi hi i

→ →

associated with R values. Easiest way to fix this is to create a relation RW to store these associations, and to remove W from the main schema:

• i.e., we decompose SNLRWH into SNLRH and RW • The information to be stored consists of SNLRWH tuples. If

we just store the projections of these tuples onto SNLRH and we just store the projections of these tuples onto SNLRH and RW, are there any potential problems that we should be aware of?

Slide No. L4-2

Page 13: L112_PPT_IVSem

Problems with DecompositionsProblems with Decompositions

• There are three potential problems to consider:There are three potential problems to consider:– Some queries become more expensive.

• e.g., How much did sailor Joe earn? (salary = W*H)– Given instances of the decomposed relations, we may not

be able to reconstruct the corresponding instance of the original relation! • Fortunately, not in the SNLRWH example.

– Checking some dependencies may require joining the instances of the decomposed relationsinstances of the decomposed relations.• Fortunately, not in the SNLRWH example.

• Tradeoff: Must consider these issues vs. redundancy.

Slide No. L4-3

Page 14: L112_PPT_IVSem

Lossless Join DecompositionsLossless Join Decompositions

• Decomposition of R into X and Y is lossless-join w.r.t. a set of Decomposition of R into X and Y is lossless join w.r.t. a set of FDs F if, for every instance r that satisfies F:– (r) (r) = r

I i l h ( ) ( )π X π Y><

• It is always true that r (r) (r)– In general, the other direction does not hold! If it does, the

decomposition is lossless-join.

X Y

⊆ π X >< π Y

p j• Definition extended to decomposition into 3 or more relations

in a straightforward way.• It is essential that all decompositions used to deal with • It is essential that all decompositions used to deal with

redundancy be lossless! (Avoids Problem (2).)

Slide No. L4-4

Page 15: L112_PPT_IVSem

More on Lossless JoinMore on Lossless Join A B

• The decomposition of R into X A B C1 24 57 2The decomposition of R into X

and Y is lossless-join wrt F if andonly if the closure of F contains:

X Y X or

1 2 34 5 67 2 8

7 2

B C– X Y X, or– X Y Y

• In particular, the decomposition of →∩

7 2 82 35 62 8

p pR into UV and R - V is lossless-join if U V holds over R.

→∩ A B C1 2 3

2 8

4 5 67 2 81 2 8

Slide No. L4-5

→ 1 2 87 2 3

Page 16: L112_PPT_IVSem

Dependency Preserving DecompositionDependency Preserving Decomposition

• Consider CSJDPQV, C is key, JP C and SD P.→ →Consider CSJDPQV, C is key, JP C and SD P.– BCNF decomposition: CSJDQV and SDP– Problem: Checking JP C requires a join!

→ →

→• Dependency preserving decomposition (Intuitive):

– If R is decomposed into X, Y and Z, and we enforce the FDs that hold on X, on Y and on Z, then all FDs that were FDs that hold on X, on Y and on Z, then all FDs that were given to hold on R must also hold. (Avoids Problem (3).)

• Projection of set of FDs F: If R is decomposed into X, ... projection of F onto X (denoted F ) is the set of FDs U V projection of F onto X (denoted FX ) is the set of FDs U V in F+ (closure of F ) such that U, V are in X.

Slide No. L4-6

Page 17: L112_PPT_IVSem

Dependency Preserving Decompositions Dependency Preserving Decompositions (Contd.)(Contd.)(Contd.)(Contd.)

• Decomposition of R into X and Y is dependency preserving if (FX union FY ) + = F +

– i.e., if we consider only dependencies in the closure F + that can be checked in X without considering Y, and in Y g ,without considering X, these imply all dependencies in F +.

• Important to consider F +, not F, in this definition:ABC A B B C C A decomposed into AB and – ABC, A B, B C, C A, decomposed into AB and BC.

– Is this dependency preserving? Is C A preserved?????→ → →

→• Dependency preserving does not imply lossless join:

– ABC, A B, decomposed into AB and BC.• And vice versa! (Example?)

Slide No. L4-7

• And vice-versa! (Example?)

Page 18: L112_PPT_IVSem

Decomposition into BCNFDecomposition into BCNF

• Consider relation R with FDs F. If X Y violates BCNF, → ,decompose R into R - Y and XY.– Repeated application of this idea will give us a collection of

relations that are in BCNF; lossless join decomposition relations that are in BCNF; lossless join decomposition, and guaranteed to terminate.

– e.g., CSJDPQV, key C, JP C, SD P, J S→ → →– To deal with SD P, decompose into SDP, CSJDQV.

– To deal with J S, decompose CSJDQV into JS and CJDQV

→ → →→

→Q• In general, several dependencies may cause violation of BCNF.

The order in which we ``deal with’’ them could lead to very different sets of relations!

Slide No. L4-8

different sets of relations!

Page 19: L112_PPT_IVSem

BCNF and Dependency PreservationBCNF and Dependency Preservation

• In general, there may not be a dependency preserving In general, there may not be a dependency preserving decomposition into BCNF.– e.g., CSZ, CS Z, Z C

C ’ d hil i 1 FD i BCNF→ →

– Can’t decompose while preserving 1st FD; not in BCNF.• Similarly, decomposition of CSJDQV into SDP, JS and

CJDQV is not dependency preserving (w.r.t. the FDs JP p y p g (C, SD P and J S).– However, it is a lossless join decomposition.

In this case adding JPC to the collection of relations → → →

– In this case, adding JPC to the collection of relations gives us a dependency preserving decomposition.

• JPC tuples stored only for checking FD! (Redundancy!)

Slide No. L4-9