Tuesday, December 4, 2012

Database Management System Chapter-7

 

 

 

Chapter 7:  Relational Database Design

 

n First Normal Form

n Pitfalls in Relational Database Design

n Functional Dependencies

n Decomposition

n Boyce-Codd Normal Form

n Third Normal Form

n Multivalued Dependencies and Fourth Normal Form

n Overall Database Design Process

 

First Normal Form

 

n Domain is atomic if its elements are considered to be indivisible units

H Examples of non-atomic domains:

4 Set of names,  composite attributes

4 Identification numbers like CS101  that can be broken up into parts

n A relational schema R is in first normal form if the domains of all attributes of R are atomic

n Non-atomic values complicate storage and encourage redundant (repeated) storage of data

H E.g.  Set of accounts stored with each customer, and set of owners stored with each account

H We assume all relations are in first normal form (revisit this in Chapter 9 on Object Relational Databases)

 

n Atomicity is actually a property of how the elements of the domain are used.

H E.g. Strings would normally be considered indivisible

H Suppose that students are given roll numbers which are strings        of the form CS0012 or EE1127

H If the first two characters are extracted to find the department, the domain of roll numbers is not atomic.

H Doing so is a bad idea: leads to encoding of information in application program rather than in the database.

 

 

 

 

Pitfalls in Relational Database Design

 

n Relational database design requires that we find a "good" collection of relation schemas.  A bad design may lead to

H Repetition of Information.

H Inability to represent certain information.

n Design Goals:

H Avoid redundant data

H Ensure that relationships among attributes are represented

H Facilitate the checking of updates for violation of database integrity constraints.

Example

 

n  Consider the relation schema:
         Lending-schema = (branch-name, branch-city, assets,
                      customer-name, loan-number, amount)

 

 

 

 

 

 

 

 

 

 

 

 

 


n  Redundancy:

H Data for branch-name, branch-city, assets are repeated for each loan that a branch makes

H Wastes space

H Complicates updating, introducing possibility of inconsistency of assets value

n  Null values

H Cannot store information about a branch if no loans exist

H Can use null values, but they are difficult to handle.

 

 

Decomposition

 

n Decompose the relation schema Lending-schema into:

Branch-schema = (branch-name, branch-city,assets)

Loan-info-schema = (customer-name, loan-number,
                                                   branch-name, amount)

n All attributes of an original schema (R) must appear in the decomposition (R1, R2):

          R = R1 È R2

n Lossless-join decomposition.
For all possible relations r on schema R

                r = ÕR1 (r)    ÕR2 (r)

 

Example of Non Lossless-Join Decomposition

 

n Decomposition of R = (A, B)
      R2 = (A)      R
2 = (B)

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


Goal — Devise a Theory for the Following

 

n Decide whether a particular relation R is in "good" form.

n In the case that a relation R is not in "good" form, decompose it into a set of relations {R1, R2, ..., Rn} such that

H each relation is in good form

H the decomposition is a lossless-join decomposition

n Our theory is based on:

H functional dependencies

H multivalued dependencies

 

Functional Dependencies

 

n Constraints on the set of legal relations.

n Require that the value for a certain set of attributes determines uniquely the value for another set of attributes.

n A functional dependency is a generalization of the notion of a key.

 

n  Let R be a relation schema

          a Í R  and  b Í R

n  The functional dependency

          a ® b
holds on R if and only if for any legal relations r(R), whenever any two tuples t1 and t2 of r agree on the attributes a, they also agree on the attributes b.  That is,

           t1[a] = t2 [a]   Þ   t1[b ]  = t2 [b ]

n  Example:  Consider r(A,B) with the following instance of r.

 

 

 

n  On this instance, A ® B does NOT hold, but  B ® A does hold.

 

n K is a superkey for relation schema R if and only if K ® R

n K is a candidate key for R if and only if

H K ® R, and

H for no a Ì K, a ® R

