Download presentation

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

2
**The Evils of Redundancy**

Introduction to Database Systems 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. Department of Information Management

3
**Introduction to Database Systems**

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? Introduction to Database Systems

4
**Functional Dependencies (FDs)**

Introduction to Database Systems Functional Dependencies (FDs) A functional dependency X Y holds over relation R if, for every allowable instance r of R: t1 r, t2 r, pX (t1) = pX (t2) implies pY (t1) = pY (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! Department of Information Management

5
**Example: Constraints on Entity Set**

Introduction to Database Systems 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 Department of Information Management

6
**Introduction to Database Systems**

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 Department of Information Management

7
**Introduction to Database Systems**

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 Department of Information Management

8
**Reasoning About FDs (Contd.)**

Introduction to Database Systems 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 Department of Information Management

9
**Reasoning About FDs (Contd.)**

Introduction to Database Systems 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+ Department of Information Management

10
**Introduction to Database Systems**

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

11
**Introduction to Database Systems**

範例 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 Introduction to Database Systems

12
**Introduction to Database Systems**

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

13
**Introduction to Database Systems**

計算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 Introduction to Database Systems

14
**Introduction to Database Systems**

範例 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) Introduction to Database Systems

15
**Introduction to Database Systems**

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. Department of Information Management

16
**Boyce-Codd Normal Form (BCNF)**

Introduction to Database Systems 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. Department of Information Management

17
**Introduction to Database Systems**

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). Introduction to Database Systems

18
**Introduction to Database Systems**

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) Department of Information Management

19
**Introduction to Database Systems**

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. Introduction to Database Systems

20
**Introduction to Database Systems**

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 Introduction to Database Systems

21
**Decomposition of a Relation Scheme**

Introduction to Database Systems 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. Department of Information Management

22
**Introduction to Database Systems**

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} Introduction to Database Systems

23
**Example Decomposition**

Introduction to Database Systems 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? Department of Information Management

24
**Problems with Decompositions**

Introduction to Database Systems 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. Department of Information Management

25
**Lossless Join Decompositions**

Introduction to Database Systems 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).) Department of Information Management

26
**Introduction to Database Systems**

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. Department of Information Management

27
**驗證Lossless Join preservation**

Simple rule R decompose into R1, R1 is lossless if R1∩R2 R1 (R2含有R1的Foreign Key), 或 R1∩R2 R2 (R1含有R2的Foreign Key) 切割成多個可以以兩個的case 推論 Introduction to Database Systems

28
**Introduction to Database Systems**

正式演算法 給定Functional dependency set F; R(A1,A2,…,An) decompose 為R1,R2,…,Rm, 決定是否是Lossless decomposition的方法 Construct an m by n tables s; where columns for attributes and rows for the decomposed relations 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) Introduction to Database Systems

29
**Introduction to Database Systems**

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 如果做完step３之後有任一個row的所有column都是a, 則為lossless, 否則為lossy. Introduction to Database Systems

30
**Introduction to Database Systems**

範例 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) Introduction to Database Systems

31
**Introduction to Database Systems**

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

32
**Dependency Preserving Decomposition**

Introduction to Database Systems 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! Department of Information Management

33
**Introduction to Database Systems**

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

34
**Dependency Preserving Decompositions (Contd.)**

Introduction to Database Systems Dependency Preserving Decompositions (Contd.) Decomposition of R into X and Y is dependency preserving 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 +. Department of Information Management

35
**Introduction to Database Systems**

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. Introduction to Database Systems

36
**Decomposition into BCNF**

Introduction to Database Systems 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 Department of Information Management

37
**Introduction to Database Systems**

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

38
**BCNF and Dependency Preservation**

Introduction to Database Systems 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. Department of Information Management

39
**Introduction to Database Systems**

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!) Introduction to Database Systems

40
**Introduction to Database Systems**

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. Department of Information Management

41
**Minimal Cover for a Set of FDs**

Introduction to Database Systems 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. Department of Information Management

42
**Introduction to Database Systems**

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 Introduction to Database Systems

43
**Introduction to Database Systems**

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 Introduction to Database Systems

44
**Summary of Schema Refinement**

Introduction to Database Systems 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! Department of Information Management

45
**Introduction to Database Systems**

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. Introduction to Database Systems

46
**Introduction to Database Systems**

習題 14.26 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. Introduction to Database Systems

47
**Introduction to Database Systems**

14.27 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: Introduction to Database Systems

48
**Introduction to Database Systems**

14.30 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? Introduction to Database Systems

49
**Introduction to Database Systems**

14.19 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. Introduction to Database Systems

50
**Introduction to Database Systems**

15.29 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. 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} D2={R1,R2,R3}; R1={A,B,C,D,E}, R2={B,F,G,H}, R3={D,I,J} 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} Introduction to Database Systems

51
**Introduction to Database Systems**

15.30 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} 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}. Based on the above key determination, state whether the relation REFRIG is n 3NF and in BCNF, giving proper reasons. Consider the decomposition of REFRIG into D={R1(M,Y,P), R2(M,MP,C)}. Is this decomposition lossless? Show why. Introduction to Database Systems

Similar presentations

OK

Schema Refinement and Normal Forms Jianlin Feng School of Software SUN YAT-SEN UNIVERSITY courtesy of Joe Hellerstein for some slides.

Schema Refinement and Normal Forms Jianlin Feng School of Software SUN YAT-SEN UNIVERSITY courtesy of Joe Hellerstein for some slides.

© 2018 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Seminar ppt on google glass Ppt on main idea and key details Ppt on wildlife and natural resources Ppt on culture and science in the ancient period of philosophy Ppt on spiritual leadership book Ppt on beer lambert law problems Ppt on viruses and bacteria structure Ppt on eye oswego Ppt on bluetooth communication ios Ppt on latest technology in electrical