1 / 67

Midterm 3 Revision 2

CS157A Lecture 20. Midterm 3 Revision 2. Prof. Sin-Min Lee Department of Computer Science. Normal Forms. BCNF. 5NF. 2NF. 4NF. 3NF. 1NF. Functional dependencies. Multivalued dependencies. Join dependencies. F + : dependencies induced by Armstrong’s Axioms.

ami
Download Presentation

Midterm 3 Revision 2

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. CS157A Lecture 20 Midterm 3 Revision 2 Prof. Sin-Min Lee Department of Computer Science

  2. Normal Forms BCNF 5NF 2NF 4NF 3NF 1NF 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) reflexivity if Y  X then X ® Y (ii) augmentation if X ® Y then XZ ® YZ (iii) transitivity if X ® Y and Y ® Z then X ® Z

  4. Completeness of Armstrong Axioms • 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 L ® O cannot be deduced in F* L M N O L ® O cannot be deduced in F+ Counterexample:

  5. Closure of a Set of Attributes • 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+ |=

  6. Note that A, B, C, are attributes We refer to the set {A,B} simply as AB Using reflexivity, we can generate all trivial dependencies 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 }

  7. Algorithm to compute closure of attributes X+ under F closure := X ; Repeat for eachU  VinFdo begin ifU  closure thenclosure := closure  V ; end Until (there is no change in closure)

  8. 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 ?

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

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

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

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

  13. Key is C CSJDPQV SDP JS SDP CSJDQV SDP JS CJDQV JS

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

  15. Possible refinement CSJDPQV Key is C SDP SDP CSJDQV SDP SDQ SDQ CSJDV SDQ 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

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

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

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

  19. 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+

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

  21. 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;

  22. 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)}

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

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

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

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

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

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

  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 • We want to preserve the dependencies because each dependency in F represents a constraint on a database.

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

  32. Multivalued Dependencies and Fourth Normal Form • Formal Definition of Multivalued Dependency

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

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

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

  36. Example SalesForce (State, SalesPerson) Delivery (State, Delivery)

  37. Beyond 4th Normal Form • 5th Normal Form • Project-Join Normal Form • Domain Key Normal Form (DKNF)

  38. Assume the relation R contains the following two tuples R(A B C D) ( 1 2 3 4 ) … ( 1 5 6 7 ) …   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: (1 2 6 7) (1 5 3 4) (1 2 6 4) (1 5 3 7) (1 2 3 7) second round (1 5 6 4) second round

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

  40. Tuple Relational Calculus • based on specifying a number of tuple variables • a tuple variable refers to any tuple

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

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

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

  44. Elements of formula • A formula is made of Predicate Calculus atoms: • an atom of the from R(ti) • ti.A op tj.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)

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

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

  47. DRC • The only difference is the type of variables: • variables range over singles values from domains of attributes • An expression of DRC is: • {x1, x2,…,xn|COND(x1,x2,…,xn, xn+2,…,xn+m)} • where x1,x2,…,xn+m are domain var range over attributers • COND is a condition (or formula)

  48. 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’

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

More Related