n Functional dependencies allow us to express constraints that cannot be expressed using superkeys.  Consider the schema:

          Loan-info-schema = (customer-name, loan-number,
                                    branch-name, amount).

    We expect this set of functional dependencies to hold:

                   loan-number ® amount
                loan-number
® branch-name

    but would not expect the following to hold:

                   loan-number ® customer-name

 

Use of Functional Dependencies

 

n We use functional dependencies to:

H test relations to see if they are legal under a given set of functional dependencies.

4  If a relation r is legal under a set F of functional dependencies, we say that r satisfies F.

H specify constraints on the set of legal relations

4 We say that F holds on R if all legal relations on R satisfy the set of functional dependencies F.

n Note:  A specific instance of a relation schema may satisfy a functional dependency even if the functional dependency does not hold on all legal instances. 

H For example, a specific instance of Loan-schema may, by chance, satisfy
               loan-number
® customer-name.

 

n A functional dependency is trivial if it is satisfied by all instances of a relation

H E.g.

4  customer-name, loan-number ® customer-name

4  customer-name ® customer-name

H In general, a ® b is trivial if b Í a
 

 

Closure of a Set of Functional Dependencies

 

n Given a set F set of functional dependencies, there are certain other functional dependencies that are logically implied by F.

H E.g.  If  A ® B and  B ® C,  then we can infer that A ® C

n The set of all functional dependencies logically implied by F is the closure of F.

n We denote the closure of F by F+.

n We can find all of F+ by applying Armstrong's Axioms:

H if b Í a, then a ® b                      (reflexivity)

H if a ® b, then g a ®  g b               (augmentation)

H if a ® b, and b ® g, then a ®  g   (transitivity)

n These rules are

H sound (generate only functional dependencies that actually hold) and

H complete (generate all functional dependencies that hold).

 

Example

n R = (A, B, C, G, H, I)
F =
A ® B
         A
® C
      CG
® H
      CG
® I
         B
® H}

n some members of F+

H A ® H       

4 by transitivity from A ® B and B ® H

H AG ® I      

4 by augmenting A ® C with G, to get AG ® CG
                   
and then transitivity with CG
® I

H CG ® HI    

4 from CG ® H and CG ® I :   "union rule" can be inferred from

  definition of functional dependencies, or
  Augmentation of CG ® I to infer CG ® CGI, augmentation of
CG
® H to infer CGI ® HI, and then transitivity

 

Procedure for Computing F+

 

n To compute the closure of a set of functional dependencies F:

     F+ = F
repeat
      for each functional dependency f in F
+
         
       apply reflexivity and augmentation rules on f
            
add the resulting functional dependencies to F
+
          for each pair of functional dependencies f1and f2 in F+
         
       if f1 and f2 can be combined using transitivity
                then add the resulting functional dependency to F
+
until F+ does not change any further

 

NOTE:  We will see an alternative procedure for this task later

 

Closure of Functional Dependencies (Cont.)

 

n We can further simplify manual computation of F+ by using the following additional rules.

H If a ® b holds and a ® g holds,  then a ® b g holds (union)

H If a ® b g holds, then a ® b  holds and a ® g holds (decomposition)

H If a ® b  holds and g b ® d holds, then a g ® d holds (pseudotransitivity)

The above rules can be inferred from Armstrong's axioms.

 

Closure of Attribute Sets

 

n Given a set of attributes  define the closure of  under F (denoted by +) as the set of attributes that are functionally determined by  under F:
                
® b is in F+  ó  b Í+

n Algorithm to compute +, the closure of  under F
      result
:= ;
      while (changes to result) do
                for each
b ® g in F do
                         begin
                                   if
b Í result then  result := result È g
                         end

 

Example of Attribute Set Closure

 

n  R = (A, B, C, G, H, I)

n  F = {A ® B
      A
® C
      CG
® H
      CG
® I
      B
® H}

n  (AG)+

1.  result = AG

2.  result = ABCG      (A ® C and A ® B)

3.  result = ABCGH   (CG ® H and CG Í AGBC)

