Week 8 Database Design
Week 8 Database Design
Pawar
Assistant Professor (Senior Scale)
Department of CSE, School of Computer Science and Engineering
Manipal University Jaipur.
Database in Good Form? Normalization
Outlines
Features of Good Relational Design
Atomic Domains and First Normal Form
Functional Dependencies
Decomposition Using Functional Dependencies
Functional Dependency Theory
Algorithms for Decomposition using Functional Dependencies
Normal Forms
Features of Good Relational Designs
• Suppose we combine instructor and department into in_dep, which represents the natural join of
the relations instructor and department
The problem arises when we have two employees with the same name
• The next slide shows how we lose information- we cannot reconstruct the original
employee relation- and this is a lossy decomposition.
A Lossy
Decomposition
Lossless Decomposition
• Let R be a relation schema and let R1 and R2 form a decomposition of
R . That is R = R1 U R2
• We say that the decomposition is a lossless decomposition if there is
no loss of information by replacing R with the two relation schemas
R1 U R2
• Formally,
R1 (r) R2 (r) = r
• And, conversely a decomposition is lossy if
r R1 (r) R2 (r) = r
Example of Lossless Decomposition
• Decomposition of R = (A, B, C)
R1 = (A, B) R2 = (B, C)
Lossless Decomposition
• We can use functional dependencies to show when certain decomposition are
lossless.
• For the case of R = (R1, R2), we require that for all possible relations r on schema R
r = R1 (r ) R2 (r )
• A decomposition of R into R1 and R2 is lossless decomposition if at least one of
the following dependencies is in F+:
• R1 R2 R1
• R1 R2 R2
• The above functional dependencies are a sufficient condition for lossless join
decomposition; the dependencies are a necessary condition only if all constraints
are functional dependencies
Example
• R = (A, B, C)
F = {A B, B C)
• R1 = (A, B), R2 = (B, C)
• Lossless decomposition:
R1 R2 = {B} and B BC
• R1 = (A, B), R2 = (A, C)
• Lossless decomposition:
R1 R2 = {A} and A AB
• Note:
• B BC
is a shorthand notation for
• B {B, C}
First Normal Form
• Domain is atomic if its elements are considered to be indivisible units
• Examples of non-atomic domains:
• Set of names, composite attributes
• Identification numbers like CS101 that can be broken up into parts
• A relational schema R is in first normal form if the domains of all
attributes of R are atomic
• Non-atomic values complicate storage and encourage redundant
(repeated) storage of data
• Example: Set of accounts stored with each customer and set of owners stored
with each account
• We assume all relations are in first normal form.
First Normal Form (Cont’d)
• Atomicity is actually a property of how the elements of the domain
are used.
• Example: Strings would normally be considered indivisible
• Suppose that students are given roll numbers which are strings of the
form CS0012 or EE1127
• If the first two characters are extracted to find the department, the
domain of roll numbers is not atomic.
• Doing so is a bad idea: leads to encoding of information in application
program rather than in the database.
• Also does not allow repeated fields, e.g., vehicle1, vehicle2, vehicle3.
Second Normal Form
All non-key fields must be a function of the full key
• – Example that violates second normal form:
• Key is Part + Warehouse
• Someone found it convenient to add Address, to make a report easier
• WarehouseAddress is a fact about Warehouse, not about Part
Second Normal Form
• Problems:
• Warehouse address is repeated in every row that refers to a part stored in a
warehouse
• If warehouse address changes, every row referring to a part stored in that
warehouse must be updated
• Data might become inconsistent, with different records showing different
addresses for the same warehouse
• If at some time there were no parts stored in the warehouse, there may be no
record in which to keep the warehouse’s address.
Second Normal Form
• Solution:
• Two entity types: Inventory, and Warehouse
• Advantage: solves problems from last slide
• Disadvantage: If application needs address of each
• warehouse stocking a part, it must access two tables instead of one.
This used to be a problem but rarely is now.
Normalization Theory
• Decide whether a particular relation R is in “good” form.
• In the case that a relation R is not in “ good ” form,
decompose it into set of relations {R1, R2, ..., Rn} such that
• Each relation is in good form
• The decomposition is a lossless decomposition
• 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.
Functional Dependencies (Cont.)
• 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.
• Constraints on the set of legal relations.
• Require that a certain attribute's value uniquely determines
the value for another set of attributes.
• A functional dependency is a generalization of the notion of
a key.
Functional Dependencies Definition
• Let R be a relation schema
R and R
• The functional dependency
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 , they also agree on the attributes . That is,
• Note: Union rule may become applicable after some extraneous attributes have been
deleted, so it has to be re-applied
Example: Computing a Canonical Cover
• R = (A, B, C)
F = {A BC
BC
AB
AB C}
• Combine A BC and A B into A BC
• Set is now {A BC, B C, AB C}
• A is extraneous in AB C
• Check if the result of deleting A from AB C is implied by the other
dependencies
• Yes: in fact, B C is already present!
• Set is now {A BC, B C}
Example: Computing a Canonical Cover
• C is extraneous in A BC
• Check if A C is logically implied by A B and the other
dependencies
• Yes: using transitivity on A B and B C.
• Can use attribute closure of A in more complex cases
• The canonical cover is: AB
BC
Dependency Preservation
• Testing functional dependency constraints each time the database is
updated can be costly
• It is useful to design the database to test constraints 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 to do the testing without
having to perform a Cartesian Produced.
• A decomposition that makes it computationally hard to enforce
functional dependency is said to be NOT dependency preserving.
Dependency Preservation
• Let Fi be the set of dependencies F + that include only attributes in Ri.
• A decomposition is dependency preserving, if
(F1 F2 … Fn )+ = F +
• Using the above definition, testing for dependency preservation take
exponential time.
• Not that if a decomposition is NOT dependency preserving then
checking updates for violation of functional dependencies may
require computing joins, which is expensive.
Dependency Preservation (Cont.)
• Let F be the set of dependencies on schema R and let R1, R2 , .., Rn be a
decomposition of R.
• The restriction of F to Ri is the set Fi of all functional dependencies in F +
that include only attributes of Ri .
• 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 in F +, not
just those in F.
• The set of restrictions F1, F2 , .. , Fn is the set of functional dependencies
that can be checked efficiently.
Testing for Dependency Preservation
To check if a dependency is preserved in a decomposition of R into R1, R2,
…, Rn , we apply the following test (with attribute closure done with respect to F)
• result =
repeat
for each Ri in the decomposition
t = (result Ri)+ Ri
result = result t
until (result does not change)
• If result contains all attributes in , then the functional dependency is preserved.
We apply the test on all dependencies in F to check if a decomposition is
dependency preserving
This procedure takes polynomial time, instead of the exponential time required
to compute F+ and (F1 F2 … Fn)+
Dependency Preservation Example
• Consider a schema:
dept_advisor(s_ID, i_ID, department_name)
• With function dependencies:
i_ID dept_name
s_ID, dept_name i_ID
• In the above design we are forced to repeat the department name once for
each time an instructor participates in a dept_advisor relationship.
• To fix this, we need to decompose dept_advisor
• Any decomposition will not include all the attributes in
s_ID, dept_name i_ID
• Thus, the composition NOT be dependency-preserving
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 Example
• Consider a schema:
dept_advisor(s_ID, i_ID, dept_name)
• With function dependencies:
i_ID dept_name
s_ID, dept_name i_ID
• Two candidate keys = {s_ID, dept_name}, {s_ID, i_ID }
• We have seen before that dept_advisor is not in BCNF
• R, however, is in 3NF
• s_ID, dept_name is a superkey
• i_ID dept_name and i_ID is NOT a superkey, but:
• { dept_name} – {i_ID } = {dept_name } and
• dept_name is contained in a candidate key
Redundancy in 3NF
• Consider the schema R below, which is in 3NF
• R = (J, K, L )
• F = {JK L, L K }
• And an instance table:
• inst_phone:
• This suggests the need for higher normal forms, such as the Fourth Normal
Form (4NF)