Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 480: Database Systems Lecture 22 March 6, 2013.

Similar presentations


Presentation on theme: "CS 480: Database Systems Lecture 22 March 6, 2013."— Presentation transcript:

1 CS 480: Database Systems Lecture 22 March 6, 2013

2 Non-Dependency Preserving BCNF Design
In this design: A student can have multiple advisors but at most one from a given department. An instructor can be associated with only a single department. Then the following functional dependencies hold: st_ID, dept_name  instr_ID instr_ID  dept_name The schema for dept_advisor is: dept_advisor(st_ID, instr_ID, dept_name) Problem: dept_advisor is not in BCNF because instr_ID appears in the left- hand side of a fd and is not a key. Then we should decompose. After BCNF decomposition: (st_ID, instr_ID) (instr_ID,dept_name)

3 Non-Dependency Preserving BCNF Design
These functional dependencies hold: st_ID, dept_name  instr_ID instr_ID  dept_name After BCNF decomposition: (st_ID, instr_ID) (instr_ID,dept_name) What is the problem with this BCNF design? It is not dependency preserving, to check whether the dependency “st_ID, dept_name  instr_ID” holds, one needs to check multiple relations. This is costly and undesirable. For cases like this, we need to relax BCNF and consider a weaker normal form called Third Normal Form (3NF).

4 Dependency Preservation
Let Fi be the set of dependencies from F + that include only attributes in Ri. A decomposition is dependency preserving, if: (F1  F2  …  Fn )+ = F + If it is not, then checking updates for violation of functional dependencies may require computing joins, which is expensive. However, checking dependency preservation with this property would be very expensive. F1, F2, …, Fn are the set of dependencies that can be checked efficiently. (F1  F2  …  Fn )+ cannot have more than F+ If it has less, then that means that you lost something…

5 Dependency Preservation
Let Fi be the set of dependencies from F + that include only attributes in Ri. A decomposition is dependency preserving, if: (F1  F2  …  Fn )+ = F + Example: R = (A,B,C), F = {B  A, C  B, C  A} Decompose to: R1 = (A,B) R2 = (B,C) Dependencies that hold: B  A C  B Is C  A lost? Is the decomposition dependency preserving? F1  F2 = {B  A, C  B} Since the closure of F1  F2 includes C  A, then the dependency is preserved and there’s no need of joining R1 and R2 to check the dependency. Example of why we need to check the closure and not the union alone…

6 Dependency Preservation
Let Fi be the set of dependencies from F + that include only attributes in Ri. A decomposition is dependency preserving, if: (F1  F2  …  Fn )+ = F + Example: R = (A,B,C), F = {B  A, C  B, C  A} Decompose to: R1 = (A,B) R2 = (B,C) Dependencies that hold: B  A C  B Is C  A lost? Is the decomposition dependency preserving? F1  F2 = {B  A, C  B} Since the closure of F1  F2 includes C  A, then the dependency is preserved and there’s no need of joining R1 and R2 to check the dependency. Example of why we need to check the closure and not the union alone…

7 Dependency Preservation
Let Fi be the set of dependencies from F + that include only attributes in Ri. A decomposition is dependency preserving, if: (F1  F2  …  Fn )+ = F + Example: R = (A,B,C), F = {B  A, C  B, C  A} Decompose to: R1 = (A,B) R2 = (B,C) Dependencies that hold: B  A C  B Is C  A lost? Is the decomposition dependency preserving? F1  F2 = {B  A, C  B} Since the closure of F1  F2 includes C  A, then the dependency is preserved and there’s no need of joining R1 and R2 to check the dependency. Example of why we need to check the closure and not the union alone…

8 Dependency Preservation
Let Fi be the set of dependencies from F + that include only attributes in Ri. A decomposition is dependency preserving, if: (F1  F2  …  Fn )+ = F + Example: R = (A,B,C), F = {B  A, C  B, C  A} Decompose to: R1 = (A,B) R2 = (B,C) Dependencies that hold: B  A C  B Is C  A lost? Is the decomposition dependency preserving? F1  F2 = {B  A, C  B} Since the closure of F1  F2 includes C  A, then the dependency is preserved and there’s no need of joining R1 and R2 to check the dependency. Example of why we need to check the closure and not the union alone…

9 Dependency Preservation
Let Fi be the set of dependencies from F + that include only attributes in Ri. A decomposition is dependency preserving, if: (F1  F2  …  Fn )+ = F + Example: R = (A,B,C), F = {B  A, C  B, C  A} Decompose to: R1 = (A,B) R2 = (B,C) Dependencies that hold: B  A C  B Is C  A lost? Is the decomposition dependency preserving? F1  F2 = {B  A, C  B} Since the closure of F1  F2 includes C  A, then the dependency is preserved and there’s no need of joining R1 and R2 to check the dependency. Example of why we need to check the closure and not the union alone…