4.  result = ABCGHI  (CG ® I and CG Í AGBCH)

n  Is AG a candidate key? 

H  Is AG a super key?

H  Does AG ® R? == Is (AG)+ Ê R

H  Is any subset of AG a superkey?

H  Does A ® R? == Is (A)+ Ê R

H  Does G ® R? == Is (G)+ Ê R

 

Uses of Attribute Closure

 

There are several uses of the attribute closure algorithm:

n Testing for superkey:

H To test if a is a superkey, we compute a+, and check if a+ contains all attributes of R.

n Testing functional dependencies

H To check if a functional dependency a ® b holds (or, in other words, is in F+), just check if b Í a+.

H That is, we compute a+ by using attribute closure, and then check if it contains b.

H Is a simple and cheap test, and very useful

n Computing closure of F

H For each g Í R, we find the closure g+, and for each S Í g+, we output a functional dependency g ® S.

 

Canonical Cover

 

n Sets of functional dependencies may have redundant dependencies that can be inferred from the others

H Eg:  A ® C is redundant in:   {A ® B,   B ® C,   A ® C}

H Parts of a functional dependency may be redundant

4 E.g. on RHS:    {A ® B,   B ® C,   A ® CD}  can be simplified to
                         {A
® B,   B ® C,   A ® D}

4 E.g. on LHS:    {A ® B,   B ® C,   AC ® D}  can be simplified to
                         {A
® B,   B ® C,   A ® D}

n Intuitively, a canonical cover of F is a "minimal" set of functional dependencies equivalent to F, having no redundant dependencies or redundant parts of dependencies

 

Extraneous Attributes

 

n Consider a set F of functional dependencies and the functional dependency a ® b in F.

H Attribute A is extraneous in a if A Î a
   and F logically implies (F – {
a ® b}) È {(a  – A) ® b}.

H Attribute A is extraneous in b if A Î b
  and the set of functional dependencies
  (F  – {
a ® b}) È {a ®(b A)} logically implies F.

n Note: implication in the opposite direction is trivial in each of the cases above, since a "stronger" functional dependency always implies a weaker one

n Example: Given F = {A ® C, AB ® C }

H B is extraneous in AB ® C because {A ® C, AB ® C} logically implies A ® C (I.e. the result of dropping B from AB ® C).

n Example:  Given F = {A ® C, AB ® CD}

H C is extraneous in AB ® CD since  AB ® C can be inferred even after deleting C

 

Testing if an Attribute is Extraneous

 

n  Consider a set F of functional dependencies and the functional dependency a ® b in F.

n  To test if attribute A Î a is extraneous in a

4 compute ({a} – A)+ using the dependencies in F

4  check that ({a} – A)+ contains A; if it does, A is extraneous

n  To test if attribute A Î b  is extraneous in b

4 compute a+  using only the dependencies in 
         F' = (F  – {
a ® b}) È {a ®(b A)},

4  check that a+  contains A; if it does, A is extraneous

 

Canonical Cover

 

n A canonical cover for F is a set of dependencies Fc such that

H F logically implies all dependencies in Fc, and

H Fc logically implies all dependencies in F, and

H No functional dependency in Fc contains an extraneous attribute, and

H Each left side of functional dependency in Fc is unique.

n To compute a canonical cover for F:
repeat
     
Use the union rule to replace any dependencies in F
               
a1 ® b1 and a1 ® b1 with a1 ® b1 b2
      Find a functional dependency
a ® b with an
                extraneous attribute either in
a or in b
      If an extraneous attribute is found, delete it from
a ® b
until F does not change

n Note: Union rule may become applicable after some extraneous attributes have been deleted, so it has to be re-applied

 

 

Example of Computing a Canonical Cover

 

n  R = (A, B, C)
F = {A
® BC
        B
® C
        A
® B
      AB
® C}

n  Combine A ® BC and A ® B into A ® BC

H Set is now {A ® BC, B ® C, AB ® C}

