Presentation is loading. Please wait.

Presentation is loading. Please wait.

Midterm 3 Revision 2 Prof. Sin-Min Lee Department of Computer Science.

Similar presentations


Presentation on theme: "Midterm 3 Revision 2 Prof. Sin-Min Lee Department of Computer Science."— Presentation transcript:

1 Midterm 3 Revision 2 Prof. Sin-Min Lee Department of Computer Science

2 Normal Forms 1NF 2NF 3NF BCNF 4NF 5NF Functional dependencies Multivalued dependencies Join dependencies

3 F + : dependencies induced by Armstrong’s Axioms Axioms for reasoning about FD’s (i), (ii) and (iii) are Armstrong’s axioms F + is the set of dependencies which can be deduced from F by applying three inference rules: (i) reflexivityif Y  X then X  Y (ii) augmentation if X  Y then XZ  YZ (iii) transitivityif X  Y and Y  Z then X  Z

4 Idea If X  Y cannot be deduced using Armstrong’s axioms then there is a relational instance for R in which all the dependencies in F are true, but X  Y does not hold R=LMNO X=L F={L  M, M  N, O  N} then X + = LMN LMN O L  O cannot be deduced in F* Completeness of Armstrong Axioms L  O cannot be deduced in F + Counterexample:

5 Let U be a set of attributes and F be a set of functional dependencies on U. Suppose that X  U is a set of attributes. Definition: X + = { A | F X  A} We would like to compute X + Closure of a Set of Attributes |=|=

6 R= ( A, B, C ) F = {A  B, B  C } F + = {A  A, B  B, C  C, AB  AB, BC  BC, AC  AC, ABC  ABC, AB  A, AB  B, BC  B, BC  C, AC  A, AC  C, ABC  AB, ABC  BC, ABC  AC, ABC  A, ABC  B, ABC  C, A  B, … (1) ( given ) B  C, … (2) ( given ) A  C, … (3) ( transitivity on (1) and (2) ) AC  BC, … (4) ( augmentation on (1) ) AC  B,… (5) ( decomposition on (4) ) A  AB,… (6) ( augmentation on (1) ) AB  AC, AB  C, B  BC, A  AC, AB  BC, AB  ABC, AC  ABC, A  BC, A  ABC } Using reflexivity, we can generate all trivial dependencies Note that A, B, C, are attributes We refer to the set {A,B} simply as AB

7 Algorithm to compute closure of attributes X + under F closure := X ; Repeat for each U  V in F do begin if U  closure then closure := closure  V ; end Until (there is no change in closure)

8

9 R( A, B, C, G, H, I ) F= {A  B, A  C, CG  H, CG  I, B  H } To compute AG + closure = AG closure = ABG ( A  B ) closure = ABCG ( A  C ) closure = ABCGH ( CG  H ) closure = ABCGHI ( CG  I ) Is AG a candidate key? AG  R A +  R ? G +  R ?

10 Example R(ABCDE) F={AB  C, CE  B, D  A, BC  E} –{A} + = –{A,B} + = –{B,D} + =

11 R- a relation schema F- set of functional dependencies on R The decomposition of R into relations with attribute sets R 1, R 2 is a lossless-join decomposition iff ( R 1  R 2 )  R 1  F + OR ( R 1  R 2 )  R 2  F + Theorem: i.e., R 1  R 2 is a superkey for R 1 or R 2. (the attributes common to R 1 and R 2 must contain a key for either R 1 or R 2 ).

12 Example R(A,B,C,D,E)  = {R1(A,D), R2(A,B),R3(B,E), R4(C,D,E), R5(A,E)} FD1. A  C FD2. B  C FD3. C  D FD4. DE  C FD5. CE  A. Decide whether the decomposition is lossless.

13 BCNF Decomposition It is a lossless join decomposition. But not necessary dependency preserving Suppose R is not in BCNF, A is an attribute, and X  A is a FD where X  A =  that violates the condition. 1.Remove A from R 2.Create a new relational schema XA 3.Repeat this process until all the relations are in BCNF

14 CSJDPQV SDP CSJDQV SD  P JS CJDQV JSJS JSJS Key is C

15 SD  P CSJDPQV SDP CSJDQV SD  P JS CJDQV JSJS JSJS Key is C JP  C CJP Does not preserve JP  C, we can add a schema: Each of SDP, JS, CJDQV, CJP is in BCNF, but there is redundancy in CJP. The result is in BCNF