10 Dependency Preservation
Let Fi be the set of dependencies from F + that include only attributes in Ri. A decomposition is dependency preserving, if: (F1  F2  …  Fn )+ = F + Example: R = (A,B,C), F = {B  A, C  B, C  A} Decompose to: R1 = (A,B) R2 = (B,C) Dependencies that hold: B  A C  B Is C  A lost? Is the decomposition dependency preserving? F1  F2 = {B  A, C  B} Since the closure of F1  F2 includes C  A, then the dependency is preserved and there’s no need of joining R1 and R2 to check the dependency. Example of why we need to check the closure and not the union alone…

11 Testing for Dependency Preservation
To check if a dependency    is preserved in a decomposition of R into R1, R2, …, Rn we apply the following test (with attribute closure done with respect to F) result =  while (changes to result) do for each Ri in the decomposition t = (result  Ri)+  Ri result = result  t If result contains all attributes in , then the functional dependency    is preserved. t is the closure of result under Ri. If at the end result has all of beta then it can be checked based on the current decomposition. We’re basically checking what can be determined by alpha, based on the restrictions of the decomposition (with the intersections to the Ri’s)…

12 Testing for Dependency Preservation
To check if a dependency    is preserved in a decomposition of R into R1, R2, …, Rn we apply the following test (with attribute closure done with respect to F) result =  while (changes to result) do for each Ri in the decomposition t = (result  Ri)+  Ri result = result  t If result contains all attributes in , then the functional dependency    is preserved. We apply the test on all dependencies in F to check if a decomposition is dependency preserving This procedure takes polynomial time, instead of the exponential time required to compute F+ and (F1  F2  …  Fn)+

13 Testing for Dependency Preservation
To check if a dependency    is preserved in a decomposition of R into R1, R2, …, Rn we apply the following test (with attribute closure done with respect to F) result =  while (changes to result) do for each Ri in the decomposition t = (result  Ri)+  Ri result = result  t If result contains all attributes in , then the functional dependency    is preserved. Let R1 = S,I and R2 = I,D and F = {SD  I, I  D}

14 Testing for Dependency Preservation
To check if a dependency    is preserved in a decomposition of R into R1, R2, …, Rn we apply the following test (with attribute closure done with respect to F) result =  while (changes to result) do for each Ri in the decomposition t = (result  Ri)+  Ri result = result  t If result contains all attributes in , then the functional dependency    is preserved. Let R1 = S,I and R2 = I,D and F = {SD  I, I  D} Start with  = SD, then (SD  R1)+  R1 = S and (SD  R2)+  R2 = D

15 Testing for Dependency Preservation
To check if a dependency    is preserved in a decomposition of R into R1, R2, …, Rn we apply the following test (with attribute closure done with respect to F) result =  while (changes to result) do for each Ri in the decomposition t = (result  Ri)+  Ri result = result  t If result contains all attributes in , then the functional dependency    is preserved. Let R1 = S,I and R2 = I,D and F = {SD  I, I  D} Start with  = SD, then (SD  R1)+  R1 = S and (SD  R2)+  R2 = D The I attribute will never be added when considering the dependency SD  I.

16 Boyce-Codd Normal Form
A relation schema R is in BCNF with respect to a set F of functional dependencies if for all functional dependencies in F+ of the form    where   R and   R, at least one of the following holds:    is trivial (i.e.,   )  is a superkey for R Example schema not in BCNF: instr_dept (ID, name, salary, dept_name, building, budget ) because dept_name building, budget holds on instr_dept, but dept_name is not a superkey

17 Decomposing a Schema to BCNF
Suppose we have a schema R and a non-trivial dependency    causes a violation of BCNF. We decompose R into: ( U  ) ( R - (  -  ) ) In our example, dept_name  building, budget  = dept_name  = building, budget and inst_dept is replaced by ( U  ) = ( dept_name, building, budget ) ( R - (  -  ) ) = ( ID, name, salary, dept_name )

18 Testing for BCNF To check if a non-trivial dependency   causes a violation of BCNF 1. compute + (the attribute closure of ), and 2. verify that it includes all attributes of R, that is, it is a superkey of R.