n  A is extraneous in AB ® C

H Check if the result of deleting A from  AB ® is implied by the other dependencies

4 Yes: in fact,  B ® C is already present!

H Set is now {A ® BC, B ® C}

n  C is extraneous in A ® BC

H Check if A ® C is logically implied by A ® B and the other dependencies

4 Yes: using transitivity on A ® B  and B ® C.

  Can use attribute closure of A in more complex cases

n  The canonical cover is:        A ® B
                B
® C

 

 

 

Goals of Normalization

 

n Decide whether a particular relation R is in "good" form.

n In the case that a relation R is not in "good" form, decompose it into a set of relations {R1, R2, ..., Rn} such that

H each relation is in good form

H the decomposition is a lossless-join decomposition

n Our theory is based on:

H functional dependencies

H multivalued dependencies

 

Decomposition

 

n  Decompose the relation schema Lending-schema into:

Branch-schema = (branch-name, branch-city,assets)

Loan-info-schema = (customer-name, loan-number,
                                                   branch-name, amount)

n  All attributes of an original schema (R) must appear in the decomposition (R1, R2):

          R = R1 È R2

n  Lossless-join decomposition.
For all possible relations r on schema R

                r = ÕR1 (r)    ÕR2 (r)

n  A decomposition of R into R1 and R2 is lossless join if and only if at least one of the following dependencies is in F+:

H   R1 Ç R2 ® R1

H R1 Ç R2 ® R2

 

Example of Lossy-Join Decomposition

n Lossy-join decompositions result in information loss.

n Example: Decomposition of R = (A, B)
      R2 = (A)      R
2 = (B)

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


Normalization Using Functional Dependencies

 

n When we decompose a relation schema R with a set of functional dependencies F into R1, R2,.., Rn we want

H Lossless-join decomposition:  Otherwise decomposition would result in information loss.

H No redundancy:  The relations Ri preferably should be in either Boyce-Codd Normal Form or Third Normal Form.

H Dependency preservation: Let Fi be the set of dependencies F+ that include only attributes in Ri.

4   Preferably the decomposition should be dependency preserving, that is,       (F1 È F2 ÈÈ Fn)+ = F+

4 Otherwise, checking updates for violation of functional dependencies may require computing joins, which is expensive.

 

Example

 

n R = (A, B, C)
F = {A
® B, B ® C)

H Can be decomposed in two different ways

n R1 = (A, B),   R2 = (B, C)

H Lossless-join decomposition:

     RÇ R2 = {B} and B ® BC

H Dependency preserving

n R1 = (A, B),   R2 = (A, C)

H Lossless-join decomposition:

     RÇ R2 = {A} and A ® AB

H Not dependency preserving
(cannot check B
® C without computing R1     R2)

 

Testing for Dependency Preservation

 

n To check if a dependency a®b is preserved in a decomposition of R into R1, R2, …, Rn we apply the following simplified test (with attribute closure done w.r.t. F)

H result = a
while (changes to result) do
  for each Ri in the decomposition
           t = (result
Ç Ri)+ Ç Ri
                  
result = result
È t

H If result contains all attributes in b, then the functional dependency
a ® b is preserved.

n We apply the test on all dependencies in F to check if a decomposition is dependency preserving

n This procedure takes polynomial time, instead of the exponential time required to compute F+ and (F1 È F2 ÈÈ Fn)+

 

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

a ® b, where a Í R and b Í R, at least one of the following holds:

 

 

n  a  ® b  is trivial (i.e., b Í a)

n  a is a superkey for R

 

 

 

 

Example

n R = (A, B, C)
F = {A
® B
      B
® C}
Key = {A}

n R is not in BCNF

n Decomposition R1 = (A, B),  R2 = (B, C)

H R1 and R2 in BCNF

H Lossless-join decomposition

H Dependency preserving

 

Testing for BCNF

 

n To check if a non-trivial dependency a ®b  causes a violation of BCNF

1.  compute a+ (the attribute closure of a), and