16 SD  P CSJDPQV SDP CSJDQV SD  P SDQ CSJDV SD  Q Key is C SD is a key in SDP and SDQ, There is no dependency between P and Q we can combine SDP and SDQ into one schema Resulting in SDPQ, CSJDV Possible refinement

17 Overview It is possible to decompose any relational schema into a set of relational schemas with the following properties: 1)Attribute Preserving 2)FDs preserving 3)Lossless Join

18 The Decomposition Algorithm Step 1. Find a minimal cover G for F Step 2. For each left-hand side X that appears in G, create a relation schema with attributes {X  A1  A2,...,  Am} where X  A1, X  A2,..., X  Am are all dependencies in G with X as left-hand side. Step 3. If none of the relation schemas contains a key of R, create one more relation schema that contains attributes that form a key for R.

19 Example Consider R(A, B, C, D, E) and F={AB  C, A  BE, C  E} Step 1.minimal cover Fmin={A  C, A  B, C  E} Step 2. R1(A,B,C), R2(C,E) Step 3. Key: {A,D} we have R3(A,D) Final Result: R1(A, B,C), R2(C,E), and R3(A,D)

20 BCNF Decomposition Property LJ1: A decomposition D={R1, R2} of R has the lossless join property with respect to a set of functional dependencies F on R if and only if either – the FD ((R1  R2)  (R1 - R2)) is in F +, or – the FD ((R1  R2)  (R2 - R1)) is in F +

21 BCNF Decomposition Property LJ2: If a decomposition D={R1, R2,..., Rm} of R has the lossless join property with respect to a set of functional dependencies F on R, and if a decomposition D1={Q1, Q2,...,Qk} of Ri has the lossless join property with respect to the projection of F on Ri, then the decomposition D2={R1, R2,... Ri-1, Q1, Q2,..., Qk, Ri+1,..., Rm} of R has the lossless join property with respect to F

22 BCNF Decomposition - Algorithm 1.Set D  {R} 2.While there is a relation schema Q in D that is not in BCNF do begin choose a relation schema Q in D that is not in BCNF; find a functional dependency X  Y in Q that violates BCNF; replace Q in D by two schemas (Q-Y) and (X  Y) end;

23 BCNF Decomposition - Example Consider R(A,B,C,D) and F={A  B, B  C, D  B} Decompose R into BCNF relations. Step 1.D={R(A,B,C,D)} Step 2. Loop 1.R is not in BCNF because A  B and A is not a superkey decompose R into R1(A, C, D), and R2(A, B) Loop 2. R1 is not in BCNF because A  C and A is not a superkey decompose R1 into R11(A, D) and R12(A, C) Result:D={R11(A,D), R12(A,C), R2(A,B)}

24 Overview Given a relation schema R(A1, A2,..., An). If R is not in the third normal form (3NF), we wan to decompose it into a set of relation schema D= { R1, R2,...,Rm }, where each Ri is in 3NF, such that the following conditions are held:  Attribute preservation condition.  Dependency preservation condition  Lossless join condition

25 Attribute Preservation Condition Attribute preservation condition states that the union of attributes of Ri equal to the set of attributes of R. For example: Given R(A, B, C, D) and the decomposition D1={ R1(A,B), R2(B,C) and R3(A,C,D)}. D1 satisfies the attribute preservation condition.

26 Attribute Preservation Condition Given R(A, B, C, D) and the decomposition D2={R1(A, B), R2(B,C), R3(A, C)}, The attribute preservation condition is violated because D is missing (not preserved in the decomposition).