19 Testing for BCNF To check if a non-trivial dependency   causes a violation of BCNF 1. compute + (the attribute closure of ), and 2. verify that it includes all attributes of R, that is, it is a superkey of R. Simplified test: To check if a relation schema R is in BCNF, it suffices to check only the dependencies in the given set F for violation of BCNF, rather than checking all dependencies in F+. If none of the dependencies in F causes a violation of BCNF, then none of the dependencies in F+ will cause a violation of BCNF either.

20 Testing for BCNF To check if a non-trivial dependency   causes a violation of BCNF 1. compute + (the attribute closure of ), and 2. verify that it includes all attributes of R, that is, it is a superkey of R. Simplified test: To check if a relation schema R is in BCNF, it suffices to check only the dependencies in the given set F for violation of BCNF, rather than checking all dependencies in F+. If none of the dependencies in F causes a violation of BCNF, then none of the dependencies in F+ will cause a violation of BCNF either. However, simplified test using only F is incorrect when testing a relation in a decomposition of R

21 Testing for BCNF To check if a non-trivial dependency   causes a violation of BCNF 1. compute + (the attribute closure of ), and 2. verify that it includes all attributes of R, that is, it is a superkey of R. Simplified test: To check if a relation schema R is in BCNF, it suffices to check only the dependencies in the given set F for violation of BCNF, rather than checking all dependencies in F+. If none of the dependencies in F causes a violation of BCNF, then none of the dependencies in F+ will cause a violation of BCNF either. However, simplified test using only F is incorrect when testing a relation in a decomposition of R Consider R = (A, B, C, D, E), with F = { A  B, BC  D} Decompose R into R1 = (A,B) and R2 = (A,C,D, E) Neither of the dependencies in F contain only attributes from (A,C,D,E) so we might be mislead into thinking R2 satisfies BCNF.

22 Testing for BCNF To check if a non-trivial dependency   causes a violation of BCNF 1. compute + (the attribute closure of ), and 2. verify that it includes all attributes of R, that is, it is a superkey of R. Simplified test: To check if a relation schema R is in BCNF, it suffices to check only the dependencies in the given set F for violation of BCNF, rather than checking all dependencies in F+. If none of the dependencies in F causes a violation of BCNF, then none of the dependencies in F+ will cause a violation of BCNF either. However, simplified test using only F is incorrect when testing a relation in a decomposition of R Consider R = (A, B, C, D, E), with F = { A  B, BC  D} Decompose R into R1 = (A,B) and R2 = (A,C,D, E) Neither of the dependencies in F contain only attributes from (A,C,D,E) so we might be mislead into thinking R2 satisfies BCNF. In fact, dependency AC  D in F+ shows R2 is not in BCNF.

23 Testing Decomposition for BCNF
To check if a relation Ri in a decomposition of R is in BCNF, Either test Ri for BCNF with respect to the restriction of F+ to Ri (that is, all FDs in F+ that contain only attributes from Ri)

24 Testing Decomposition for BCNF
To check if a relation Ri in a decomposition of R is in BCNF, Either test Ri for BCNF with respect to the restriction of F+ to Ri (that is, all FDs in F+ that contain only attributes from Ri) or use the original set of dependencies F that hold on R, but with the following test: for every set of attributes   Ri, check that + (the attribute closure of ) either includes no attribute of Ri- , or includes all attributes of Ri.

25 Testing Decomposition for BCNF
To check if a relation Ri in a decomposition of R is in BCNF, Either test Ri for BCNF with respect to the restriction of F+ to Ri (that is, all FDs in F+ that contain only attributes from Ri) or use the original set of dependencies F that hold on R, but with the following test: for every set of attributes   Ri, check that + (the attribute closure of ) either includes no attribute of Ri- , or includes all attributes of Ri. If the condition is violated by some    in F, the dependency   (+ - )  Ri can be shown to hold on Ri, and Ri violates BCNF. We use above dependency to decompose Ri

26 BCNF Decomposition Algorithm
result := {R }; done := false; compute F +; while (not done) do if (there is a schema Ri in result that is not in BCNF) then begin let    be a nontrivial functional dependency that holds on Ri such that   Ri is not in F +, and    = ; result := (result – Ri )  (Ri – )  (,  ); end else done := true; Note: each Ri is in BCNF, and decomposition is lossless-join. Exponential in the size of the initial schema Checking if a relation in the decomposition satisfies BCNF can take exponential time in itself The book cites a poly time algorithm but does not go into details (it overnormalizes)

27 Example of BCNF Decomposition
class (course_id, title, dept_name, credits, sec_id, semester, year, building, room_number, capacity, time_slot_id) Functional dependencies: course_id→ title, dept_name, credits building, room_number→capacity course_id, sec_id, semester, year→building, room_number, time_slot_id

