1 / 22

Normalization

Normalization. Chapter 19. What it’s all about. Given a relation, R, and a set of functional dependencies, F, on R. Assume that R is not in a desirable form for enforcing F.

avedis
Download Presentation

Normalization

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Normalization Chapter 19

  2. What it’s all about • Given a relation, R, and a set of functional dependencies, F, on R. • Assume that R is not in a desirable form for enforcing F. • Decompose relation R into relations, R1,..., Rk, with associated functional dependencies, F1,..., Fk, such that R1,..., Rk are in a more desirable form, 3NF or BCNF. • While decomposing R, make sure to preserve the dependencies, and make sure not to lose information.

  3. Decomposition of a Relation Scheme • Suppose that relation R contains attributes A1 ... An. A decompositionof R consists of replacing R by two or more relations such that: • Each new relation scheme contains a subset of the attributes of R (and no attributes that do not appear in R), and • 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.

  4. Example Decomposition • Decompositions should be used only when needed. • SNLRWH has FDs S  SNLRWH and R  W • Second FD causes violation of 3NF; W values repeatedly 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 RW, are there any potential problems that we should be aware of?

  5. Problems with Decompositions • 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 relations. • Fortunately, not in the SNLRWH example. • Tradeoff: Must consider these issues vs. redundancy.

  6. Lossless Join Decompositions • 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 • It is always true that r (r) (r) • In general, the other direction does not hold! If it does, the decomposition is lossless-join. • Definition extended to decomposition into 3 or more relations in a straightforward way. • It is essential that all decompositions used to deal with redundancy be lossless! (Avoids Problem (2).)

  7. Example of Non Lossless-Join Decomposition

  8. More on Lossless Join • The decomposition of R into X and Y is lossless-join wrt F if and only if the closure of F contains: • X  Y  X, or • X  Y  Y • In particular, the decomposition of R into UV and R - V is lossless-join if U V holds over R. (U  V = )

  9. Testing for Lossless Join Property • Input: a decomposition D={R1, R2, ..., Rm} of R and a set F of FDs. • Create an initial matrix S with one row i for each relation Ri in D and one column for each attribute Aj in R. • Set S(i,j) = bij for all matrix entries. • For each row i For each column j if relation Ri includes attribute Aj then set S(i,j) = aj; • Repeat the following loop until a complete loop execution results in no changes to S: for each FD X  Y in F for all rows in S that have the same symbols in the columns corresponding to attributes in X make the symbols in each column that correspond to an attribute in Y be the same in all these rows. • If a row is made up entirely of “a” symbols then the decomposition has the lossless join property; otherwise it does not.

  10. Example • R={ssn, ename, pnumber, pname, ploc, hrs} • D= {R1 = Emp_Loc(ename, ploc), R2 = Emp_Proj(ssn, pnumber, hrs, pname, ploc)} • F = {ssn  ename, pno  {pname, ploc}, {ssn, pno}  hrs} • Is D a lossless join decomposition?

  11. Example (cont) • D2 = {R1, R2, R3} where • R1 = {ssn, ename} • R2 = {pno, pname, ploc} • R3 = {ssn, pno, hrs} • Is D2 lossless join decomposition?

  12. Dependency Preserving Decomposition • 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 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 FX ) is the set of FDs U  V in F+(closure of F )such that U, V are in X.

  13. Dependency Preserving Decompositions (Contd.) • Decomposition of R into X and Y is dependencypreserving if (FX 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 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 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: Contracts relation)

  14. Example • R = (A, B, C)F = {A B, B C) • Can be decomposed in two different ways • R1 = (A, B), R2 = (B, C) • Lossless-join decomposition: R1  R2 = {B} and B BC • Dependency preserving • R1 = (A, B), R2 = (A, C) • Lossless-join decomposition: R1  R2 = {A} and A  AB • Not dependency preserving (cannot check B C without computing R1 R2)

  15. Decomposition into BCNF • Consider relation R with FDs F. If X  Y violates BCNF, decompose R into XY and R - Y. • Repeated application of this idea will give us a collection of 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 • 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!

  16. Example of BCNF Decomposition • R = (branch-name, branch-city, assets, customer-name, loan-number, amount) F = {branch-name assets branch-city loan-number amount branch-name} Key = {loan-number, customer-name} • Decomposition • R1 = (branch-name, branch-city, assets) • R2 = (branch-name, customer-name, loan-number, amount) • R3 = (branch-name, loan-number, amount) • R4 = (customer-name, loan-number) • Final decomposition R1, R3, R4

  17. BCNF and Dependency Preservation • In general, there may not be a dependency preserving decomposition into BCNF. • e.g., Reserves (S,B,D), SB  D, D  B • 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C, SD  P and J  S). • However, it is a lossless join decomposition. • In this case, adding JPC to the collection of relations gives us a dependency preserving decomposition. • JPC tuples stored only for checking FD! (Redundancy!)

  18. Decomposition into 3NF • Obviously, the algorithm for lossless join decomp into BCNF can be used to obtain a lossless join decomp into 3NF (typically, can stop earlier). • To ensure dependency preservation, one idea: • If X Y is not preserved, add relation XY. • Problem is that XY may violate 3NF! e.g., consider the addition of CJP to `preserve’ JP C. What if we also have J  C ? • Refinement: Instead of the given set of FDs F, use a minimal cover for F.

  19. Minimal Cover for a Set of FDs • Minimal coverG for a set of FDs F: • Closure of F = closure of G. • Right hand side of each FD in G is a single attribute. • If we modify G by deleting an FD or by deleting attributes from an FD in G, the closure changes. • Intuitively, every FD in G is needed, and ``as small as possible’’ in order to get the same closure as F. • e.g., A  B, ABCD  E, EF  GH, ACDF  EG has the following minimal cover: • A  B, ACD  E, EF  G and EF  H

  20. Dependency preserving and lossless join Decomposition into 3NF • Find a minimal cover G for F. • For each left-hand-side X of a FD that appears in G create a relation schema in D with attributes {X {A1} ,..., {Ak}}, where X A1, ..., X Ak are the only dependencies in G with X as left-hand-side (X is the key of this relation). • If none of the relation schemas in D contains a key of R, then create one more relation schema in D that contains attributes that form a key of R. Example: R = {A,B,C,D,E,G}, F = {ABCD, DEG}, where DEG violates 3NF. R is decomposed into two relations, R1 = {A,B,C,D} and R2= {D,E,G}.

  21. since name dname ssn lot did budget Works_In Employees Departments budget since name dname ssn did lot Works_In Employees Departments Refining an ER Diagram • 1st diagram translated: Workers(S,N,L,D,S) Departments(D,M,B) • Lots associated with workers. • Suppose all workers in a dept are assigned the same lot: DL • Redundancy; fixed by: Workers2(S,N,D,S) Dept_Lots(D,L) • Can fine-tune this: Workers2(S,N,D,S) Departments(D,M,B,L) Before: After:

  22. Summary of Schema Refinement • If a relation is in BCNF, it is free of redundancies that can be detected using FDs. Thus, trying to ensure that all relations are in BCNF is a good heuristic. • If a relation is not in BCNF, we can try to decompose it into a collection of BCNF relations. • Must consider whether all FDs are preserved. If a lossless-join, dependency preserving decomposition into BCNF is not possible (or unsuitable, given typical queries), should consider decomposition into 3NF. • Decompositions should be carried out and/or re-examined while keeping performance requirements in mind.

More Related