Download presentation

Presentation is loading. Please wait.

Published byAshley Lawrence Modified about 1 year ago

1
Normalization 如何將表格切割而不造成資料遺失

2
The Evils of Redundancy Redundancy is at the root of several problems associated with relational schemas: redundant storage, insert/delete/update anomalies( 資料庫管理困難 ) Integrity constraints, in particular functional dependencies, can be used to identify schemas with such problems and to suggest refinements.

3
Introduction to Database Systems3 Main refinement technique: decomposition (replacing ABCD with, say, AB and BCD, or ACD and ABD). Decomposition should be used judiciously: Is there reason to decompose a relation? What problems (if any) does the decomposition cause?

4
Functional Dependencies (FDs) A functional dependency X Y holds over relation R if, for every allowable instance r of R: t1 r, t2 r, X (t1) = X (t2) implies Y (t1) = Y (t2) i.e., given two tuples in r, if the X values agree, then the Y values must also agree. (X and Y are sets of attributes.) An FD is a statement about all allowable relations. Must be identified based on semantics of application. Given some allowable instance r1 of R, we can check if it violates some FD f, but we cannot tell if f holds over R! K is a candidate key for R means that K R However, K R does not require K to be minimal!

5
Example: Constraints on Entity Set Consider relation obtained from Hourly_Emps: Hourly_Emps (ssn, name, lot, rating, hrly_wages, hrs_worked) Notation: We will denote this relation schema by listing the attributes: SNLRWH This is really the set of attributes {S,N,L,R,W,H}. Sometimes, we will refer to all attributes of a relation by using the relation name. (e.g., Hourly_Emps for SNLRWH) Some FDs on Hourly_Emps: ssn is the key: S SNLRWH rating determines hrly_wages: R W

6
Example Problems due to R W : Update anomaly: Can we change W in just the 1st tuple of SNLRWH? Insertion anomaly: What if we want to insert an employee and don’t know the hourly wage for his rating? Deletion anomaly: If we delete all employees with rating 5, we lose the information about the wage for rating 5! Hourly_Emps2 Wages

7
Reasoning About FDs Given some FDs, we can usually infer additional FDs: ssn did, did lot implies ssn lot An FD f is implied by a set of FDs F if f holds whenever all FDs in F hold. F + = closure of F is the set of all FDs that are implied by F. Armstrong’s Axioms (X, Y, Z are sets of attributes): Reflexivity: If X Y, then X Y Augmentation: If X Y, then XZ YZ for any Z Transitivity: If X Y and Y Z, then X Z

8
Reasoning About FDs (Contd.) Couple of additional rules (that follow from AA): Union: If X Y and X Z, then X YZ Decomposition: If X YZ, then X Y and X Z

9
Reasoning About FDs (Contd.) Computing the closure of a set of FDs can be expensive. (Size of closure is exponential in # attrs!) Typically, we just want to check if a given FD X Y is in the closure of a set of FDs F. An efficient check: Compute attribute closure of X (denoted X + ) wrt F: Set of all attributes A such that X A is in F + There is a linear time algorithm to compute this. Check if Y is in X +

10
Introduction to Database Systems10 計算 Closure 給一個 FD set F ，找出所有被 Attribute A 所 determine 的所有 attribute ，表示為 A + ；即 B A + if A B( 含推導而得者） 演算法 result := A while (result changes) do for each FD B C in F if B is in result then result := result ∪ C

11
Introduction to Database Systems11 範例 R （ W,X,Y,Z) FD: W Z YZ X WZ Y 計算 WZ + (1) result = {WZ} (F1) WZ Y result = {WYZ} (2) YZ X result = {WXYZ} 所以 WZ 為一 superkey

12
Introduction to Database Systems12 再計算 w + (1) result = {W} W Z result = {WZ} WZ Y result = {WYZ} YZ X result = {WXYZ} 所以 W 為一 candidate key 而 WZ 不是 Candidate key

13
Introduction to Database Systems13 計算 Redundant FD 定義：一條 FD X Y 是一條 redundant FD 如果 他可以被不包括它的 FD 聯合推得 演算法 1. Choose a candidate FD, say X Y, remove it from F 2. Result := {x}; while (result changes and Y is not contained in result) do for each FD, A B, remaining in the reduced set of FDs if A is a subset of result then result := result ∪ B end 3. If Y is a subset of result then, FD X Y is redundant

14
Introduction to Database Systems14 範例 FDs (1)W Z (2) W Y (3)YZ X (4)WZ Y Check rule W Y {W} {WZ} {WYZ} {WXYZ} W 在 {WXYZ} 中, 所以 (2) W Y 是一條 redundant rule (remove it)

15
Normal Forms Back to schema refinement… Q1: is any refinement is needed??! If a relation is in a normal form (BCNF, 3NF etc.): we know that certain problems are avoided/minimized. helps decide whether decomposing a relation is useful.

16
Boyce-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.

17
Introduction to Database Systems17 I.e.: R is in BCNF if the only non-trivial FDs over R are key constraints. No dependency in R that can be predicted using FDs alone. 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, unless X is a key. If example relation is in BCNF, and X A, the 2 tuples must be identical (since X is a key).

18
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 is part of some key (not superkey!) for R. (A is prime)

19
Introduction to Database Systems19 Minimality of a key is crucial in third condition above! 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). Lossless-join, dependency-preserving decomposition of R into a collection of 3NF relations always possible.