28 Example of BCNF Decomposition
class (course_id, title, dept_name, credits, sec_id, semester, year, building, room_number, capacity, time_slot_id) Functional dependencies: course_id→ title, dept_name, credits building, room_number→capacity course_id, sec_id, semester, year→building, room_number, time_slot_id A candidate key?

29 Example of BCNF Decomposition
class (course_id, title, dept_name, credits, sec_id, semester, year, building, room_number, capacity, time_slot_id) Functional dependencies: course_id→ title, dept_name, credits building, room_number→capacity course_id, sec_id, semester, year→building, room_number, time_slot_id A candidate key {course_id, sec_id, semester, year}.

30 Example of BCNF Decomposition
class (course_id, title, dept_name, credits, sec_id, semester, year, building, room_number, capacity, time_slot_id) Functional dependencies: course_id→ title, dept_name, credits building, room_number→capacity course_id, sec_id, semester, year→building, room_number, time_slot_id A candidate key {course_id, sec_id, semester, year}. BCNF Decomposition: course_id→ title, dept_name, credits holds but course_id is not a superkey.

31 Example of BCNF Decomposition
class (course_id, title, dept_name, credits, sec_id, semester, year, building, room_number, capacity, time_slot_id) Functional dependencies: course_id→ title, dept_name, credits building, room_number→capacity course_id, sec_id, semester, year→building, room_number, time_slot_id A candidate key {course_id, sec_id, semester, year}. BCNF Decomposition: course_id→ title, dept_name, credits holds but course_id is not a superkey. We replace class by: course(course_id, title, dept_name, credits) class-1 (course_id, sec_id, semester, year, building, room_number, capacity, time_slot_id)

32 Example of BCNF Decomposition
class (course_id, title, dept_name, credits, sec_id, semester, year, building, room_number, capacity, time_slot_id) Functional dependencies: course_id→ title, dept_name, credits building, room_number→capacity course_id, sec_id, semester, year→building, room_number, time_slot_id A candidate key {course_id, sec_id, semester, year}. BCNF Decomposition: course_id→ title, dept_name, credits holds but course_id is not a superkey. We replace class by: course(course_id, title, dept_name, credits) class-1 (course_id, sec_id, semester, year, building, room_number, capacity, time_slot_id) Is course in BCNF? Course is, any attributes in course will not appear on left side of a dependency unless they contain themselves.

33 Example of BCNF Decomposition
class (course_id, title, dept_name, credits, sec_id, semester, year, building, room_number, capacity, time_slot_id) Functional dependencies: course_id→ title, dept_name, credits building, room_number→capacity course_id, sec_id, semester, year→building, room_number, time_slot_id A candidate key {course_id, sec_id, semester, year}. BCNF Decomposition: course_id→ title, dept_name, credits holds but course_id is not a superkey. We replace class by: course(course_id, title, dept_name, credits) class-1 (course_id, sec_id, semester, year, building, room_number, capacity, time_slot_id) Is class-1 in BCNF? Class-1 is not in BCNF by violation of building,room_number is not a key of class-1

34 Example of BCNF Decomposition
class (course_id, title, dept_name, credits, sec_id, semester, year, building, room_number, capacity, time_slot_id) Functional dependencies: course_id→ title, dept_name, credits building, room_number→capacity course_id, sec_id, semester, year→building, room_number, time_slot_id A candidate key {course_id, sec_id, semester, year}. BCNF Decomposition: course_id→ title, dept_name, credits holds but course_id is not a superkey. We replace class by: course(course_id, title, dept_name, credits) class-1 (course_id, sec_id, semester, year, building, room_number, capacity, time_slot_id) Was this decomposition loss-less join? Yes, course inters. Class-1 = course_id and course_id is a key of course.

35 BCNF Decomposition (Cont.)
class-1 (course_id, sec_id, semester, year, building, room_number, capacity, time_slot_id) course (course_id, title, dept_name, credits) Functional dependencies: course_id→ title, dept_name, credits building, room_number→capacity course_id, sec_id, semester, year→building, room_number, time_slot_id

36 BCNF Decomposition (Cont.)
class-1 (course_id, sec_id, semester, year, building, room_number, capacity, time_slot_id) course (course_id, title, dept_name, credits) Functional dependencies: course_id→ title, dept_name, credits building, room_number→capacity course_id, sec_id, semester, year→building, room_number, time_slot_id building, room_number→capacity holds on class-1 but {building, room_number} is not a superkey for class-1.

