Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 10- 1.

Similar presentations


Presentation on theme: "Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 10- 1."— Presentation transcript:

1 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 10- 1

2 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 10 Functional Dependencies and Normalization for Relational Databases

3 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 10- 3 Chapter Outline 1 Informal Design Guidelines for Relational Databases 1.1Semantics of the Relation Attributes 1.2 Redundant Information in Tuples and Update Anomalies 1.3 Null Values in Tuples 1.4 Spurious Tuples 2 Functional Dependencies (FDs) 2.1 Definition of FD 2.2 Inference Rules for FDs

4 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 10- 4 Chapter Outline 3 Normal Forms Based on Primary Keys 3.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)

5 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 10- 5 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?

6 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 10- 6 Informal Design Guidelines for Relational Databases (2) We first discuss informal guidelines for good relational design Then we discuss formal concepts of functional dependencies and normal forms - 1NF (First Normal Form) - 2NF (Second Normal Form) - 3NF (Third Normal Form) - BCNF (Boyce-Codd Normal Form)

7 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 10- 7 1.1Semantics of the Relation Attributes GUIDELINE 1: Informally, each tuple in a relation should represent one entity or relationship instance. (Applies to individual relations and their attributes). 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.

8 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 10- 8 Figure 10.1 A simplified COMPANY relational database schema

9 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 10- 9 1.2 Redundant Information in Tuples and Update Anomalies Information is stored redundantly Wastes storage Causes problems with update anomalies Insertion anomalies Deletion anomalies Modification anomalies

10 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 10- 10 EXAMPLE OF AN UPDATE ANOMALY 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.

11 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 10- 11 EXAMPLE OF AN INSERT ANOMALY Consider the relation: EMP_PROJ(Emp#, Proj#, Ename, Pname, No_hours) Insert Anomaly: Cannot insert a project unless an employee is assigned to it. Conversely Cannot insert an employee unless an he/she is assigned to a project.

12 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 10- 12 EXAMPLE OF AN DELETE ANOMALY Consider the relation: EMP_PROJ(Emp#, Proj#, Ename, Pname, No_hours) 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.

13 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 10- 13 Figure 10.3 Two relation schemas suffering from update anomalies

14 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 10- 14 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 anomalies present, then note them so that applications can be made to take them into account.

15 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 10- 15 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

16 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 10- 16 1.4 Spurious Tuples A spurious tuple is, basically, a record in a database that gets created when two tables are joined badly. In database, spurious tuples are created when two tables are joined on attributes that are neither primary keys nor foreign keys. 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.

17 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe

18 Which suppliers supplied the parts for Proj2? To get this information, we can combine the information in the two tables by the PartNo From PROJECT_PARTS, Proj2 uses P1 and P2. P2 is supplied by S2 (from tuple-2 of SUPPLIER_PARTS). However, which supplier supplied P1 to Proj2?

19 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 10- 19 2.1 Functional Dependencies (1) Functional dependencies (FDs) 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 A set of attributes X functionally determines a set of attributes Y if the value of X determines a unique value for Y

20 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 10- 20 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

21 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe SSNLastFIRST 111SmithBob 222JonesDavid 111SmithBob 333SmithJoe 444JonesDoe 555WhiteDavid Slide 10- 21 SSN  Last

22 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 10- 22 Examples of FD constraints (1) Social security number determines employee name SSN -> ENAME Project number determines project name and location PNUMBER -> {PNAME, PLOCATION} Employee ssn and project number determines the hours per week that the employee works on the project {SSN, PNUMBER} -> HOURS

23 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Functional Dependencies and Keys 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)

24 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe 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 sid is a candidate key If there are multiple candidate keys, the DB designer chooses designates one as the primary key.

25 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Reasoning about Functional Dependencies It is sometimes possible to infer new functional dependencies from a set of given functional dependencies independently from any particular instance of the relation scheme or of any additional knowledge Example: From {sid}  {first_name} and {sid}  {last_name} We can infer {sid}  {first_name, last_name}

26 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 10- 26 2.2 Inference Rules for FDs (1) – Armstrong Axiom 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 sname, sid  sname IR2. (Augmentation) If X -> Y, then XZ -> YZ (Notation: XZ stands for X U Z) crno  subj so crno, exp-grade  subj, exp-grade

27 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Armstrong inference rules – Contd. IR3. (Transitive) If X -> Y and Y -> Z, then X -> Z crno  cid and cid  subj so crno  subj 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 Slide 10- 27

28 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 10- 28 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)

29 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe 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.

30 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Computing the Closure: Algorithm F +:= F add all functional dependencies obtained by reflexivity to F+ Repeat for each functional dependency f in F+ apply the augmentation rule in each possible way to f and add the result to F+ for each pair of functional dependencies f1 and f2 in F+ if possible, apply the transitivity rule to f1 and f2 and add the result to F+ until F+ does not change any further Slide 10- 30

31 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Consider the relation scheme R(A,B,C,D) with F = {{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}, […]} Slide 10- 31

32 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Closure of a Set of Attributes For a set X of attributes, we call the closure of X (with respect to a set of functional dependencies F), noted X+, the maximum set of attributes such that X  X+ (as a consequence of F) Consider the relation scheme R(A,B,C,D) with functional dependencies {A}  {C} and {B}  {D}.  {A}+ = {A,C}  {B}+ = {B,D}  {C}+={C}  {D}+={D}  {A,B}+ = {A,B,C,D}

33 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe 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) := X Repeat X (i+1) := X (i)  Z, where Z is the set of attributes such that there exists Y  Z in F, and Y  X (i) Until X (i+1) := X (i) Return X (i+1)

34 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe 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} 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)

35 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe 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 Y  Z in F Compute Y+ with respect to G If Z  Y+ then Y  Z is in G+ And vice versa

36 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe 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


Download ppt "Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 10- 1."

Similar presentations


Ads by Google