0% found this document useful (0 votes)
5 views42 pages

DB06_Algorithms

RDBMS slide chương 6

Uploaded by

vubaohuy2903
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views42 pages

DB06_Algorithms

RDBMS slide chương 6

Uploaded by

vubaohuy2903
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 42

06

Algorithms
FIT

Outline

• Further topics in Functional Dependencies


• Properties of Relational Decompositions
• Algorithms for Relational Database Schema Design

• Ref: Chapter 14, 15

2 Design and Manage Database


FIT

Functional Dependencies

• To recollect:
• A set of attributes X functionally determines a set of attributes Y if
the value of X determines a unique value for Y.
• Our goal here is to determine the properties of functional
dependencies and to find out the ways of manipulating them.

3 Design and Manage Database


FIT

Defining Functional Dependencies

• X → Y holds if whenever two tuples have the same value for X,


they must have the same value for Y
• For any two tuples t1 and t2 in any relation instance r(R): If t1[X] =
t2[X], then t1[Y] = t2[Y]
• X → Y in R specifies a constraint on all relation instances r(R)
• Written as X → Y; can be displayed graphically on a relation
schema as in Figures in Chapter 14.
• FDs are derived from the real-world constraints on the attributes

4 Design and Manage Database


FIT

Inference Rules for FDs

• Definition: An FD X  Y is inferred from or implied by a set of


dependencies F specified on R if X  Y holds in every legal relation
state r of R; that is, whenever r satisfies all the dependencies in F,
X  Y also holds in r.

• Given a set of FDs F, we can infer additional FDs that hold


whenever the FDs in F hold

5 Design and Manage Database


FIT

• Armstrong's inference rules:


• IR1. (Reflexive) If Y subset-of X, then X → Y
• IR2. (Augmentation) If X → Y, then XZ → YZ
• (Notation: XZ stands for X U Z)
• IR3. (Transitive) If X → Y and Y → Z, then X → Z

• IR1, IR2, IR3 form a sound and complete set of inference rules
• These are rules hold and all other rules that hold can be deduced
from these

6 Design and Manage Database


FIT

• Some additional inference rules that are useful:


• Decomposition: If X → YZ, then X → Y and X → Z
• Union: If X → Y and X → Z, then X → YZ
• Psuedotransitivity: If X → Y and WY → Z, then WX → Z

• The last three inference rules, as well as any other inference rules,
can be deduced from IR1, IR2, and IR3 (completeness property)

7 Design and Manage Database


FIT

Closure

• Closure of a set F of FDs is the set F+ of all FDs that can be


inferred from F

• Closure of a set of attributes X with respect to F is the set X+ of all


attributes that are functionally determined by X

• X+ can be calculated by repeatedly applying IR1, IR2, IR3 using the


FDs in F

8 Design and Manage Database


FIT

Algorithm to determine Closure

• Algorithm 15.1. Determining X+, the Closure of X under F


• Input: A set F of FDs on a relation schema R, and a set of
attributes X, which is a subset of R.
X+ := X;
repeat
oldX+ := X+;
for each functional dependency Y  Z in F do
if X+  Y then X+ := X+  Z;
until (X+ = oldX+);

9 Design and Manage Database


FIT

