Relational Database Design Algorithms and Further Dependencies.
FUNCTIONAL DEPENDENCIES. Chapter Outline 1 Informal Design Guidelines for Relational Databases...
-
Upload
lindsay-butler -
Category
Documents
-
view
222 -
download
0
Transcript of FUNCTIONAL DEPENDENCIES. Chapter Outline 1 Informal Design Guidelines for Relational Databases...
FUNCTIONAL DEPENDENCIES
Chapter Outline
1 Informal Design Guidelines for Relational Databases1.1Semantics of the Relation Attributes1.2 Redundant Information in Tuples and Update Anomalies1.3 Null Values in Tuples1.4 Spurious Tuples
2 Functional Dependencies (FDs)2.1 Definition of FD2.2 Inference Rules for FDs2.3 Equivalence of Sets of FDs2.4 Minimal Sets of FDs
Chapter Outline(contd.)
3 Normal Forms Based on Primary Keys3.1 Normalization of Relations
3.2 Practical Use of Normal Forms
3.3 Definitions of Keys and Attributes Participating in Keys
3.4 First Normal Form
3.5 Second Normal Form
3.6 Third Normal Form
4 General Normal Form Definitions (For Multiple Keys)
5 BCNF (Boyce-Codd Normal Form)
1 Informal Design Guidelines for
Relational Databases (1)
What is relational database design?
The grouping of attributes to form "good" relation schemas
Two levels of relation schemas The logical "user view" level The storage "base relation" level
Design is concerned mainly with base relations
What are the criteria for "good" base relations?
1.1 Semantics of the Relation Attributes
GUIDELINE 1: Informally, each tuple in a relation should represent one entity or relationship instance.
Attributes of different entities (EMPLOYEEs, DEPARTMENTs, PROJECTs) should not be mixed in the same relation
Only foreign keys should be used to refer to other entities
Entity and relationship attributes should be kept apart as much as possible.
Bottom Line: Design a schema that can be explained easily relation by relation. The semantics of attributes should be easy to interpret.
Figure 10.1 A simplified COMPANY relational database schema
Note: The above figure is now called Figure 10.1 in Edition 4
1.2 Redundant Information in Tuples and Update
Anomalies
Mixing attributes of multiple entities may cause problems
Information is stored redundantly wasting storage
Problems with update anomalies Insertion anomalies Deletion anomalies Modification anomalies
EXAMPLE OF AN UPDATE ANOMALY (1) Consider the relation:EMP_PROJ ( Emp#, Proj#, Ename, Pname,
No_hours)
Update Anomaly: Changing the name of
project number P1 from “Billing” to “Customer-Accounting” may cause this update to be made for all 100 employees working on project P1.
EXAMPLE OF AN UPDATE ANOMALY (2)
Insert Anomaly: Cannot insert a project unless an employee is assigned to .
Inversely - Cannot insert an employee unless an he/she is assigned to a project.
Delete Anomaly: When a project is deleted, it will result in deleting all the employees who work on that project. Alternately, if an employee is the sole employee on a project, deleting that employee would result in deleting the corresponding project.
Figure 10.3 Two relation schemas suffering from update anomalies
Figure 10.4 Example States for EMP_DEPT and EMP_PROJ
Note: The above figure is now called Figure 10.4 in Edition 4
Guideline to Redundant Information in Tuples and Update Anomalies
GUIDELINE 2: Design a schema that does not suffer from the insertion, deletion and update anomalies.
If there are any present, then note them so that applications can be made to take them into account
1.3 Null Values in Tuples
GUIDELINE 3: Relations should be designed such that their tuples will have as few NULL values as possible
Attributes that are NULL frequently could be placed in separate relations (with the primary key)
Reasons for nulls: attribute not applicable or invalid attribute value unknown (may exist) value known to exist, but unavailable
1.4 Spurious Tuples
Bad designs for a relational database may result in erroneous results for certain JOIN operations
The "lossless join" property is used to guarantee meaningful results for join operations
GUIDELINE 4: The relations should be designed to satisfy the lossless join condition.
No spurious tuples should be generated by doing a natural-join of any relations.
Consider EMP_PROJ relation of Case II, and its relation state
Let us split EMP_PROJ into 2 relation EMP_PROJ1 and EMP_LOCS as shown
below.
• A tuple in EMP_LOCS means that the employee whose name is ENAME
works on some project whose location is PLOCATION
• A tuple in EMP_PROJ1 means that the employee whose social security number is SSN works HOURS per week on the project whose name, number, and location are PNAME, PNUMBER, and PLOCATION
Now produce tuples for EMP_LOCS by applying project operation to
EMP_PROJ relation state , So we get tuples as shown below
Tuples for EMP_LOCS obtained by applying Project operation to EMP_PROJ
Now produce tuples for EMP_PROJ1 by applying project operation to
EMP_PROJ relation state , So we get tuples as shown below
Tuples for EMP_PROJ1 obtained by applying Project operation to EMP_PROJ
Now apply natural join operation (*) on EMP_PROJ1 and EMP_LOCS to get the original relation EMP_PROJ. EMP_PROJ1 * EMP_LOCS
• Additional tuples that were not in EMP_PROJ are called spurious tuples
because they represent spurious or wrong information that is not
valid. The spurious tuples are marked by asterisks (*).
• Decomposing EMP_PROJ into EMP_LOCS and EMP_PROJ! is undesirable
because, when we JOIN them back using NATURAL JOIN, we do not
get the correct original information. This is because PLOCATION
is neither a primary key nor a foreign key in either EMP_LOCS or
EMP_PROJ1
Spurious Tuples (2)
There are two important properties of decompositions:
(a) non-additive or losslessness of the corresponding join
(b) preservation of the functional dependencies.
Note :property (a) is extremely important and cannot be sacrificed.
Property (b) is less stringent and may be sacrificed.
Functional Dependencies
2.1 Functional Dependencies (1)
Functional dependencies (FDs) are used to specify formal measures of the "goodness" of relational designs
FDs and keys are used to define normal forms for relations
FDs are constraints that are derived from the meaning and interrelationships of the data attributes
X->Y : A set of attributes X functionally determines a set of attributes Y if the value of X determines a unique value for Y.
Functional Dependencies (2)
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. ( denoted by the arrow->).
FDs are derived from the real-world constraints on the attributes
Examples of FD constraints (1)
social security number determines employee nameSSN -> ENAME
project number determines project name and locationPNUMBER -> {PNAME, PLOCATION}
employee ssn and project number determines the hours per week that the employee works on the project{SSN, PNUMBER} -> HOURS
From the semantics of the attributes, we know that the following functional dependencies should hold:
FD1 : {SSN, PNUMBER} HOURS
FD2 : SSN ENAME
FD3 : PNUMBER {PNAME, PLOCATION}
DIAGRAMMATIC NOTATION FOR DISPLAYING FDs
A FD cannot be inferred automatically from a given relation state r; but must be defined
explicitly by someone who knows the semantics of the attributes of R.
Example:
TEXT(Author) COURSE(Subject) . Is it true for all legal states of TEACH?
YES
But we can definitely say TEACHER COURSE is not true.
FDs – NOT A PROPERTY OF A RELATION STATE
Important….
An FD is a property of the attributes in the schema R The constraint must hold on every relation instance r(R) If K is a key of R, then K functionally determines all
attributes in R (since we never have two distinct tuples with t1[K]=t2[K])
2.2 Inference Rules for FDs (1)
Given a set of FDs F, we can infer additional FDs that hold whenever the FDs in F hold
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 or {X, Z})
IR3. (Transitive) If X -> Y and Y -> Z, then X -> Z
IR1, IR2, IR3 form a sound and complete set of inference rules
Inference Rules for FDs (2)
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)
Given R = (A, B, C, G, H, I) F = { A B A C
CG HCG I
B H }
Based on Inference Rules, some members of F+ would be
A H (transitivity from A B and B H)
AG I (augmenting A C with G, to get AG CG and then transitivity with CG I )
CG HI (adding CG I and CG H to infer CG HI)
INFERENCE RULES - EXAMPLE
Inference Rules for FDs (3)
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
2.3 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: 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
1. Consider the following two sets of functional dependencies:
F = {A C, AC D, E AD, E H}
G = {A CD, E AH}.
Check whether they are equivalent.
2. Consider two sets of FDs, F and G,
F = {A B, B C, AC D} and
G = {A B, B C, A D}
Are F and G equivalent?3. F = {A B, A C} G = {A B, B C} Are F and G equivalent?
PRACTICE - EQUIVALENCE OF SETS OF FUNCTIONAL DEPENDENCIES
Closure of a Set of Functional Dependencies
For a set F of functional dependencies, we call the closure of F, noted F+, the set of all the functional dependencies that can be derived from F (by the application of Armstrong’s axioms). Intuitively, F+ is equivalent to F, but it contains some additional
FDs that are only implicit in F.
Consider the relation scheme R(A,B,C,D) withF = {{A} {B},{B,C} {D}}F+ = {
{A} {A}, {B}{B}, {C}{C}, {D}{D}, {A,B}{A,B}, […], {A}{B}, {A,B}{B}, {A,D}{B,D}, {A,C}{B,C}, {A,C,D}{B,C,D}, {A} {A,B}, {A,D}{A,B,D}, {A,C}{A,B,C}, {A,C,D}{A,B,C,D}, {B,C} {D}, […], {A,C} {D}, […]}
Algorithm for Computing the Closure of a Set of Attributes
Input: R a relation scheme F a set of functional dependencies X R (the set of attributes for which we want to compute
the closure)Output:
X+ the closure of X w.r.t. F
X(0) := XRepeat
X(i+1) := X(i) Z, where Z is the set of attributes such that there exists YZ in F, and Y X(i)
Until X(i+1) := X(i)
Return X(i+1)
Closure of a Set of Attributes: Example
R = {A,B,C,D,E,G}
F = { {A,B}{C}, {C}{A}, {B,C}{D}, {A,C,D}{B}, {D}{E,G}, {B,E}{C}, {C,G}{B,D}, {C,E}{A,G}}
X = {B,D}
Find X+
X(0) = {B,D} {D}{E,G},
X(1) = {B,D,E,G}, {B,E}{C}
X(2) = {B,C,D,E,G}, {C,E}{A,G}
X(3) = {A,B,C,D,E,G}
X(4) = X(3)
Uses of Attribute Closure
There are several uses of the attribute closure algorithm: Testing for superkey
To test if X is a superkey, we compute X+, and check if X+ contains all attributes of R. X is a candidate key if none of its subsets is a key.
Testing functional dependencies To check if a functional dependency X Y holds (or, in other words, is in
F+), just check if Y X+. Computing the closure of F
For each subset X R, we find the closure X+, and for each Y X+, we output a functional dependency X Y.
Computing if two sets of functional dependencies F and G are equivalent, i.e., F+ = G+ For each functional dependency YZ in F
Compute Y+ with respect to G If Z Y+ then YZ is in G+
And vice versa
Redundancy of FDs
Sets of functional dependencies may have redundant dependencies that can be inferred from the others {A}{C} is redundant in: {{A}{B}, {B}{C},{A} {C}}
Parts of a functional dependency may be redundant Example of extraneous/redundant attribute on RHS: {{A}{B}, {B}{C}, {A}{C,D}} can be simplified to {{A}{B}, {B}{C}, {A}{D}} (because {A}{C} is inferred from {A} {B}, {B}{C})
Example of extraneous/redundant attribute on LHS: {{A}{B}, {B}{C}, {A,C}{D}} can be simplified to {{A}{B}, {B}{C}, {A}{D}} (because of {A}{C})
2.4 Minimal Sets of FDs (1)
A set of FDs is minimal if it satisfies the following conditions:
(1) Every dependency in F has a single attribute for its RHS.
(2) We cannot remove any dependency from F and have a set of dependencies that is equivalent to F.
(3) We cannot replace any dependency X -> A in F with a dependency Y -> A, where Y proper-subset-of X ( Y subset-of X) and still have a set of dependencies that is equivalent to F.
Minimal Sets of FDs (2)
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
Canonical Cover
A canonical cover for F is a set of dependencies Fc such that F and Fc,are equivalent
Fc contains no redundancy
Each left side of functional dependency in Fc is unique.
For instance, if we have two FD XY, XZ, we convert them to XYZ.
Algorithm for canonical cover of F:repeat
Use the union rule to replace any dependencies in F X1 Y1 and X1 Y2 with X1 Y1 Y2
Find a functional dependency X Y with an extraneous attribute either in X or in Y
If an extraneous attribute is found, delete it from X Y until F does not change
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
R = (A, B, C)F = {A BC
B C A BAB 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 because of B C. Set is now {A BC, B C}
C is extraneous in A BC because of A B and B C. The canonical cover is: A B
B C
Keys and FDs
A FD is a generalization of the notion of a key.
For Student (sid, name, supervisor_id, specialization), we write:
{sid} {name, supervisor_id, specialization} The sid determines all attributes (i.e., the entire record) If two tuples in the relation student have the same sid,
then they must have the same values on all attributes. In other words they must be the same tuple (since the
relational model does not allow duplicate records)
Superkeys and Candidate Keys A set of attributes that determine the entire tuple is a
superkey {sid, name} is a superkey for the student table. Also {sid, name, supervisor_id} etc.
A minimal set of attributes that determines the entire tuple is a candidate key {sid, name} is not a candidate key because I can remove the
name. sid is a candidate key
If there are multiple candidate keys, the DB designer chooses designates one as the primary key.
Finding Keys
Example: Consider the relation scheme R(A,B,C,D) with functional dependencies {A}{C} and {B}{D}.
Is {A,B} a candidate key?
For {A,B} to be a candidate key, it must determine all attributes (i.e., be a superkey) be minimal
{A,B} is a superkey because: {A}{C} {A,B}{A,B,C} (augmentation by AB)
{B}{D} {A,B,C}{A,B,C,D} (augmentation by A,B,C)
We obtain {A,B}{A,B,C,D} (transitivity)
{A,B} is minimal because neither {A} nor {B} alone are candidate keys
Pitfalls in Relational Database Design
Functional dependencies can be used to refine ER diagrams or independently (i.e., by performing repetitive decompositions on a "universal" relation that contains all attributes).
Relational database design requires that we find a “good” collection of relation schemas. A bad design may lead to Repetition of Information. Inability to represent certain information.
Design Goals: Avoid redundant data Ensure that relationships among attributes are represented Facilitate the checking of updates for violation of database integrity
constraints.
Example of Bad Design Consider the relation schema: Lending-schema = (branch-name, branch-
city, assets, customer-name, loan-number, amount) where: {branch-name}{branch-city, assets}
Bad Design Wastes space. Data for branch-name, branch-city, assets are repeated
for each loan that a branch makes Complicates updating, introducing possibility of inconsistency of
assets value Difficult to store information about a branch if no loans exist. Can use
null values, but they are difficult to handle.
Usefulness of FDs
Use functional dependencies to 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 a set of relations {R1, R2, ..., Rn} such that each relation is in good form the decomposition is a lossless-join decomposition if possible, preserve dependencies
In our example the problem occurs because there FDs ({branch-name}{branch-city, assets}) where the LHS is not a key
Solution: decompose the relation schema Lending-schema into: Branch-schema = (branch-name, branch-city,assets) Loan-info-schema = (customer-name, loan-number, branch-name,
amount)