©Silberschatz, Korth and Sudarshan
7.1
Database System Concepts
Lecture No. 21
Boyce-Codd Normal Form
    is trivial (i.e.,   )
  is a superkey for R
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:
Example
 R = (A, B, C)
F = {A  B
B  C}
Key = {A}
 R is not in BCNF
 Decomposition R1 = (A, B), R2 = (B, C)
 R1 and R2 in BCNF
 Lossless-join decomposition
 Dependency preserving
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 with a given set of
functional dependencies F 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+.
 We can show that 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, using only F is incorrect when testing a relation in a
decomposition of R
 E.g. Consider R (A, B, C, D), with F = { A B, B C}
 Decompose R into R1(A,B) and R2(A,C,D)
 Neither of the dependencies in F contain only attributes from (A,C,D) so
we might be mislead into thinking R2 satisfies BCNF.
 In fact, dependency A  C in F+ shows R2 is not in BCNF.
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.
Example of BCNF Decomposition
 R = (branch-name, branch-city, assets,
customer-name, loan-number, amount)
F = {branch-name  assets branch-city
loan-number  amount branch-name}
Key = {loan-number, customer-name}
 Decomposition
 R1 = (branch-name, branch-city, assets)
 R2 = (branch-name, customer-name, loan-number, amount)
 R3 = (branch-name, loan-number, amount)
 R4 = (customer-name, loan-number)
 Final decomposition
R1, R3, R4
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
BCNF and Dependency Preservation
 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
It is not always possible to get a BCNF decomposition that is
dependency preserving
©Silberschatz, Korth and Sudarshan
7.9
Database System Concepts
Lecture No. 22
Third Normal Form: 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.
 Allows some redundancy (with resultant problems; we will see
examples later)
 But FDs can be checked on individual relations without computing a
join.
 There is always a lossless-join, dependency-preserving decomposition
into 3NF.
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 (will see why later).
3NF (Cont.)
 Example
 R = (J, K, L)
F = {JK  L, L  K}
 Two candidate keys: JK and JL
 R is in 3NF
JK  L JK is a superkey
L  K K is contained in a candidate key
 BCNF decomposition has (JL) and (LK)
 Testing for JK  L requires a join
 There is some redundancy in this schema
 Equivalent to example in book:
Banker-schema = (branch-name, customer-name, banker-name)
banker-name  branch name
branch name customer-name  banker-name
Testing for 3NF
 Optimization: Need to check only FDs in F, need not check all
FDs in F+.
 Use attribute closure to check, for each dependency   , if 
is a superkey.
 If  is not a superkey, we have to verify if each attribute in  is
contained in a candidate key of R
 this test is rather more expensive, since it involve finding candidate
keys
 testing for 3NF has been shown to be NP-hard
 Interestingly, decomposition into third normal form (described
shortly) can be done in polynomial time
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
return (R1, R2, ..., Ri)
3NF Decomposition Algorithm (Cont.)
 Above algorithm ensures:
 each relation schema Ri is in 3NF
 decomposition is dependency preserving and lossless-join
 Proof of correctness is at end of this file (click here)
Example
 Relation schema:
Banker-info-schema = (branch-name, customer-name,
banker-name, office-number)
 The functional dependencies for this relation schema are:
banker-name  branch-name office-number
customer-name branch-name  banker-name
 The key is:
{customer-name, branch-name}
Applying 3NF to Banker-info-schema
 The for loop in the algorithm causes us to include the
following schemas in our decomposition:
Banker-office-schema = (banker-name, branch-name,
office-number)
Banker-schema = (customer-name, branch-name,
banker-name)
 Since Banker-schema contains a candidate key for
Banker-info-schema, we are done with the decomposition
process.
Comparison of BCNF and 3NF
 It is always possible to decompose a relation into relations in
3NF and
 the decomposition is lossless
 the dependencies are preserved
 It is always possible to decompose a relation into relations in
BCNF and
 the decomposition is lossless
 it may not be possible to preserve dependencies.