2.  verify that it includes all attributes of R, that is, it is a superkey of R.

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

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

n However, using only F is incorrect when testing a relation in a decomposition of R

H E.g. Consider R (A, B, C, D), with F = { A ®B, B ®C}

4 Decompose R into R1(A,B) and R2(A,C,D)

4 Neither of the dependencies in F contain only attributes from
(A,C,D) so we might be mislead into thinking R2 satisfies BCNF. 

4 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 R
i in result  that is not in BCNF)
                then begin
                        
let
a  ® b  be a nontrivial functional
                                   dependency that holds on Ri
                                  
such that
a  ® Ri is not in F+,
                                   and
a Ç b  = Æ;
                            result := (result – R
i ) È (Rib) È (a, b );
               
end
                else
done := true;

Note:  each Ri is in BCNF, and decomposition is lossless-join.

 

Example of BCNF Decomposition

 

n 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}

n Decomposition

H R1 = (branch-name, branch-city, assets)

H R2 = (branch-name, customer-name, loan-number, amount)

H R3 = (branch-name, loan-number, amount)

H R4 = (customer-name, loan-number)

n   Final decomposition
                R1, R3, R4

 

Testing Decomposition for BCNF

 

n To check if a relation Ri in a decomposition of R is in BCNF,

H 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)

H or use the original set of dependencies F that hold on R, but with the following test:

  for every set of attributes a Í Ri, check that a+ (the attribute closure of a) either includes no attribute of Ri- a, or includes all attributes of Ri.

4 If the condition is violated by some a ® b  in F, the dependency
     
a ® (a+ - a ) Ç Ri
can be shown to hold on Ri, and Ri violates BCNF.

4  We use above dependency to decompose Ri

 

BCNF and Dependency Preservation

 

It is not always possible to get a BCNF decomposition that is

dependency preserving

 

 

n R = (J, K, L)
F = {JK
® L
      L
® K}
Two candidate keys = JK and JL

n R is not in BCNF

n Any decomposition of R will fail to preserve
               

                   JK ® L

 

Third Normal Form: Motivation

 

n There are some situations where

H BCNF is not dependency preserving, and

H efficient checking for FD violation on updates is important

n Solution: define a weaker normal form, called Third Normal Form.

H Allows some redundancy (with resultant problems; we will see examples later)

H But FDs can be checked on individual relations without computing a join.

H There is always a lossless-join, dependency-preserving decomposition into 3NF.

 

Third Normal Form

 

n A relation schema R is in third normal form (3NF) if for all:

          a ® b in F+
at least one of the following holds:

H a ® b is trivial (i.e., b Î a)

H a is a superkey for R

H Each attribute A in ba is contained in a candidate key for R.

   (NOTE: each attribute may be in a different candidate key)

n If a relation is in BCNF it is in 3NF (since in BCNF one of the first two conditions above must hold).

n Third condition is a minimal relaxation of BCNF to ensure dependency preservation (will see why later).

 

n  Example

H R = (J, K, L)
F =
{JK ® L, L ® K}

H Two candidate keys:  JK and JL

H R is in 3NF

     JK ® L       JK is a superkey
  L
® K         K is contained in a candidate key

n BCNF decomposition has  (JL) and (LK)

n Testing for JK ® L requires a join

n  There is some redundancy in this schema

n  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

 

n Optimization: Need to check only FDs in F, need not check all FDs in F+.

n Use attribute closure to check for each dependency a ® b, if a is a superkey.

n If a is not a superkey, we have to verify if each attribute in b is contained in a candidate key of R

H this test is rather more expensive, since it involve finding candidate keys

H testing for 3NF has been shown to be NP-hard

H 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
a ® b in Fc do
      if
none of the schemas Rj, 1 £ j  £ i contains  a b
                then begin
                                  
i := i  + 1;
                                   Ri  :=
a b
                        
end
if
none of the schemas Rj, 1 £ j  £ i contains a candidate key for R
     
then begin
                        