20
Introduction to Database Systems20 Key review Key: minimal subset of the fields (attributes) of a relation that is a unique identifier for a tuple. Super key: a set of fields (attributes) that includes the key

21
Decomposition of a Relation Scheme Suppose that relation R contains attributes A1... An. A decomposition of 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.

22
Introduction to Database Systems22 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. {ssn, name, lot, rating, hourly_wages, hours_worked}

23
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 Q: potential problems of decomposition?

24
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) · May be impossible to reconstruct the original relation! Fortunately, not in the SNLRWH example. ¸ Dependency checking may require joins. Fortunately, not in the SNLRWH example.

25
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).)

26
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.

27
Introduction to Database Systems27 驗證 Lossless Join preservation Simple rule R decompose into R1, R1 is lossless if 1. R1 ∩ R2 R1 (R2 含有 R1 的 Foreign Key), 或 2. R1 ∩ R2 R2 (R1 含有 R2 的 Foreign Key) 切割成多個可以以兩個的 case 推論

28
Introduction to Database Systems28 正式演算法 給定 Functional dependency set F; R(A1,A2,…,An) decompose 為 R1,R2,…,Rm, 決定是否是 Lossless decomposition 的方法 1. Construct an m by n tables s; where columns for attributes and rows for the decomposed relations 2. For each cell s(i, j) of s, if the attribute for column Aj is in row Ri, put a(j) in the cell else put b(I,j) in the cell. (Initialization)

29
Introduction to Database Systems29 3. Repeat the following until no changes on s For each X Y in F 找出所有 X attribute 的 column 內為 a 的 row （切割 ) 者, 如果有任一個 row 其 column Y 內為 a, 則這堆 row 的 Y column 都放 a 4. 如果做完 step ３之後有任一個 row 的所有 column 都是 a, 則為 lossless, 否則為 lossy.

30
Introduction to Database Systems30 範例 R(A,B,C,D,E) FD: A C --- (1) ;AB D - --(2); D E ---(3); 切割成 R1(A,C) R2(A,B,D) R3(D,E) Step 1&2 建表 Step3 考慮 rule 1; R1, R2, 在 column A 上 都是 a 而 R1 在 C 的位置是 a(3); 所以 R2 在 column C 的位置放 a(3); 同理 rule 3 使我們在 R2 的 column E 放 a(5)

31
Introduction to Database Systems31 ABCDE R1(A,C)a(1)b(2,1)a(3)b(1,4)b(1,5) R2(A,B,D)a(1)a(2)b(2,3)a(4)b(2,5) R3(D,E)b(3,1)b(3,2)b(3,3)a(4)a(5)

32
Dependency Preserving Decomposition Consider CSJDPQV, C is key, JP C and SD P. {contractid, supplierid, projectid,deptid,partid, qty, value} BCNF decomposition: CSJDQV and SDP Problem: Checking JP C requires a join!

33
Introduction to Database Systems33 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, the projection of F on X (denoted F X ) is the set of FDs U V in F + (closure of F ) such that all of the attributes U, V are in X.

34
Dependency Preserving Decompositions (Contd.) Decomposition of R into X and Y is dependency preserving if (F X F Y ) + = 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 +.

35
Introduction to Database Systems35 Important to consider 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.

36
Decomposition 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, and guaranteed to terminate. e.g., CSJDPQV, key C, JP C, SD P, J S {contractid, supplierid, projectid,deptid,partid, qty, value} To deal with SD P, decompose into SDP, CSJDQV. To deal with J S, decompose CSJDQV into JS and CJDQV