• For example, consider the following relation schema about classes held at a
university in a given academic year.
CLASS ( ClassID, Course#, Instr_name, Credit_hrs, Text, Publisher,
Classroom, Capacity).
• Let F, the set of functional dependencies for the above relation include the
following f.d.s:
FD1: ClassID  Course#, Instr_name, Credit_hrs, Text, Publisher, Classroom, Capacity;
FD2: Course#  Credit_hrs;
FD3: {Course#, Instr_name}  Text, Classroom;
FD4: Text  Publisher
FD5: Classroom  Capacity
These f.d.s above represent the meaning of the individual attributes and the relationship
among them and defines certain rules about the classes.

10 Design and Manage Database


FIT

• The closures of attributes or sets of attributes for some example sets:


{Classid}+ = {Classid, Course#, Instr_name, Credit_hrs, Text, Publisher, Classroom,
Capacity } = CLASS
{Course#}+ = {Course#, Credit_hrs}
{Course#, Instr_name}+ = {Course#, Credit_hrs, Text, Publisher, Classroom, Capacity}

Note that each closure above has an interpretation that is revealing about the
attribute(s) on the left-hand-side. The closure of {Classid} + is the entire relation CLASS
indicating that all attributes of the relation can be determined from Classid and hence it
is a key.

11 Design and Manage Database


FIT

Equivalence of Sets of FDs

• Two sets of FDs F and G are equivalent if:


• Every FD in F can be inferred from G, and
• Every FD in G can be inferred from F
• Hence, F and G are equivalent if F+ =G+
• Definition (Covers):
• F covers G if every FD in G can be inferred from F
• (i.e., if G+ subset-of F+)
• F and G are equivalent if F covers G and G covers F
• There is an algorithm for checking equivalence of sets of FDs

12 Design and Manage Database


FIT

Finding Minimal Cover of F.D.s

• Just as we applied inference rules to expand on a set F of FDs to


arrive at F+, its closure, it is possible to think in the opposite
direction to see if we could shrink or reduce the set F to its
minimal form so that the minimal set is still equivalent to the
original set F.
• Definition: An attribute in a functional dependency is considered
extraneous attribute if we can remove it without changing the
closure of the set of dependencies. Formally, given F, the set of
functional dependencies and a functional dependency X  A in F ,
attribute Y is extraneous in X if Y is a subset of X, and F logically
implies (F - (X  A)  { (X – Y)  A } )

13 Design and Manage Database


FIT

Minimal Sets of FDs

• A set of FDs is minimal if it satisfies the following conditions:


• Every dependency in F has a single attribute for its right-hand side.
• We cannot remove any dependency from F and have a set of
dependencies that is equivalent to F.
• We cannot replace any dependency X  A in F with a dependency Y
 A, where Y is a proper-subset-of X and still have a set of
dependencies that is equivalent to F.

14 Design and Manage Database


FIT

• Algorithm 15.2. Finding a Minimal Cover F for a Set of Functional Dependencies E


• Input: A set of functional dependencies E.
1. Set F:=E.
2. Replace each functional dependency X → {A1, A2, ..., An} in F by the n functional
dependencies X →A1, X →A2, ..., X → An.
3. For each functional dependency X → A in F for each attribute B that is an element of X
if { {F – {X → A} } ∪ { (X – {B} ) → A} } is equivalent to F then
replace X → A with (X – {B} ) → A in F.
(* The above constitutes a removal of the extraneous attribute B from X*)
4. For each remaining functional dependency X → A in F
if {F – {X → A} } is equivalent to F, then
remove X → A from F.
(* The above constitutes a removal of the redundant dependency X  A from F *)

15 Design and Manage Database


FIT

We illustrate algorithm 15.2 with the following:


Let the given set of FDs be E : {B → A, D → A, AB → D}.We have to find the minimum cover of E.
■ All above dependencies are in canonical form; so we have completed step 1 of Algorithm 15.2
and can proceed to step 2. In step 2 we need to determine if AB → D has any redundant
attribute on the left-hand side; that is, can it be replaced by B → D or A → D?
■ Since B → A, by augmenting with B on both sides (IR2), we have BB → AB, or B → AB (i).
However, AB → D as given (ii).
■ Hence by the transitive rule (IR3), we get from (i) and (ii), B → D. Hence AB → D may be replaced
by B → D.
■ We now have a set equivalent to original E , say E′ : {B → A, D → A, B → D}.
No further reduction is possible in step 2 since all FDs have a single attribute on the left-hand
side.
■ In step 3 we look for a redundant FD in E′. By using the transitive rule on B → D and D → A, we
derive B → A. Hence B → A is redundant in E’ and can be eliminated.
■ Hence the minimum cover of E is {B → D, D → A}.

16 Design and Manage Database


FIT

• Every set of FDs has an equivalent minimal set


• There can be several equivalent minimal sets
• There is no simple algorithm for computing a minimal set of FDs
that is equivalent to a set F of FDs. The process of Algorithm 15.2
is used until no further reduction is possible.
• To synthesize a set of relations, we assume that we start with a
set of dependencies that is a minimal set
• E.g., see algorithm 15.4

17 Design and Manage Database


FIT

Design a set of relations

• The Approach of Relational Synthesis (Bottom-up Design):


• Assumes that all possible functional dependencies are known.
• First constructs a minimal set of FDs
• Then applies algorithms that construct a target set of 3NF or BCNF
relations.
• Additional criteria may be needed to ensure the set of relations in a
relational database are satisfactory (see Algorithm 15.3).

18 Design and Manage Database


FIT

• Goals:
• Lossless join property (a must)
• Algorithm 15.3 tests for general losslessness.
• Dependency preservation property
• Observe as much as possible
• Algorithm 15.5 decomposes a relation into BCNF components by
sacrificing the dependency preservation.

19 Design and Manage Database


FIT

Algorithm to determine the key of a relation

• Algorithm 15.2a Finding a Key K for R, given a set F of Functional


Dependencies
• Input: A universal relation R and a set of functional
dependencies F on the attributes of R.
1. Set K := R;
2. For each attribute A in K {
Compute (K - A)+ with respect to F;
If (K - A)+ contains all the attributes in R,
then set K := K - {A};
}

20 Design and Manage Database


FIT

Properties of Relational Decompositions

• Relation Decomposition and Insufficiency of Normal Forms:


• Universal Relation Schema:
• A relation schema R = {A1, A2, …, An} that includes all the attributes of the
database.
• Universal relation assumption:
• Every attribute name is unique.
• Decomposition:
• The process of decomposing the universal relation schema R into a set of
relation schemas D = {R1,R2, …, Rm} that will become the relational database
schema by using the functional dependencies.
• Attribute preservation condition:
• Each attribute in R will appear in at least one relation schema Ri in the
decomposition so that no attributes are “lost”.

21 Design and Manage Database


FIT

• Another goal of decomposition is to have each individual relation


Ri in the decomposition D be in BCNF or 3NF.
• Additional properties of decomposition are needed to prevent
from generating spurious tuples

22 Design and Manage Database


FIT

Dependency Preservation Property of a Decomposition:


• Definition: Given a set of dependencies F on R, the projection of F
on Ri, denoted by pRi(F) where Ri is a subset of R, is the set of
dependencies X  Y in F+ such that the attributes in X Y are all
contained in Ri.
• Hence, the projection of F on each relation schema Ri in the
decomposition D is the set of functional dependencies in F+, the
closure of F, such that all their left- and right-hand-side attributes
are in Ri.

23 Design and Manage Database


FIT

• Dependency Preservation Property:


• A decomposition D = {R1, R2, ..., Rm} of R is dependency-preserving
with respect to F if the union of the projections of F on each Ri in D is
equivalent to F; that is
((R1(F))  . . .  (Rm(F)))+ = F+

• Claim 1:
• It is always possible to find a dependency-preserving decomposition
D with respect to F such that each relation Ri in D is in 3NF.

24 Design and Manage Database


FIT

Non-additive (Lossless) Join Property of a Decomposition:


• Definition: Lossless join property: a decomposition D = {R1, R2, ..., Rm} of
R has the lossless (nonadditive) join property with respect to the set
of dependencies F on R if, for every relation state r of R that satisfies F,
the following holds, where * is the natural join of all the relations in D:
* ( R1(r), ..., Rm(r)) = r
• Note: The word loss in lossless refers to loss of information, not to loss
of tuples. In fact, for “loss of information” a better term is “addition of
spurious information”

25 Design and Manage Database


FIT

• Algorithm 15.3: Testing for Lossless Join Property


• Input: A universal relation R, a decomposition D = {R1, R2, ..., Rm} of R, and
a set F of functional dependencies.
1. Create an initial matrix S with one row i for each relation Ri in D, and one column
j for each attribute Aj in R.
2. Set S(i,j):=bij for all matrix entries. (* each bij is a distinct symbol associated with
indices (i,j) *).
3. For each row i representing relation schema Ri
{for each column j representing attribute Aj
{if (relation Ri includes attribute Aj) then set S(i,j):= aj;};};
• (* each aj is a distinct symbol associated with index (j) *)

26 Design and Manage Database


FIT

4. Repeat the following loop until a complete loop execution results in no changes to S
{for each functional dependency X Y in F
{for all rows in S which have the same symbols in the columns corresponding to attributes
in X
{make the symbols in each column that correspond to an attribute in Y be the same in all
these rows as follows:
If any of the rows has an “a” symbol for the column, set the other rows to that same “a”
symbol in the column.
If no “a” symbol exists for the attribute in any of the rows, choose one of the “b” symbols
that appear in one of the rows for the attribute and set the other rows to that same “b”
symbol in the column ;};
};
};
5. If a row is made up entirely of “a” symbols, then the decomposition has the lossless join
property; otherwise it does not.

27 Design and Manage Database


FIT

• Example
Figure 15.1 Nonadditive join test for n-ary decompositions.
(a) Case 1: Decomposition of EMP_PROJ into EMP_PROJ1 and EMP_LOCS fails test.
(b) A decomposition of EMP_PROJ that has the lossless join property.

28 Design and Manage Database


FIT

• Example
Nonadditive join test for n-ary
decompositions. (Figure 15.1)
(c) Case 2: Decomposition of
EMP_PROJ into EMP, PROJECT,
and WORKS_ON satisfies test.

29 Design and Manage Database


FIT

Test for checking non-additivity of Binary Relational


Decompositions
2.4 Testing Binary Decompositions for Non-additive Join
(Lossless Join) Property
• Binary Decomposition: Decomposition of a relation R into two
relations.
• PROPERTY NJB (non-additive join test for binary
decompositions): A decomposition D = {R1, R2} of R has the lossless
join property with respect to a set of functional dependencies F on R
if and only if either
• The f.d. ((R1 ∩ R2)  (R1- R2)) is in F+, or
• The f.d. ((R1 ∩ R2)  (R2 - R1)) is in F+.

30 Design and Manage Database


FIT

Successive Non-additive Join Decomposition:


• Claim 2 (Preservation of non-additivity in successive
decompositions):
• If a decomposition D = {R1, R2, ..., Rm} of R has the lossless (non-
additive) join property with respect to a set of functional dependencies
F on R,
• and if a decomposition Di = {Q1, Q2, ..., Qk} of Ri has the lossless (non-
additive) join property with respect to the projection of F on Ri,
• then the decomposition D2 = {R1, R2, ..., Ri-1, Q1, Q2, ..., Qk, Ri+1, ..., Rm}
of R has the non-additive join property with respect to F.

31 Design and Manage Database


FIT

• Design of 3NF Schemas:


Algorithm 15.4 Relational Synthesis into 3NF with Dependency Preservation and Non-
Additive (Lossless) Join Property
• Input: A universal relation R and a set of functional dependencies F on the
attributes of R.
1. Find a minimal cover G for F (use Algorithm 15.2).
2. For each left-hand-side X of a functional dependency that appears in G,
create a relation schema in D with attributes {X  {A1}  {A2} ...  {Ak}},
where X  A1, X  A2, ..., X  Ak are the only dependencies in G with X as left-
hand-side (X is the key of this relation).
3. If none of the relation schemas in D contains a key of R, then create one more
relation schema in D that contains attributes that form a key of R. (Use Algorithm
15.2a to find the key of R)

32 Design and Manage Database


FIT

• Design of BCNF Schemas


Algorithm 15.5: Relational Decomposition into BCNF with Lossless (non-additive) join property
• Input: A universal relation R and a set of functional dependencies F on the
attributes of R.
1. Set D := {R};
2. While there is a relation schema Q in D that is not in BCNF
do {
choose a relation schema Q in D that is not in BCNF;
find a functional dependency X  Y in Q that violates BCNF;
replace Q in D by two relation schemas (Q - Y) and (X  Y);
};

Assumption: No null values are allowed for the join attributes.

33 Design and Manage Database


FIT

Problems with Null Values and Dangling Tuples

• when some tuples have NULL values for attributes that will be used to
join individual relations in the decomposition that may lead to
incomplete results.
• E.g., see Figure 15.2(a), where two relations EMPLOYEE and
DEPARTMENT are shown. The last two employee tuples—‘Berger’ and
‘Benitez’—represent newly hired employees who have not yet been
assigned to a department (assume that this does not violate any
integrity constraints).
• If we want to retrieve a list of (Ename, Dname) values for all the
employees. If we apply the NATURAL JOIN operation on EMPLOYEE and
DEPARTMENT (Figure 15.2(b)), the two aforementioned tuples will not
appear in the result.
• In such cases, LEFT OUTER JOIN may be used. The result is shown in
Figure 15.2 (c).

34 Design and Manage Database


FIT

• Issues with NULL-value joins.


• 15.2(a) Some EMPLOYEE tuples have NULL for the join attribute
Dnum.

35 Design and Manage Database


FIT

• Issues with NULL-value joins.


• 15.2(b) Result of applying NATURAL JOIN to the EMPLOYEE and
DEPARTMENT relations.
• 15.2(c) Result of applying LEFT OUTER JOIN to EMPLOYEE and
DEPARTMENT

36 Design and Manage Database


FIT

Problems with Dangling Tuples


• Consider the decomposition of EMPLOYEE into EMPLOYEE_1 and EMPLOYEE_2
as shown in Figure 15.3 (a) and 15.3 (b).
• Their NATURAL JOIN yields the original relation EMPLOYEE in Figure 15.2(a).
• We may use the alternative representation, shown in Figure 15.3(c), where we
do not include a tuple in EMPLOYEE_3 if the employee has not been assigned a
department (instead of including a tuple with NULL for Dnum as in
EMPLOYEE_2).
• If we use EMPLOYEE_3 instead of EMPLOYEE_2 and apply a NATURAL JOIN on
EMPLOYEE_1 and EMPLOYEE_3, the tuples for Berger and Benitez will not
appear in the result; these are called dangling tuples in EMPLOYEE.

37 Design and Manage Database


FIT

• The dangling tuple problem. (a) The relation EMPLOYEE_1 (includes all attributes of
EMPLOYEE from Figure 15.2(a) except Dnum). (b) The relation EMPLOYEE_2 (includes
Dnum attribute with NULL values). (c) The relation EMPLOYEE_3 (includes Dnum
attribute but does not include tuples for which Dnum has NULL values).

38 Design and Manage Database


FIT

About Normalization Algorithms

Discussion of Normalization Algorithms:


• Problems:
• The database designer must first specify all the relevant functional
dependencies among the database attributes.
• These algorithms are not deterministic in general.
• It is not always possible to find a decomposition into relation
schemas that preserves dependencies and allows each relation
schema in the decomposition to be in BCNF (instead of 3NF as in
Algorithm 15.5).

39 Design and Manage Database


FIT

40 Design and Manage Database


FIT

41 Design and Manage Database


FIT

42 Design and Manage Database

You might also like