i := i  + 1;
                         R
i := any candidate key for R;
               
end
return
(R
1, R2, ..., Ri)              

 

n  Above algorithm ensures:

H each relation schema Ri is in 3NF

H decomposition is dependency preserving and lossless-join

H Proof of correctness is at end of this file (click here)

 

Example

 

n Relation schema:

   Banker-info-schema = (branch-name, customer-name,
                     banker-name, office-number)

n The functional dependencies for this relation schema are:
      banker-name
® branch-name office-number
      customer-name branch-name
® banker-name

n The key is:

                   {customer-name, branch-name}

 

Applying 3NF to Banker-info-schema

 

n 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)

n Since Banker-schema contains a candidate key for
Banker-info-schema, we are done with the decomposition process.

 

Comparison of BCNF and 3NF

 

n It is always possible to decompose a relation into relations in 3NF and

H the decomposition is lossless

H the dependencies are preserved

n It is always possible to decompose a relation into relations in BCNF and

H the decomposition is lossless

H it may not be possible to preserve dependencies.

 

n Example of problems due to redundancy in 3NF

H R = (J, K, L)
F =
{JK ® L, L ® K}

 

 

 

 

 

 

 

 

 

 

 

 

 

 


A schema that is in 3NF but not in BCNF has the problems of

nrepetition of information (e.g., the relationship l1, k1)

nneed to use null values (e.g., to represent the relationship
     l
2, k2 where there is no corresponding value for J).

 

 

 

 

 

Design Goals

 

n Goal for a relational database design is:

H BCNF.

H Lossless join.

H Dependency preservation.

n If we cannot achieve this, we accept one of

H Lack of dependency preservation

H Redundancy due to use of 3NF

n 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

n 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

 

n  If decomposition is not dependency preserving, we can have an extra materialized view for each dependency a ®b in Fc that is not preserved in the decomposition

n  The materialized view is defined as a projection on a b of the join of the relations in the decomposition

n  Many newer database systems support materialized views and database system maintains the view when the relations are updated.

H No extra coding effort for programmer.

n  The functional dependency a ® b is expressed by declaring a  as a candidate key on the materialized view.

n  Checking for candidate key cheaper than checking a ® b

n  BUT:

H Space overhead: for storing the materialized view

H Time overhead: Need to keep materialized view up to date when  
relations are updated

H Database system may not support key declarations on
materialized views

 

Multivalued Dependencies

 

n There are database schemas in BCNF that do not seem to be sufficiently normalized

n 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

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

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


n here are no non-trivial functional dependencies and therefore the relation is in BCNF

n 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)

n Therefore, it is better to decompose classes into:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


We shall see that these two relations are in Fourth Normal Form (4NF)

 

 

 

 

 

Multivalued Dependencies (MVDs)

 

n Let R be a relation schema and let a Í R and b Í R.   The multivalued dependency

                   a ®® b

    holds on R if in any legal relation r(R), for all pairs for tuples t1 and t2 in r such that t1[a] = t2 [a], there exist tuples t3 and t4 in r such that:

           t1[a] = t2 [a] = t3 [a] t4 [a]
       t3[
b]         =  t1 [b]
       t3[R  –
b] =  t2[R  – b]
       t4 [
b]         =  t2[b]
       t4[R  –
b] =  t1[R  – b]

 

n Tabular representation of a ®® b

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


Example

 

n Let R be a relation schema with a set of attributes that are partitioned into 3 nonempty subsets.

                   Y, Z, W

n 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 < y2, z2, w1 > Î r

n Note that since the behavior of Z and W are identical it follows that Y ®® Z if Y ®® W

 

n In our example:

          course ®® teacher      
      course
®® book

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

n Note:

H If Y ® Z  then  Y ®® Z

H Indeed we have (in above notation) Z1 = Z2
The claim follows.

 

Use of Multivalued Dependencies

 

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

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

         

Theory of MVDs

n  

n From the definition of multivalued dependency, we can derive the following rule:

