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) R2 = (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 ® C 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) R2 = (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:
R1 Ç R2 = {B} and B ® BC
H Dependency preserving
n R1 = (A, B), R2 = (A, C)
H Lossless-join decomposition:
R1 Ç 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 Ri 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 – Ri ) È (Ri – b) È (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 b – a 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;
Ri := any candidate key for R;
end
return (R1, 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
l2, 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)
0 comments:
Post a Comment