Comparison of BCNF and 3NF (Cont.)
J
j1
j2
j3
null
L
l1
l1
l1
l2
K
k1
k1
k1
k2
A schema that is in 3NF but not in BCNF has the problems of
 repetition of information (e.g., the relationship l1, k1)
 need to use null values (e.g., to represent the relationship
l2, k2 where there is no corresponding value for J).
 Example of problems due to redundancy in 3NF
 R = (J, K, L)
F = {JK  L, L  K}
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
 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.
Testing for FDs Across Relations
 If decomposition is not dependency preserving, we can have an
extra materialized view for each dependency   in Fc that is
not preserved in the decomposition
 The materialized view is defined as a projection on   of the join
of the relations in the decomposition
 Many newer database systems support materialized views and
database system maintains the view when the relations are
updated.
 No extra coding effort for programmer.
 The FD becomes a candidate key on the materialized view.
 Space overhead: for storing the materialized view
 Time overhead: Need to keep materialized view up to date when
relations are updated
Multivalued Dependencies
 There are database schemas in BCNF that do not seem to be
sufficiently normalized
 Consider a database
classes(course, teacher, book)
such that (c,t,b)  classes means that t is qualified to teach c,
and b is a required textbook for c
 The database is supposed to list for each course the set of
teachers any one of which can be the course’s instructor, and the
set of books, all of which are required for the course (no matter
who teaches it).
 Since there are non-trivial dependencies, (course, teacher, book)
is the only key, and therefore the relation is in BCNF
 Insertion anomalies – i.e., if Sara is a new teacher that can teach
database, two tuples need to be inserted
(database, Sara, DB Concepts)
(database, Sara, Ullman)
course teacher book
database
database
database
database
database
database
operating systems
operating systems
operating systems
operating systems
Avi
Avi
Hank
Hank
Sudarshan
Sudarshan
Avi
Avi
Jim
Jim
DB Concepts
Ullman
DB Concepts
Ullman
DB Concepts
Ullman
OS Concepts
Shaw
OS Concepts
Shaw
classes
 Therefore, it is better to decompose classes into:
course teacher
database
database
database
operating systems
operating systems
Avi
Hank
Sudarshan
Avi
Jim
teaches
course book
database
database
operating systems
operating systems
DB Concepts
Ullman
OS Concepts
Shaw
text
We shall see that these two relations are in Fourth Normal
Form (4NF)
Multivalued Dependencies (MVDs)
 Let R be a relation schema and let   R and   R.
The multivalued dependency
  
holds on R if in any legal relation r(R), for all pairs for
tuples t1 and t2 in r such that t1[] = t2 [], there exist
tuples t3 and t4 in r such that:
t1[] = t2 [] = t3 [] t4 []
t3[] = t1 []
t3[R – ] = t2[R – ]
t4 ] = t2[]
t4[R – ] = t1[R – ]
MVD (Cont.)
 Tabular representation of   
Example
 Let R be a relation schema with a set of attributes that are
partitioned into 3 nonempty subsets.
Y, Z, W
 We say that Y  Z (Y multidetermines Z)
if and only if for all possible relations r(R)
< y1, z1, w1 >  r and < y2, z2, w2 >  r
then
< y1, z1, w2 >  r and < y1, z2, w1 >  r
 Note that since the behavior of Z and W are identical it follows
that Y  Z if Y  W
Example (Cont.)
 In our example:
course  teacher
course  book
 The above formal definition is supposed to formalize the
notion that given a particular value of Y (course) it has
associated with it a set of values of Z (teacher) and a set
of values of W (book), and these two sets are in some
sense independent of each other.
 Note:
 If Y  Z then Y  Z
 Indeed we have (in above notation) Z1 = Z2
The claim follows.
Use of Multivalued Dependencies
 We use multivalued dependencies in two ways:
1. To test relations to determine whether they are legal under a
given set of functional and multivalued dependencies
2. To specify constraints on the set of legal relations. We shall
thus concern ourselves only with relations that satisfy a given
set of functional and multivalued dependencies.
 If a relation r fails to satisfy a given multivalued
dependency, we can construct a relations r that does
satisfy the multivalued dependency by adding tuples to r.
An Illegal bc Relation
Decomposition of loan-info
Relation of Exercise 7.4