37 BCNF Decomposition (Cont.)
class-1 (course_id, sec_id, semester, year, building, room_number, capacity, time_slot_id) course (course_id, title, dept_name, credits) Functional dependencies: course_id→ title, dept_name, credits building, room_number→capacity course_id, sec_id, semester, year→building, room_number, time_slot_id building, room_number→capacity holds on class-1 but {building, room_number} is not a superkey for class-1. We replace class-1 by: classroom (building, room_number, capacity) section (course_id, sec_id, semester, year, building, room_number, time_slot_id)

38 BCNF Decomposition (Cont.)
class-1 (course_id, sec_id, semester, year, building, room_number, capacity, time_slot_id) course (course_id, title, dept_name, credits) Functional dependencies: course_id→ title, dept_name, credits building, room_number→capacity course_id, sec_id, semester, year→building, room_number, time_slot_id building, room_number→capacity holds on class-1 but {building, room_number} is not a superkey for class-1. We replace class-1 by: classroom (building, room_number, capacity) section (course_id, sec_id, semester, year, building, room_number, time_slot_id) Are classroom and section in BCNF? Was the decomposition loss-less join?

39 BCNF and Dependency Preservation
It is not always possible to get a BCNF decomposition that is dependency preserving R = (J, K, L ) F = {JK  L L  K } Two candidate keys = JK and JL R is not in BCNF Any decomposition of R will fail to preserve JK  L This implies that testing for JK  L requires a join

40 Third Normal Form (3NF): Motivation
There are some situations where BCNF is not dependency preserving, and efficient checking for FD violation on updates is important Solution: define a weaker normal form, called Third Normal Form (3NF) Allows some redundancy (with resultant problems; we will see examples later) But functional dependencies can be checked on individual relations without computing a join. There is always a lossless-join, dependency-preserving decomposition into 3NF.

41 Third Normal Form A relation schema R is in third normal form (3NF) if for all:    in F+ at least one of the following holds:    is trivial (i.e.,   )  is a superkey for R Each attribute A in  –  is contained in a candidate key for R. (NOTE: each attribute may be in a different candidate key) If a relation is in BCNF it is in 3NF (since in BCNF one of the first two conditions above must hold). Third condition is a minimal relaxation of BCNF to ensure dependency preservation.

42 3NF Example Relation dept_advisor:
dept_advisor (s_ID, i_ID, dept_name) F = {s_ID, dept_name  i_ID, i_ID  dept_name} Two candidate keys: s_ID, dept_name, and i_ID, s_ID R is in 3NF s_ID, dept_name  i_ID s_ID s_ID, dept_name is a superkey i_ID  dept_name dept_name is contained in a candidate key

43 Redundancy in 3NF There is some redundancy in this schema
Example of problems due to redundancy in 3NF R = (J, K, L) F = {JK  L, L  K } J L K j1 j2 j3 null l1 l2 k1 k2 NULL value example would be an instructor that is not advising any students. repetition of information (e.g., the relationship l1, k1) (i_ID, dept_name) need to use null values (e.g., to represent the relationship l2, k2 where there is no corresponding value for J). (i_ID, dept_nameI) if there is no separate relation mapping instructors to departments

44 3NF Decomposition Algorithm
Let Fc be a canonical cover for F; i := 0; for each functional dependency    in Fc do if none of the schemas Rj, 1  j  i contains   then begin i := i + 1; Ri :=   end if none of the schemas Rj, 1  j  i contains a candidate key for R then begin i := i + 1; Ri := any candidate key for R; end /* Optionally, remove redundant relations */ repeat if any schema Rj is contained in another schema Rk then /* delete Rj */ Rj = R;; i=i-1; return (R1, R2, ..., Ri) First loop, creates

45 3NF Decomposition Algorithm
Above algorithm ensures: each relation schema Ri is in 3NF decomposition is dependency preserving and lossless-join Proof of in the textbook.

46 Comparison of BCNF and 3NF
It is always possible to decompose a relation into a set of relations that are in 3NF such that: the decomposition is lossless the dependencies are preserved It is always possible to decompose a relation into a set of relations that are in BCNF such that: it may not be possible to preserve dependencies.

47 Design Goals Goal for a relational database design is: BCNF.
Lossless join. Dependency preservation. If we cannot achieve this, we accept one of Lack of dependency preservation Redundancy due to use of 3NF Interestingly, SQL does not provide a direct way of specifying functional dependencies other than superkeys. Can specify FDs using assertions, but they are expensive to test, (and currently not supported by any of the widely used databases!) Even if we had a dependency preserving decomposition, using SQL we would not be able to efficiently test a functional dependency whose left hand side is not a key.


Download ppt "CS 480: Database Systems Lecture 22 March 6, 2013."

Similar presentations


Ads by Google