27 Dependency Preservation Condition We say that a decomposition D={R1, R2,..., Rm} of R is dependency preserving with respect to F if the union of the projections of F on each Ri in D is equivalent to F. That is: ((  F (R1) ...   F (Rm)) + = F + Given a set of dependencies F on R, the projection of F on Ri, denoted by  F (Ri) where Ri is a subset of R, is the set of dependencies X  Y in F + such that the attributes in X  Y are all contained in Ri.

28 Dependency Preservation Condition Given R(A, B, C, D) and F = { A  B, B  C, C  D} Let D1={R1(A,B), R2(B,C), R3(C,D)}  F (R1)={A  B}  F (R2)={B  C}  F (R3)={C  D} FDs are preserved.

29 Dependency Preservation Condition Given R(A, B, C, D) and F = { A  B, B  C, C  D} Let D2={R1(A,B}, R2(B,C), R3(A, D)}, then FDs are not preserved.

30 Dependency Preservation Condition Given R(A, B, C, D) and F = { A  B, B  C, C  D} Let D2={R1(A,B}, R2(B,C), R3(A, D)}, then FDs are not preserved.

31 Dependency Preservation Condition We want to preserve the dependencies because each dependency in F represents a constraint on a database.

32 Dependency Preservation Condition If one of the dependencies is not represented by the dependencies on some individual relation Ri of the decomposition, we will not be able to enforce this constraint by looking only at an individual relation, instead, to enforce the constraint, we will have to join two or more of the relations in the decomposition and then check that functional dependency hold in the result of the join operation. This is very inefficient.

33 Multivalued Dependencies and Fourth Normal Form Formal Definition of Multivalued Dependency

34 Multi-Valued Dependency Problem: multi-valued (or binary join) dependency –Definition: If every instance of schema R can be (losslessly) decomposed using attribute sets (X, Y) such that: r =  X (r)  Y (r) then a multi-valued dependency exists Ex : Person =  SSN,PhoneN (Person)  SSN,ChildSSN (Person)

35 Fourth Normal Form A schema is in fourth normal form if for every non-trivial multi-valued dependency: R = XY either: - X  Y or Y  X (trivial case) or - X  Y is a superkey of R (i.e., X  Y  R )

36 4th Normal Form No multivalued dependencies and BCNF Create separate tables for each separate functional dependency

37 Example SalesForce (State, SalesPerson) Delivery (State, Delivery)

38 Beyond 4th Normal Form 5th Normal Form –Project-Join Normal Form Domain Key Normal Form (DKNF)

39 Assume the relation R contains the following two tuples R(A B C D) ( ) … ( ) … What other tuples must R contain so that A ->-> B and A ->-> C hold for R ? Answer: The tuples that must be included due to the two multi-valued dependency are: ( ) ( ) ( ) ( ) ( ) second round ( ) second round

40 Example Consider the following relation and determinants. R(a,b,c,d) a,c -> b,d a,d -> b To be in BCNF, all valid determinants must be a candidate key. In the relation R, a,c->b,d is the determinate used, so the first determinate is fine. a,d->b suggests that a,d can be the primary key, which would determine b. However this would not determine c. This is not a candidate key, and thus R is not in BCNF.

41 Tuple Relational Calculus based on specifying a number of tuple variables a tuple variable refers to any tuple

42 Simple example 1 To find all employees whose salary is greater than $50,000 –{t| EMPLOYEE(t) and t.Salary>5000} where EMPLOYEE(t) specifies the range of tuple variable t –The above operation selects all the attributes

43 Simple example 2 To find only the names of employees whose salary is greater than $50,000 –{t.FNAME, t.NAME| EMPLOYEE(t) and t.Salary>5000} The above is equivalent to SELECT T.FNAME, T.LNAME FROM EMPLOYEE T WHERE T.SALARY > 5000

44 Elements of a tuple calculus In general, we need to specify the following in a tuple calculus expression: –Range Relation (I.e, R(t)) = FROM –Selected combination= WHERE –Requested attributes= SELECT

45 Elements of formula A formula is made of Predicate Calculus atoms: – an atom of the from R(ti) –t i.A op t j.B op  {=,,..} –F1 And F2 where F1 and F2 are formulas –F1 OR F2 –Not (F1) –F’=(  t) (F) or F’= (  t) (F)  Y friends (Y, John)  X likes(X, ICE_CREAM)

46 More Example For every project located in ‘Stafford’, retrieve the project number, the controlling department number, and the last name, birthrate, and address of the manger of that department.

47 Domain Relational Calculus (DRC) Another type of formal predicate calculus- based language QBE is based on DRC The language shares a lot of similarities with the tuple calculus

48 DRC The only difference is the type of variables: –variables range over singles values from domains of attributes An expression of DRC is: –{x 1, x 2,…,x n |COND(x 1,x 2,…,x n, x n+2,…,x n+m )} where x 1,x 2,…,x n+m are domain var range over attributers COND is a condition (or formula)

49 Examples Retrieve the birthdates and address of the employee whose name is ‘John B. Smith’ {uv| (  q)(  r)(  s) (EMPLOYEE(qrstuvwxyz) and q=‘John’ and r=‘B’ and s=‘Smith’

50 Alternative notation Ssign the constants ‘John’, ‘B’, and ‘Smith’ directly {uv|EMPLOYEE (‘John’, ’B’, ’Smith’,t,u,v,x,y,z)}

51 More example Retrieve the name and address of all employees who work for the ‘Reseach’ department {qsv | (  z) EMPLOYEE(qrstuvwxyz) and (  l) (  m) (DEPARTMENT (lmno) and l=‘Research’ and m=z))}

52 More example List the names of managers who have at least on e dependent {sq| (  t) EMPLOYEE(qrstuvwxyz) and ((  j)( DEPARTMENT (hijk) and ((  l) | (DEPENTENT (lmnop) and t=j and t=l))))}

53 Characteristics of a Decomposition Two important characteristics of a decomposition: –lossless join: necessary, otherwise original relation cannot be recreated, even if tables are not modified –dependency preserving: allows us to check that inserts/updates are correct without joining the sub-relations

54 Lossless Join CS DBCohen OSLevy DBLevy TS SmithCohen JonesLevy SmithLevy TCS SmithDBCohen JonesOSLevy SmithDBLevy

55 Checking Check for a lossless join using the algorithm from class (with the a-s and b-s) Check for dependency preserving using an algorithm shown today

56 Dependency Preservation R=ABC Decomposition {AB, AC} Dependencies {A  B, B  C}. Is it lossless? Does this decomposition preserve B  C?

57 Dependency Preservation (cont’d) CBA BA CA

58 Definitions We define  S (F) to be the set of dependencies X  Y in F + such that X and Y are in S. We say that a decomposition R 1...R n of R is dependency preserving if for all instances r of R that satisfy the FDs of R: (  R 1 (F) U... U  R n (F))+ = F+ Note that one inclusion clearly holds always. This definition implies an exponential algorithm to check if a decomposition is dependency preserving We give a polynomial algorithm

59 Algorithm Let R be a relation, decomposed into R 1, R 2,…,R n Let F be a set of functional dependencies To check whether R 1,…,R n preserves all the functional dependencies in F, run the algorithm on the next slide for each X -> Y in F If the answer is “Yes” for all FDs, then the decomposition preserves F If the answer is “No” for at least one FD, then the decomposition does not preserve F

60 Testing Dependency Preservation To check if the decomposition preserves X  Y: Z:=X while changes to Z occur do for i=1 to n do Z:= Z  ((Z  R i ) +  R i ) if Y  Z return “yes” else return “no”

61 Example (1) R=ABCD F = {A -> B, B -> C, C -> D, D -> A} R 1 =AB, R 2 =BC, R 3 =CD Is this decomposition dependency preserving?

62 Example (2) R = ABCDE F = {A -> ABCDE, BC -> A, DE -> C} Suppose we decompose R into ABDE and DEC. Is the decomposition dependency preserving?

63 Normal Forms The basic idea: if a relation is in one of these forms, then it avoids certain problems (e.g., redundancy) Normal Forms: –BCNF: Every dependency X->A in F+ must be (1) trivial or (2) X is a super-key –3NF: Every dependency X->A in F+ must be (1) trivial, (2) X is a super-key or (3) A is an attribute of a key

64 Example Reminder F+ = {X -> X+ | exist Y->Z in F st Y in X and Z not in X} Suppose that R = ABC. For each of the following values of F, decide whether R is in BCNF/3NF: –F = {} –F = {A -> B} –F = {A -> B, A -> C} –F = {A -> B, B -> C} –F = {A -> B, BC -> A}

65 Decomposition into 3NF Given a relation R with functional dependencies F Step 1: Find a non-redundant cover G of F Step 2: For each FD X  A in G, create a schema XA Step 3: If no schema created so far contains a key, add a key as a schema Step 4: Remove schemas that are contained in other schemas The result is a decomposition into 3NF that is dependency preserving and has a lossless join

66 Example Find a decomposition into 3NF for the relation R = ABCDEFGH, with the functional dependencies F = {A  B, ABCD  E, EF  GH, ACDF  EG}

67 Example Non-redundant cover G = {A  B, ACD  E, EF  G, EF  H} Key ACDF Schema: AB, ACDE, EFG, EFH, ACDF


Download ppt "Midterm 3 Revision 2 Prof. Sin-Min Lee Department of Computer Science."

Similar presentations


Ads by Google