37
Introduction to Database Systems37 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!

38
BCNF and Dependency Preservation In general, there may not be a dependency preserving decomposition into BCNF. e.g., CSZ, CS Z, Z C Can’t decompose while preserving 1st FD; not in BCNF.

39
Introduction to Database Systems39 Similarly, decomposition of CSJDPQV into SDP, JS and CJDQV is not dependency preserving (w.r.t. the FDs JP C, SD P and J S). {contractid, supplierid, projectid,deptid,partid, qty, value} 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!)

40
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. Refinement: Instead of the given set of FDs F, use a minimal cover for F.

41
Minimal Cover for a Set of FDs Minimal cover G 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.

42
Introduction to Database Systems42 Minimal cover Put the FDs in a standard form: single attribute on the right side ( using decomposition axiom) A->BC change to A->B, A->C Minimize the left side of each FD ABC->D and B->C ; change ABC->D to AB->D Delete redundant FDs

43
Introduction to Database Systems43 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

44
Summary of Schema Refinement BCNF: free of redundancies detectable by FDs. ensuring BCNF is a good heuristic. Not in BCNF? Try decomposing into BCNF relations. Must consider whether all FDs are preserved!

45
Introduction to Database Systems45 Lossless-join, dependency preserving decomposition into BCNF impossible? Consider 3NF. Same if BCNF decomp is unsuitable for typical queries Decompositions should be carried out and/or re-examined while keeping performance requirements in mind.

46
Introduction to Database Systems46 習題 Consider the universal relation R = {A, B, C, D, E, F, G, H, I} and the set of functional dependencies F = { {A, B} -> {C}, {A} -> {D, E}, {B} -> {F}, {F} -> {G, H}, {D} -> {I, J} }. What is the key for R? Decompose R into 2NF, then 3NF relations.

47
Introduction to Database Systems Repeat exercise for the following different set of functional dependencies G = { {A, B} -> {C}, {B, D} -> {E, F}, {A, D} -> {G, H}, {A} -> {I}, {H} -> {J} }. Answer:

48
Introduction to Database Systems Consider the relation R, which has attributes that hold schedules of courses and sections at a university; R = {CourseNo, SecNo, OfferingDept, CreditHours, CourseLevel, InstructorSSN, Semester, Year, Days_Hours, RoomNo, NoOfStudents}. Suppose that the following functional dependencies hold on R: {CourseNo} -> {OfferingDept, CreditHours, CourseLevel} {CourseNo, SecNo, Semester, Year} -> {Days_Hours, RoomNo, NoOfStudents, InstructorSSN} {RoomNo, Days_Hours, Semester, Year} -> {InstructorSSN, CourseNo, SecNo} Try to determine which sets of attributes form keys of R. How would you normalize this relation?

49
Introduction to Database Systems Consider the following two sets of functional dependencies F= {A ->C, AC - >D, E ->AD, E ->H} and G = {A ->CD, E ->AH}. Check whether or not they are equivalent.

50
Introduction to Database Systems consider the following decomposition for the relation schema R of Exercise Determine whether each decomposition has (i) the dependency preservation property, and (ii) the lossless join property, with respect to F. Also determine which normal form each relation in the decomposition is in. a. D1 = {R1, R2, R3, R4, R5}; R1 = {A,B,C}, R2={A,D,E}, R3={B,F}, R4={F,G,H}, R5={D,I,J} b. D2={R1,R2,R3}; R1={A,B,C,D,E}, R2={B,F,G,H}, R3={D,I,J} c. D3= {R1,R2,R3,R4,R5}; R1={A,B,C,D}, R2={D,E}, R3={B,F}, R4={F,G,H}, R5={D,I,J}

51
Introduction to Database Systems Consider the following relation REFRIG(Model#, Year, Price, Manu_Plant, Color), which is abbreviated as REFRIG(M,Y,P,MP,C), and with the following set F of functional dependencies:F = {M MP, {M,Y} P, MP C} a. Evaluate each of the following as a candidate key for REFRIG, given reasons why it can or cannot be a key:{M}, {M,Y},{M,C}. b. Based on the above key determination, state whether the relation REFRIG is n 3NF and in BCNF, giving proper reasons. c. Consider the decomposition of REFRIG into D={R1(M,Y,P), R2(M,MP,C)}. Is this decomposition lossless? Show why.

Similar presentations

© 2016 SlidePlayer.com Inc.

All rights reserved.

Ads by Google