Normalization
Features of Good Relational Design
Decomposition
The only way to avoid the repitition-of-information problem is to decompose schemas. Not all decompositions are good. If we decompose a schema into two, we can lose information. This is known as lossy decomposition.
Lossless Decomposition
We say that the decomposition is a lossless decomposition if there is no loss of information by replacing a schema with two relation schemas.
Normalization Theory
- Decide whether a particular relation is in "good" form.
- In the case that a relation is not in "good" form, decompose it into set of relations () such that
- each relation is in good form
- the decomposition is a lossless composition
- Our theory is based on:
- Functional Dependencies
- Multivalued Dependencies
Functional Dependencies
There are usually a variety of constraints (rules) on the data in the real world.
- For example, some of the constraints that are expected to hold in a university database are:
- Students and instructors are uniquely identified by their ID.
- Each student and instructor has only one name.
- Each instructor and student is (primarily) associated with only one department.
- Each department has only one value for its budget, and only one associated building.
An instance of a relation that satisfies all such real-world constraints is called a legal instance of the relation. A legal instance of a database is one where all the relation instances are legal instances.
- A superkey is a set of attributes that uniquely identifies an entire tuple
- A functional dependency requires that the value for a certain set of attributes determines uniquely the value for another set of attributes.
- A functional dependency is a generalization of the notion of a key.
Definition
Let be a relation schema and . The functional dependency hold on iff and only if for any legal relations , whenever any two tuples of agree on the attributes , they also agree on the attributes .
Closure of a Set of Functional Dependencies
- Given a set of functional dependencies, there are certain other functinoal dependencies that are logically implied by . The set of all functional dependencies logically implied by is the closure of . We denote the closure of by .
Keys and Functional Dependencies
is a superkey for relation schema if and only if and for no .
Functional dependencies allow us to express constraints that cannot be expressed using superkeys.
Use of Functional Dependencies
We use functional dependencies to:
- To test relations to see if they are legal under a given set of functional dependencies.
- if a relation is legal under a set of functional dependencies, we say that satisfies
- to specify constraints on the set of legal relations
- we say that holds on if all legal relations on satisfy the set of functional dependencies
A functional dependency is trivial if it is satisfied by all instances of a relation. In general is trivial if .
Dependency Preservation
- Testing functional dependency constraints each time the database is updated can be costly
- It is useful to design the database in a way that constraints can be tested efficiently
- If testing a functional dependency can be done by considering just one relation, then the cost of testing this constraint is low
- When decomposing a relation it is possible that it is no longer possible to do the testing without having the perform a Cartesian Product
- A decomposition that makes it computationally hard to enforce functional dependency is said to be NOT dependency preserving.
Let be the set of dependencies in that include only attributes in
- A decomposition is dependency preserving if
With the above definition, testing for dependency preservation takes exponential time.
Normal Forms
Boyce-Codd Normal Form
- A relation schema is in BCNF with respect to a set of functional dependencies if for all functional dependencies in of the form where and , at least one of the following holds:
- is trivial
- is a superkey for
Decomposing a Schema into BCNF
Let be a schema that is not in BCNF. Let be the FD that causes a violation of BCNF.
We decompose into:
- (\alpha \cup \beta)
- ( - (\beta - \alpha))
BCNF Decomposition Algorithm
result = {R}done = Falsecompute F+while (not done):if (there is a schema R_i, in result that is not in BCNF):let a -> B be a nontrivial functional dependency that holds on R_i such that a -> R_i is not in F+ and a intersection B = empty setelse:done = True
Third Normal Form
- A relation schema is in third normal form (3NF) if for all:
- in
- at least one of the following holds:
- is trivial
- is superkey for
- Each attribute in is contained in a candidate key for
- 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)
There are some situations where
- BCNF is not dependency preserving
- 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)
- but functional dependencies can be checked on individual relations without computing a join
- there is always a lossless-join, dependency-preserving decomposition into 3NF
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:i := i + 1;Ri := a Bif none of the schemas Rj, 1 <= j <= i contains a candidate key for R:i := i + 1;Ri := any candidate key for R;/* Optionally, remove redundant relations */repeatif any schema Rj is contained in another schema Rk/* delete Rj */Rj = R;;i=i-1;return (R1, R2, ..., Ri)
Above algorithm ensures
- each relation schema is in 3NF
- decomposition is dependency preserving and lossless-join
- proof of correctness
Comparison of BCNF and 3NF
Advantages to 3NF over BCNF. It is always possible to obtain a 3NF design without sacrificing losslessness or dependency preservation.
Disadvantages to 3NF
- We may have to use null values to represent some of the possible meaningful relationships among data items
- there is the problem of repetition of information
Goals of Normalization
- Let be a relation scheme with a set of functional dependencies
- Decide whether a relation scheme is in "good" form
- In the case that a relation scheme is not in "good" form, need to decompose it into a set of relation scheme such that:
- each relation scheme is in good form
- the decomposition is a lossless decomposition
- preferably, the decomposition should be dependency preserving
Functional-Dependency Theory Roadmap
- formal theory that tells us which dependencies are implied logically by a given set of functional dependencies
- algorithms to generate lossless decompositions into BCNF and 3NF
- algorithms to test if a decomposition is a dependency-preserving
Closure of a Set of Functional Dependencies
We can compute , the closure of , by repeatedly applying Armstrong's Axioms:
- reflexive rule: if , then
- augmentation rule: if , then
- transitivity rule: if , and , then
- These rules are:
- Sound: generate only functional dependencies that actually hold
- Complete: generate all functional dependencies that hold
- Union: if and holds, then holds.
- Decomposition: if holds, then holds and holds
- Pseudotransitivity: if holds and holds, then holds.
- The above rules can be inferred from Armstrong's axioms
Procedure for Computing F+
- To compute the closure of a set of functional dependencies :
F+ = Fwhile F+ does not change any furtherfor each functional dependency f in F+:apply reflexivity and augmentation rules on Fadd the resulting functional dependencies to F+for each pair of functional dependencies f1 and f2 in F+:if f1 and f2 can be combined using transitivity:add the resulting functional dependency to F+
Closure of Attribute Sets
- Given a set of attributes , the closure of under (denoted by ) is the set of attributes that are functionally determined by under
- Algorithm to compute , the closure of under
result = awhile (changes to result):for each beta in gamma in F:if beta is a super set of result:result = result union gamma
Uses of Attribute Closure
There are several uses of the attribute closure algorithm:
- Testing for superkey
- to test if is a superkey, we compute and check if contains all attribute of
- Testing functional dependencies
- to check if a functional dependency holds, check if
- we compute by using attribute closure, and then check if it contains
- this is a simple and cheap test, and very useful
- computing closure of
- for each , we find the closure , and for each , we output a functional dependency
Canonical Cover
- Suppose that we have a set of functional dependencies on a relation schema. Whenever a user performs an update on the relation, the database system must ensure that the update does not violate any functional dependencies; that is, all the functional dependencies in are satisfied in the new database state.
- If an update violates any functional dependencies in the set , the system must roll back the update.
- We can reduce the effort spent in checking for violations by testing a simplified set of functional dependencies that has the same closure as the given set.
- This simplified set is termed the canonical cover
- To define canonical cover we must first define extraneous attributes.
- An attribute of a functional dependency in is extraneous if we can remove it without changing
To compute a canonical cover for :
repeatUse the union rule to replace any dependencies in FFind a functional dependency in F with an extraneous attribute either in a or in BIf an extraneous attribute is found, delete it from a -> Buntil F not change
A canonical cover for is a set of dependencies such that:
- logically implies all dependencies in
- logically implies all dependencies in
- No functional dependency in contains an extraneous attribute
- Each left side of functional dependency in is unique. There are no two dependencies in
Extraneous attributes
- Removing an attribute from the left side of a functional dependency could make it a stronger constraint. Depending on what our set of functional dependencies happens to be, we may be able to remove from safely.
- Removing an attribute from the right side of a functional dependency could make it a weaker constraint. Depending on what our set of functional dependencies happens to be, we may able to remove from safely
An attribute of a functional dependency in is extraneous if we can remove it without changing
Testing if an Attribute is Extraneous
Let be a relation schema and let be a set of functional dependencies that hold on . Consider an attribute in the functional dependency
- To test if is extraneous in
- Consider the set:
- $F' = (F - {\alpha \to \beta}) \cup {\alpha \to (\beta - A)}
- check that contains under ; if it does, A is extraneous in
- Consider the set:
- To test if is extraneous in
- Let . Check if can be inferred from
- Compute using the dependencies in
- IF includes all attributes in then, A is extraneous in
- Let . Check if can be inferred from
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.
Dependency Preservation
Let be the set of dependencies in that include only attributes in .
- A decomposition is dependency preserving, if
Using the above definition, for testing for dependency preservation takes exponential time. Note that if a decomposition is NOT dependency preserving then checking updates for violation of functional dependencies may require computing joins, which is expensive.
- Let be the set of dependencies on schema and let be a decomposition of .
- The restriction of F to Ri is the set $F_i of all functional dependencies in that include only attributes of .
- Since all functional dependencies in a restriction involve attributes of only one relation schema, it is possible to test such a dependency for satisfaction by checking only one relation.
- Note that the definition of restriction uses all dependencies in , not just those in .
- The set of restrictions is the set of functional dependencies that can be checked efficiently.
Testing for Dependency Preservation
Checking is expensive and since it requires the computation of .
Alternative 1:
- If each member of F can be tested on one of the relations of the decomposition, then the decomposition is dependency preserving. However, this does not always work! This can be used as a sufficient condition, but if it fails we cannot conclude that the decomposition is not dependency preserving!
Alternative 2:
- To check if a dependency is preserved in a decomposition of into , we apply the following test (with attribute closure done with respect to )
result = arepeatfor each $R_i$ in the decompositiont = (result intersection Ri)+ intersection Riresult = result union tuntil (result does not change)If result contains all attributes in , then the functional dependencya -> B is preserved.
- We apply the test on all dependencies in to check if a decomposition is dependency preserving
- This procedure takes polynomial time, instead of the exponential time required to compute and
Multi-valued Dependencies
A multi-valued dependency (MVD) on says that: if two tuples of agree on all the attributes of , then their components in may be swapped, and the result will be two tuples that are also in the relation, i.e., for each value of , the values of are independent of the values .
Let be a relation schema and let and . The multi-valued dependency holds on if in any legal relation , for all pairs for tuples and in such that , there exist tuples and in such that:
t1[a] = tt2[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]
Let be a relation schema with a set of attributes that are partitioned into 3 nonempty subsets . We say that multi-determines ) if and only if for all possible relations and then and
Use of Multi-valued Dependencies
We use multi-values dependencies in two ways:
- To test relations to determine whether they are legal under a given set of functional and multi-valued dependencies
- To specify constraints on the set of legal relations. We shall concern ourselves only with relations that satisfy a given set of functional and multi-valued dependencies.
If a relation fails to satisfy a given multi-values dependency, we can construct a relation that does satisfy the multi-valued dependency by adding tuples to .
Fourth Normal Form
A relation schema is in 4NF with respect to a set of functional and multi-valued dependencies if for all multi-valued dependencies in of the form , where and , at least one of the following hold:
- is trivial (i.e., or )
- is a superkey for schema
IF a relation is in 4NF it is in BCNF.
Decomposition Algorithm
result: = {R}done := falsecompute D+Let Di denote the restriction of D+ to Riwhile (not done):if (there is a schema Ri in result that is not in 4NF):let a -> B be a nontrivial multivalued dependency that holds on Ri such that a -> Ri is not in Di and a intersection B = empty setresult := (result - Ri) union (Ri - B) union (a, B)else:done := true