H If a ® b, then a ®® b

    That is, every functional dependency is also a multivalued dependency

n The closure D+ of D is the set of all functional and multivalued dependencies logically implied by D.

H We can compute D+ from D, using the formal definitions of functional dependencies and multivalued dependencies.

H We can manage with such reasoning for very simple multivalued dependencies, which seem to be most common in practice

H For complex dependencies, it is better to reason about sets of     dependencies using a system of inference rules (see Appendix C).

 

 

 

 

 

Fourth Normal Form

 

n A relation schema R is in 4NF with respect to a set D of functional and multivalued dependencies if for all multivalued dependencies in D+ of the form a ®® b, where a Í R and b Í R, at least one of the following hold:

H a ®® b is trivial (i.e., b Í a or a È b = R)

H a is a superkey for schema R

n If a relation is in 4NF it is in BCNF

 

Restriction of Multivalued Dependencies

 

n The restriction of  D to Ri is the set Di consisting of

H   All functional dependencies in D+ that include only attributes of Ri

H All multivalued dependencies of the form

   a ®® (b Ç Ri)

    where a Í Ri  and  a ®® b is in D+

 

4NF Decomposition Algorithm

 

     result: = {R};
done := false;
compute D+;
Let D
i denote the restriction of D+ to Ri

    while (not done)
    if (there is a schema Ri in result that is not in 4NF) then
       begin

          let a ®® b be a nontrivial multivalued dependency that holds
            on Ri such that
a ® Ris not in Di, and aÇb=f;
          result :=  (result - Ri)
È (Ri - bÈ (a, b);
       end
    else done:= true;

Note: each Ri is in 4NF, and decomposition is lossless-join

 

Example

 

n  R =(A, B, C, G, H, I)

    F ={ A ®® B

          B ®® HI

          CG ®® H }

n  R is not in 4NF since A ®® B and A is not a superkey for R

n  Decomposition

    a) R1 = (A, B)                       (R1 is in 4NF)

    b) R2 = (A, C, G, H, I)                    (R2 is not in 4NF)

    c) R3 = (C, G, H)                  (R3 is in 4NF)

    d) R4 = (A, C, G, I)               (R4 is not in 4NF)

n  Since A ®® B and B ®® HI, A ®® HI, A ®® I

    e) R5 = (A, I)                         (R5 is in 4NF)

    f)R6 = (A, C, G)                    (R6 is in  4NF)

 

Further Normal Forms

 

n Join dependencies generalize multivalued dependencies

H lead to project-join normal form (PJNF) (also called fifth normal form)

n A class of even more general constraints, leads to a normal form called domain-key normal form.

n Problem with these generalized constraints:  are hard to reason with, and no set of sound and complete set of inference rules exists.

n Hence rarely used

 

Overall Database Design Process

 

n We have assumed schema R is given

H R could have been generated when converting E-R diagram to a set of tables.

H R could have been a single relation containing all attributes that are of interest (called universal relation).

H Normalization breaks R into smaller relations.

H R could have been the result of some ad hoc design of relations, which we then test/convert to normal form.

 

ER Model and Normalization

 

n When an E-R diagram is carefully designed, identifying all entities correctly, the tables generated from the E-R diagram should not need further normalization.

n However, in a real (imperfect) design there can be FDs from non-key attributes of an entity to other attributes of the entity

n E.g. employee entity with attributes department-number  and department-address, and  an FD department-number ® department-address

H Good design would have made department an entity

n FDs from non-key attributes of a relationship set possible, but rare --- most relationships are binary

 

Universal Relation Approach

 

n Dangling tuples – Tuples that "disappear" in computing a join.

H Let r1 (R1), r2 (R2), …., rn (Rn) be a set of relations

H A tuple r of the relation ri is a dangling tuple if r is not in the relation:

              ÕRi (r1      r2      …     rn)

n The relation r1     r2    …    rn is called a universal relation since it involves all the attributes in the "universe" defined by