Lecture No. 21-22.ppt

  • 1.
    ©Silberschatz, Korth andSudarshan 7.1 Database System Concepts Lecture No. 21
  • 2.
    Boyce-Codd Normal Form    is trivial (i.e.,   )   is a superkey for R 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:
  • 3.
    Example  R =(A, B, C) F = {A  B B  C} Key = {A}  R is not in BCNF  Decomposition R1 = (A, B), R2 = (B, C)  R1 and R2 in BCNF  Lossless-join decomposition  Dependency preserving
  • 4.
    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 with a given set of functional dependencies F 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+.  We can show that 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, using only F is incorrect when testing a relation in a decomposition of R  E.g. Consider R (A, B, C, D), with F = { A B, B C}  Decompose R into R1(A,B) and R2(A,C,D)  Neither of the dependencies in F contain only attributes from (A,C,D) so we might be mislead into thinking R2 satisfies BCNF.  In fact, dependency A  C in F+ shows R2 is not in BCNF.
  • 5.
    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.
  • 6.
    Example of BCNFDecomposition  R = (branch-name, branch-city, assets, customer-name, loan-number, amount) F = {branch-name  assets branch-city loan-number  amount branch-name} Key = {loan-number, customer-name}  Decomposition  R1 = (branch-name, branch-city, assets)  R2 = (branch-name, customer-name, loan-number, amount)  R3 = (branch-name, loan-number, amount)  R4 = (customer-name, loan-number)  Final decomposition R1, R3, R4
  • 7.
    Testing Decomposition forBCNF  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
  • 8.
    BCNF and DependencyPreservation  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 It is not always possible to get a BCNF decomposition that is dependency preserving
  • 9.
    ©Silberschatz, Korth andSudarshan 7.9 Database System Concepts Lecture No. 22
  • 10.
    Third Normal Form: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.  Allows some redundancy (with resultant problems; we will see examples later)  But FDs can be checked on individual relations without computing a join.  There is always a lossless-join, dependency-preserving decomposition into 3NF.
  • 11.
    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 (will see why later).
  • 12.
    3NF (Cont.)  Example R = (J, K, L) F = {JK  L, L  K}  Two candidate keys: JK and JL  R is in 3NF JK  L JK is a superkey L  K K is contained in a candidate key  BCNF decomposition has (JL) and (LK)  Testing for JK  L requires a join  There is some redundancy in this schema  Equivalent to example in book: Banker-schema = (branch-name, customer-name, banker-name) banker-name  branch name branch name customer-name  banker-name
  • 13.
    Testing for 3NF Optimization: Need to check only FDs in F, need not check all FDs in F+.  Use attribute closure to check, for each dependency   , if  is a superkey.  If  is not a superkey, we have to verify if each attribute in  is contained in a candidate key of R  this test is rather more expensive, since it involve finding candidate keys  testing for 3NF has been shown to be NP-hard  Interestingly, decomposition into third normal form (described shortly) can be done in polynomial time
  • 14.
    3NF Decomposition Algorithm LetFc 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 return (R1, R2, ..., Ri)
  • 15.
    3NF Decomposition Algorithm(Cont.)  Above algorithm ensures:  each relation schema Ri is in 3NF  decomposition is dependency preserving and lossless-join  Proof of correctness is at end of this file (click here)
  • 16.
    Example  Relation schema: Banker-info-schema= (branch-name, customer-name, banker-name, office-number)  The functional dependencies for this relation schema are: banker-name  branch-name office-number customer-name branch-name  banker-name  The key is: {customer-name, branch-name}
  • 17.
    Applying 3NF toBanker-info-schema  The for loop in the algorithm causes us to include the following schemas in our decomposition: Banker-office-schema = (banker-name, branch-name, office-number) Banker-schema = (customer-name, branch-name, banker-name)  Since Banker-schema contains a candidate key for Banker-info-schema, we are done with the decomposition process.
  • 18.
    Comparison of BCNFand 3NF  It is always possible to decompose a relation into relations in 3NF and  the decomposition is lossless  the dependencies are preserved  It is always possible to decompose a relation into relations in BCNF and  the decomposition is lossless  it may not be possible to preserve dependencies.
  • 19.
    Comparison of BCNFand 3NF (Cont.) J j1 j2 j3 null L l1 l1 l1 l2 K k1 k1 k1 k2 A schema that is in 3NF but not in BCNF has the problems of  repetition of information (e.g., the relationship l1, k1)  need to use null values (e.g., to represent the relationship l2, k2 where there is no corresponding value for J).  Example of problems due to redundancy in 3NF  R = (J, K, L) F = {JK  L, L  K}
  • 20.
    Design Goals  Goalfor 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  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.
  • 21.
    Testing for FDsAcross Relations  If decomposition is not dependency preserving, we can have an extra materialized view for each dependency   in Fc that is not preserved in the decomposition  The materialized view is defined as a projection on   of the join of the relations in the decomposition  Many newer database systems support materialized views and database system maintains the view when the relations are updated.  No extra coding effort for programmer.  The FD becomes a candidate key on the materialized view.  Space overhead: for storing the materialized view  Time overhead: Need to keep materialized view up to date when relations are updated
  • 22.
    Multivalued Dependencies  Thereare database schemas in BCNF that do not seem to be sufficiently normalized  Consider a database classes(course, teacher, book) such that (c,t,b)  classes means that t is qualified to teach c, and b is a required textbook for c  The database is supposed to list for each course the set of teachers any one of which can be the course’s instructor, and the set of books, all of which are required for the course (no matter who teaches it).
  • 23.
     Since thereare non-trivial dependencies, (course, teacher, book) is the only key, and therefore the relation is in BCNF  Insertion anomalies – i.e., if Sara is a new teacher that can teach database, two tuples need to be inserted (database, Sara, DB Concepts) (database, Sara, Ullman) course teacher book database database database database database database operating systems operating systems operating systems operating systems Avi Avi Hank Hank Sudarshan Sudarshan Avi Avi Jim Jim DB Concepts Ullman DB Concepts Ullman DB Concepts Ullman OS Concepts Shaw OS Concepts Shaw classes
  • 24.
     Therefore, itis better to decompose classes into: course teacher database database database operating systems operating systems Avi Hank Sudarshan Avi Jim teaches course book database database operating systems operating systems DB Concepts Ullman OS Concepts Shaw text We shall see that these two relations are in Fourth Normal Form (4NF)
  • 25.
    Multivalued Dependencies (MVDs) Let R be a relation schema and let   R and   R. The multivalued dependency    holds on R if in any legal relation r(R), for all pairs for tuples t1 and t2 in r such that t1[] = t2 [], there exist tuples t3 and t4 in r such that: t1[] = t2 [] = t3 [] t4 [] t3[] = t1 [] t3[R – ] = t2[R – ] t4 ] = t2[] t4[R – ] = t1[R – ]
  • 26.
    MVD (Cont.)  Tabularrepresentation of   
  • 27.
    Example  Let Rbe a relation schema with a set of attributes that are partitioned into 3 nonempty subsets. Y, Z, W  We say that Y  Z (Y multidetermines Z) if and only if for all possible relations r(R) < y1, z1, w1 >  r and < y2, z2, w2 >  r then < y1, z1, w2 >  r and < y1, z2, w1 >  r  Note that since the behavior of Z and W are identical it follows that Y  Z if Y  W
  • 28.
    Example (Cont.)  Inour example: course  teacher course  book  The above formal definition is supposed to formalize the notion that given a particular value of Y (course) it has associated with it a set of values of Z (teacher) and a set of values of W (book), and these two sets are in some sense independent of each other.  Note:  If Y  Z then Y  Z  Indeed we have (in above notation) Z1 = Z2 The claim follows.
  • 29.
    Use of MultivaluedDependencies  We use multivalued dependencies in two ways: 1. To test relations to determine whether they are legal under a given set of functional and multivalued dependencies 2. To specify constraints on the set of legal relations. We shall thus concern ourselves only with relations that satisfy a given set of functional and multivalued dependencies.  If a relation r fails to satisfy a given multivalued dependency, we can construct a relations r that does satisfy the multivalued dependency by adding tuples to r.
  • 30.
    An Illegal bcRelation
  • 31.
  • 32.