Copyright 2007 Ramez Elmasri and Shamkant B Navathe
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 10 - 1
Chapter 10 Functional Dependencies and Normalization for Relational Databases Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe
Chapter Outline n 1 Informal Design Guidelines for Relational Databases n n n 1. 1 Semantics of the Relation Attributes 1. 2 Redundant Information in Tuples and Update Anomalies(up normal) 1. 3 Null Values in Tuples 1. 4 Spurious Tuples 2 Functional Dependencies (FDs) n 2. 1 Definition of FD Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 10 - 3
Chapter Outline n 3 Normal Forms Based on Primary Keys n n n 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 n 4 General Normal Form Definitions (For Multiple Keys) n 5 BCNF (Boyce-Codd Normal Form) Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 10 - 4
1 Informal Design Guidelines for Relational Databases (1) n What is relational database design? n n Two levels of relation schemas n n The grouping of attributes to form "good" 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? Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 10 - 5
Informal Design Guidelines for Relational Databases (2) n n We first discuss informal guidelines for good relational design Then we discuss formal concepts of functional dependencies and normal forms n n n - 1 NF (First Normal Form) - 2 NF (Second Normal Form) - 3 NF (Third Normal Form) - BCNF (Boyce-Codd Normal Form) Additional types of dependencies, further normal forms, relational design algorithms are discussed in Chapter 11 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 10 - 6
1. 1 Semantics of the Relation Attributes n GUIDELINE 1: Informally, each tuple in a relation should represent one entity or relationship instance. (Applies to individual relations and their attributes). n n 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 separately 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. Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 10 - 7
Figure 10. 1 A simplified COMPANY relational database schema Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 10 - 8
1. 2 Redundant Information in Tuples and Update Anomalies n Information is stored redundantly n n Wastes storage Causes problems with update anomalies n n n Insertion anomalies Deletion anomalies Modification anomalies Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 10 - 9
EXAMPLE OF AN UPDATE ANOMALY n Consider the relation: n n EMP_PROJ(Emp#, Proj#, Ename, Pname, No_hours) Update Anomaly: n Changing the name of project number P 1 from “Billing” to “Customer-Accounting” may cause this update to be made for all 100 employees working on project P 1. Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 10 - 10
EXAMPLE OF AN INSERT ANOMALY n Consider the relation: n n Insert Anomaly: n n EMP_PROJ(Emp#, Proj#, Ename, Pname, No_hours) Cannot insert a project unless an employee is assigned to it. Conversely n Cannot insert an employee unless an he/she is assigned to a project. Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 10 - 11
EXAMPLE OF AN DELETE ANOMALY n Consider the relation: n n EMP_PROJ(Emp#, Proj#, Ename, Pname, No_hours) Delete Anomaly: n n When a project is deleted, it will result in deleting all the employees who work on that project. Alternately, if an employee is the lone employee on a project, deleting that employee would result in deleting the corresponding project. Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 10 - 12
Figure 10. 3 Two relation schemas suffering from update anomalies Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 10 - 13
Figure 10. 4 Example States for EMP_DEPT and EMP_PROJ Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 10 - 14
Guideline to Redundant Information in Tuples and Update Anomalies n GUIDELINE 2: n n Design a schema that does not suffer from the insertion, deletion and update anomalies. If there any anomalies present, then note them so that applications can be made to take them into account. Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 10 - 15
1. 3 Null Values in Tuples n GUIDELINE 3: n n n 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: n n n Attribute not applicable or invalid Attribute value unknown (may exist) Value known to exist, but unavailable Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 10 - 16
Lec 2 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 10 - 17
2. 1 Functional Dependencies (1) n Functional dependencies (FDs) n n Are used to specify formal measures of the "goodness" of relational designs And keys are used to define normal forms for relations 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 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 10 - 18
Functional Dependencies (2) n X -> Y holds if whenever two tuples have the same value for X, they must have the same value for Y n n For any two tuples t 1 and t 2 in any relation instance r(R): If t 1[X]=t 2[X], then t 1[Y]=t 2[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 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 10 - 19
Examples of FD constraints (1) n Social security number determines employee name n n Project number determines project name and location n n SSN -> ENAME PNUMBER -> {PNAME, PLOCATION} Employee ssn and project number determines the hours per week that the employee works on the project n {SSN, PNUMBER} -> HOURS Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 10 - 20
Examples of FD constraints (2) n n n 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 n (since we never have two distinct tuples with t 1[K]=t 2[K]) Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 10 - 21
3 Normal Forms Based on Primary Keys n n n 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 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 10 - 22
3. 1 Normalization of Relations (1) n Normalization: n n The process of decomposing unsatisfactory "bad" relations by breaking up their attributes into smaller relations Normal form: n Condition using keys and FDs of a relation to certify whether a relation schema is in a particular normal form Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 10 - 23
Normalization of Relations (2) n 2 NF, 3 NF, BCNF n n 4 NF n n based on keys and FDs of a relation schema based on keys, multi-valued dependencies : Additional properties may be needed to ensure a good relational design (lossless join, dependency preservation; Chapter 11) Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 10 - 24
3. 2 Practical Use of Normal Forms n n n Normalization is carried out in practice so that the resulting designs are of high quality and meet the popular properties The practical utility of these normal forms becomes questionable (open to discuss) when the constraints on which they are based are hard to understand or to detect The database designers need not normalize to the highest possible normal form n n (usually up to 3 NF, BCNF or 4 NF) Denormalization: n The process of storing the join of higher normal form relations as a base relation—which is in a lower normal form Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 10 - 25
3. 3 Definitions of Keys and Attributes Participating in Keys (1) n n A superkey of a relation schema R = {A 1, A 2, . . , An} is a set of attributes S subset-of R with the property that no two tuples t 1 and t 2 in any legal relation state r of R will have t 1[S] = t 2[S] A key K is a superkey with the additional property that removal of any attribute from K will cause K not to be a superkey any more. Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 10 - 26
Definitions of Keys and Attributes Participating in Keys (2) n If a relation schema has more than one key, each is called a candidate key. n n n One of the candidate keys is arbitrarily designated to be the primary key, and the others are called secondary keys. A Prime attribute must be a member of some candidate key A Nonprime attribute is not a prime attribute— that is, it is not a member of any candidate key. Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 10 - 27
3. 2 First Normal Form n Disallows n n composite attributes multivalued attributes nested relations; attributes whose values for an individual tuple are non-atomic Considered to be part of the definition of relation Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 10 - 28
Figure 10. 8 Normalization into 1 NF Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 10 - 29
Figure 10. 9 Normalization nested relations into 1 NF Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 10 - 30
Example 2 Poorly Designed Table Figure 2: Poorly Designed Table Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 1 -31
Conversion To First Normal Form (1 NF) - A table is said to be in 1 NF if it satisfies the following conditions: 1. It contains a primary key to uniquely identify each row. 2. There are no repeating groups. In other words, each row/column intersection can contain one and only one value, (not a set of values). Note that the table in Figure 2 contains repeating groups because any project number (PROJ_NUM) can have a group of several data entries. For example, the Evergreen project’s entries are shown in Figure 3: Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 1 -32
Conversion To First Normal Form (1 NF) (cont) Figure 3: Example Repeating Group Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 1 -33
Conversion To First Normal Form (1 NF) (cont) 3. All attributes are dependent on the primary key. Note that the primary key determines any other attribute in the table. Note that all tables conforming to the Relational Model we learned in Chapter 5 are in 1 NF by default. n - Figure 4 below displays the conversion of the table in Figure 2 to 1 NF by satisfying the above three conditions. Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 1 -34
Conversion To First Normal Form (1 NF) (cont) Figure 4: The Table in Figure 1 in 1 NF Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 1 -35
Conversion To First Normal Form (1 NF) (cont) Note that the table in Figure 4 satisfies the three conditions. The first condition is satisfied because the table has a composite primary key consisting of the key attributes PROJ_NUM and EMP_NUM. The secondition is satisfied because the table no longer contains repeating groups. The third condition is satisfied because any attribute is dependent on the primary key. For example, EMP_NAME is dependent on the primary key PROJ_NUM, EMP_NUM because if we know PROJ_NUM and EMP_NUM (15 and 103 for example), we can determine EMP_NAME (J. Arbough). Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 1 -36
Conversion To First Normal Form (1 NF) (cont) Dependencies between attributes for the table in Figure 4 can be identified with the help of the n dependency diagram as shown in Figure 5 below: Figure 5: A Dependency Diagram: First Normal Form (1 NF) Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 1 -37
Conversion To First Normal Form (1 NF) (cont) - Note the following about the dependency diagram in Figure 5: 1. The primary key is bold and underlined. 2. The arrows above attributes include all desirable dependencies. These dependencies can be expressed as follows: PROJ_NUM, EMP_NUM PROJ_NAME, EMP_NAME, JOB_CLASS, CHG_HOUR, HOURS Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 1 -38
Conversion To First Normal Form (1 NF) (cont) 3. The arrows below the dependency diagram indicate less desirable dependencies: Partial Dependencies: This applies to composite primary keys where the value of an attribute is only dependent on part of the primary key. For example, PROJ_NAME is dependent on PROJ_NUM, which forms part of the primary key (if we know PROJ_NUM, we can determine PROJ_NAME). Dependencies based on only a part of a composite primary key are called partial dependencies. PROJ_NUM PROJ_NAME EMP_NUM EMP_NAME, JOB_CLASS, CHG_HOUR Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 1 -39
Conversion To First Normal Form (1 NF) (cont) n Transitive Dependencies: Looking at Figure 5, note that CHG_HOUR is dependent on JOB_CLASS. Because neither CHG_HOUR nor JOB_CLASS is a key attribute, we have a condition known as transitive dependency. In other words, a transitive dependency is a dependency of one nonkey attribute on another nonkey attribute. JOB_CLASS CHG_HOUR Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 10 - 40
Lec 3. 1 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 10 - 41
3. 3 Second Normal Form (1) n n Uses the concepts of FDs, primary key Definitions n n n Prime attribute: An attribute that is member of the primary key K Full functional dependency: a FD Y -> Z where removal of any attribute from Y means the FD does not hold any more Examples: n n {SSN, PNUMBER} -> HOURS is a full FD since neither SSN -> HOURS nor PNUMBER -> HOURS hold {SSN, PNUMBER} -> ENAME is not a full FD (it is called a partial dependency ) since SSN -> ENAME also holds Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 10 - 42
Second Normal Form (2) n n A relation schema R is in second normal form (2 NF) if every non-prime attribute A in R is fully functionally dependent on the primary key R can be decomposed into 2 NF relations via the process of 2 NF normalization Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 10 - 43
Figure 10. 10 Normalizing into 2 NF Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 10 - 44
Conversion To Second Normal Form (2 NF) A table is said to be in second normal form (2 NF) if it satisfies the following conditions: It is in 1 NF. 2. It includes no partial dependencies; that is, no attribute is dependent on a portion of (part of) the primary key. - Let us convert the table in Figure 4 from 1 NF to 2 NF. To do that, follow these steps: n Write each key attribute on a separate line, then write the primary key on the last line: 1. PROJ_NUM EMP_NUM Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 1 -45
Conversion To Second Normal Form (2 NF) n The attributes on each line will become keys in a new table. In other words, the original table is now split into three tables. We’ll call these tables PROJECT, EMPLOYEE and ASSIGN respectively. The attribute(s) on each line will form the primary key for the new tables. The rest of the attributes for each table can be determined from the dependency diagram: PROJECT(PROJ_NUM, PROJ_NAME) because in the dependency diagram in Figure 5, PROJ_NUM PROJ_NAME Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 1 -46
Conversion To Second Normal Form (2 NF) EMPLOYEE(EMP_NUM, EMP_NAME, JOB_CLASS, CHG_HOUR) because in the dependency diagram in Figure 5, EMP_NUM EMP_NAME, JOB_CLASS, CHG_HOUR ASSIGN(PROJ_NUM, EMP_NUM, HOURS) because in the dependency diagram in Figure 5, PROJ_NUM, EMP_NUM HOURS - Note that the new tables are in 2 NF because each table is in 1 NF and none of the tables contain partial dependencies. The dependency diagram for the new tables is shown in Figure 6: Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 1 -47
Conversion To Second Normal Form (2 NF) Figure 6: Second Normal Form (2 NF) Conversion Results Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 1 -48
Conversion To Second Normal Form (2 NF) -Note that the conversion to 2 NF did not eliminate the transitive dependency (the conversion to 3 NF will eliminate it). - Because a partial dependency applies only to tables with composite primary keys, a table whose primary key consists of only a single attribute must automatically be in 2 NF if it is in 1 NF. Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 1 -49
LEC 3. 2 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 10 - 50
Conversion To Third Normal Form (3 NF) - To eliminate the transitive dependency, the table must be converted to 3 NF. A table is said to be in 3 NF if it satisfies the following two conditions: Ø It is in 2 NF. Ø It contains no transitive dependencies. - Note that in Figure 6, the EMPLOYEE table is the one that contains transitive dependency: Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 1 -51
Conversion To Third Normal Form (3 NF) EMPLOYEE (EMP_NUM, EMP_NAME, JOB_CLASS, CHG_HOUR) Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 1 -52
Conversion To Third Normal Form (3 NF) n To eliminate the transitive dependency, simply move the attributes causing data redundancy (namely JOB_CLASS and CHG_HOUR) into a new table called JOB. Then in the EMPLOYEE table, define a foreign key that will be used to link it to the JOB table. In this case, the foreign key will be JOB_CLASS. The result of the conversion to 3 NF will be the following tables: Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 1 -53
Conversion To Third Normal Form (3 NF) Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 1 -54
Conversion To Third Normal Form (3 NF) n - Note that the conversion to 3 NF has eliminated the original EMPLOYEE table’s transitive dependency; the tables are now said to be in third normal form (3 NF). Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 1 -55
3. 4 Third Normal Form (1) n Definition: n n Transitive functional dependency: a FD X -> Z that can be derived from two FDs X -> Y and Y -> Z Examples: n SSN -> DMGRSSN is a transitive FD n n Since SSN -> DNUMBER and DNUMBER -> DMGRSSN -> ENAME is non-transitive n Since there is no set of attributes X where SSN -> X and X -> ENAME Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 10 - 56
Third Normal Form (2) n n n A relation schema R is in third normal form (3 NF) if it is in 2 NF and no non-prime attribute A in R is transitively dependent on the primary key R can be decomposed into 3 NF relations via the process of 3 NF normalization NOTE: n n n In X -> Y and Y -> Z, with X as the primary key, we consider this a problem only if Y is not a candidate key. When Y is a candidate key, there is no problem with the transitive dependency. E. g. , Consider EMP (SSN, Emp#, Salary ). n Here, SSN -> Emp# -> Salary and Emp# is a candidate key. Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 10 - 57
Figure 10. 10 Normalizing into 3 NF Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 10 - 58
The Boyce-Codd Normal Form (BCNF (Raymond 'Ray' Boyce & Donald D. Chamberlin )) A table T is said to be in Boyce-Codd Normal Form (BCNF) if every determinant in the table is a superkey of T. Note that every table in BCNF is also in 3 NF, however, a table in 3 NF is not necessarily in BCNF (see following example). A table that is in 3 NF and not in BCNF if it contains a nonkey that is the determinant of a key attribute. This situation can be illustrated using the example in Figure 7 below: Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 1 -59
The Boyce-Codd Normal Form (BCNF) Figure 7: A Table That is In 3 NF But Not In BCNF • Note the following functional dependencies about Figure 7: A, B C, D C B Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 1 -60
The Boyce-Codd Normal Form (BCNF) • Note that the table structure in Figure 7 has no partial dependencies nor does it contain transitive dependencies (Note that C B indicates that a nonkey attribute determines a key attribute and this dependency is neither partial nor transitive). • Therefore, the table structure in Figure 7 is in 3 NF but not in BCNF because of the dependency C B. • To convert the table structure in Figure 7 to BCNF, follow the following procedure: Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 1 -61
The Boyce-Codd Normal Form (BCNF) 1. Change the primary key to A, C and create table containing A, C and D. 2. Create another table containing C as the primary key and B. Note that C will also serve as the foreign key to link this table to the table created in step 1. The conversion to BCNF is shown in Figure 8. Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 1 -62
The Boyce-Codd Normal Form (BCNF) Figure 8: The Decomposition of a Table Structure To Meet BCNF Requirements Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 1 -63
5 BCNF (Boyce-Codd Normal Form) n A relation schema R is in Boyce-Codd Normal Form (BCNF) if whenever an FD X -> A holds in R, then X is a superkey of R n Each normal form is strictly stronger than the previous one n Every 2 NF relation is in 1 NF n Every 3 NF relation is in 2 NF n Every BCNF relation is in 3 NF n There exist relations that are in 3 NF but not in BCNF n The goal is to have each relation in BCNF (or 3 NF) Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 10 - 64
END Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 10 - 65
- Slides: 65