R1 È R2 ÈÈ Rn

n If dangling tuples are allowed in the database, instead of decomposing a universal relation, we may prefer to synthesize a collection of normal form schemas from a given set of attributes.

 

Universal Relation Approach

 

n Dangling tuples may occur in practical database applications.

n They represent incomplete information

n E.g. may want to break up information about loans into:

(branch-name, loan-number) 

(loan-number, amount)

(loan-number, customer-name)

n Universal relation would require null values, and have dangling tuples

 

n A particular decomposition defines a restricted form of incomplete information that is acceptable in our database.

H Above decomposition requires at least one of customer-name,        branch-name or amount in order to enter a loan number without using null values

H Rules out storing of customer-name, amount without an appropriate   loan-number (since it is a key, it can't be null either!)

n Universal relation requires unique attribute names unique role assumption

H e.g.  customer-name, branch-name

n Reuse of attribute names is natural in SQL since relation names can be prefixed to disambiguate names

 

Denormalization for Performance

 

n May want to use non-normalized schema for performance

n E.g. displaying customer-name along with account-number and balance requires join of account with depositor

n Alternative 1:  Use denormalized relation containing attributes of account as well as depositor with all above attributes

H faster lookup

H Extra space and extra execution time for updates

H extra coding work for programmer and possibility of error in extra code

n Alternative 2: use a materialized view defined as
          account      depositor

H Benefits and drawbacks same as above, except no extra coding work for programmer and avoids possible errors

 

Other Design Issues

 

n Some aspects of database design are not caught by normalization

n Examples of bad database design, to be avoided:

    Instead of earnings(company-id, year, amount), use

H earnings-2000, earnings-2001, earnings-2002, etc., all on the schema (company-id, earnings).

4 Above are in BCNF, but make querying across years difficult and needs new table each year

H company-year(company-id, earnings-2000, earnings-2001, 
                                                                                    earnings-2002
)

4 Also in BCNF, but also makes querying across years difficult and requires new attribute each year.

4 Is an example of a crosstab, where values for one attribute become column names

4 Used in spreadsheets, and in data analysis tools

 

 

 

 

 

 

 

Proof of Correctness of 3NF Decomposition Algorithm

Correctness of 3NF Decomposition Algorithm

 

n 3NF decomposition algorithm is dependency preserving (since there is a relation for every FD in Fc)

n Decomposition is lossless join

H A candidate key (C) is in one of the relations Ri in decomposition

H Closure of candidate key under Fc must contain all attributes in R

H   Follow the steps of attribute closure algorithm to show there is only one tuple in the join result for each tuple in Ri

Claim: if a relation Ri is in the decomposition generated by the

above algorithm, then Ri satisfies 3NF.

n Let Ri be generated from the dependency a ®b

n Let g ® B be any non-trivial functional dependency on Ri. (We need only consider FDs whose right-hand side is a single attribute.)

n Now, B can be in either b or a but not in both. Consider each case separately.

n Case 1: If B in b:

H If g is a superkey, the 2nd condition of 3NF is satisfied

H Otherwise a must contain some attribute not in g

H Since g ® B is in F+ it must be derivable from Fc, by using attribute closure on g.

H Attribute closure not have used a ®b - if it had been used, a must be contained in the attribute closure of g, which is not possible, since we assumed g is not a superkey.

H Now, using a®  (b- {B}) and g ® B, we can derive a ®B

   (since g Í a b, and B Ï g since g ® B is non-trivial)

H Then, B is extraneous in the right-hand side of a ®b; which is not possible since a ®b is in Fc.

H Thus, if B is in b then g  must be a superkey, and the second condition of 3NF must be satisfied.

n Case 2:  B is in a.

H Since a  is a candidate key, the third alternative in the definition of 3NF is trivially satisfied.

H In fact, we cannot show that g is a superkey.

H This shows exactly why the third alternative is present in the definition of 3NF.

Q.E.D.

End of Chapter

 

0 comments:

Post a Comment