Chapter Outline Informal Design Guidelines for Relational Databases
Chapter Outline Informal Design Guidelines for Relational Databases Functional Dependencies Normal Forms Based on Primary Keys General Normal Form Definitions (For Multiple Keys) BCNF (Boyce-Codd Normal Form)
Informal Design Guidelines for Relational Databases - 1 • What is relational database design? – The grouping of attributes to form "good" relation schemas • There are two levels of relation schemas – The logical "user view" level – The storage "base relation" level • DB Design is concerned mainly with base relations • ER design: – does not result in a unique database schema – does not provide a way of evaluating alternative schemas
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 – – 1 NF (First Normal Form) 2 NF (Second Normal Form) 5 NF (Third Normal Form) BCNF (Boyce-Codd Normal Form)
Problems with Redundancy • Dependencies between attributes cause redundancy – Ex. All addresses in the same town have the same zip code SSN Name Town Zip 1254 Joe Stony Brook 11790 4521 Mary Stony Brook 11790 5454 Tom Stony Brook 11790 …………………. Redundancy
Problems with Redundancy • Set valued attributes in the E-R diagram result in multiple rows in corresponding table • Example: Person (SSN, Name, Address, Hobbies) – A person entity with multiple hobbies yields multiple rows in table Person • Hence, the association between Name and Address for the same person is stored redundantly – SSN is key of entity set, but (SSN, Hobby) is key of corresponding relation • The relation Person can’t describe people without hobbies
Example ER Model Relational Model SSN Name Address Hobby 1111 Joe 125 Main biking 1111 Joe 125 Main hiking ……………. Redundancy
Anomalies • Redundancy leads to anomalies: – Update anomaly: A change in Address must be made in several places – Deletion anomaly: Suppose a person gives up all hobbies. Do we: • Set Hobby attribute to null? No, since Hobby is part of key • Delete the entire row? No, since we lose other information in the row – Insertion anomaly: Hobby value must be supplied for any inserted row since Hobby is part of key
Solution: Decomposition • Use two relations to store Person information – Person 1 (SSN, Name, Address) – Hobbies (SSN, Hobby) • The decomposition is more general: people with and without hobbies can now be described • No update anomalies: – Name and address stored once – A hobby can be separately supplied or deleted • But, there is still redundancy in the schema
A “Better” Solution • This is the ultimate redundancy eliminator • What is wrong with it?
Exercise: Create an ER Diagram Student Name Filibeck, James Fujii, Bryce Gasilos, Daphne Hasegawa, Kyle Hatakenaka, Garrett Hiraoka, Sherwin Hytowitz, Jonathan Izuka, Brandon Jung, Minho Kanehira, Jon Kobayashi, Erin Miyake, Brian Mouzourakis, Carmen Muraoka, Justin Nakama, Robert Nishimoto, Kevin Sahara, Kevin Shah, Hosneara Shimabukuro, Kyle Takahashi, Yoshio Tauyan, Craig Teves, Kimberly Tsutsumi, Matthew Uehara, Andrew Willing, Terri Yoshizumi, Brett Student ID 15245134 41264609 72458325 90259728 81814624 61567325 90426949 04882790 11585921 21852401 85429040 78551204 25459697 87426026 84214824 65250907 25417029 09528412 54528259 75642432 76217422 54519160 78641427 12522831 25414995 66451148 Email filibeck@hawaii. edu brycef@hawaii. edu daphneg@hawaii. edu kylehase@hawaii. edu ghataken@hawaii. edu shiraoka@hawaii. edu hytowitz@hawaii. edu bizuka@hawaii. edu minho@hawaii. edu jkanehir@hawaii. edu emkobaya@hawaii. edu miyakeb@hawaii. edu mouzoura@hawaii. edu justinmu@hawaii. edu rnakama@hawaii. edu kmnishim@hawaii. edu saharak@hawaii. edu hosneara@hawaii. edu kkshimab@hawaii. edu yoshiot@hawaii. edu ctauyan@hawaii. edu teveskim@hawaii. edu mtsut@hawaii. edu ueharaa@hawaii. edu twilling@hawaii. edu byoshizu@hawaii. edu Group Unpossible Fetch DB Specialists Unpossible Spanning Weak. Sauce Spanning Fetch DB Specialists Spanning DB Specialists Fetch Weak. Sauce Unpossible Weak. Sauce Spanning Unpossible Weak. Sauce Topic ADDIE TRAC Library Kalaheo ADDIE Lightweight CRM Church. Online Lightweight CRM TRAC Library Kalaheo Lightweight CRM Kalaheo TRAC Library Church. Online ADDIE Church. Online Lightweight CRM ADDIE Church. Online
Semantics of Relation Attributes GUIDELINE 1: • 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. • Design a schema that can be explained easily relation by relation. The semantics of attributes should be easy to interpret.
A Simplified COMPANY Schema
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 (SSN, PNum, EName, PName, Num. Hours) • Update Anomaly: 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.
Example of an Update Anomaly - 2 • Insert Anomaly: – Cannot insert a project unless an employee is assigned to it. – Cannot insert an employee unless 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.
Two Relation Schemas Suffering From Update Anomalies
Guideline for Redundant Information in Tuples and Update Anomalies GUIDELINE 2 • Design a schema that does not suffer from insertion, deletion, and update anomalies. • If there any anomalies, note them so that applications can take them into account.
Null Values in Tuples GUIDELINE 3 • Relations should be designed such that their tuples will have as few NULL values as possible • Reasons for nulls: – attribute not applicable or invalid – attribute value unknown (may exist) – value known to exist, but unavailable • NULLs cause problems for COUNT, SUM, etc. • Attributes that are NULL frequently could be placed in separate relations (with the primary key)
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 • Relations should be designed to satisfy the lossless join condition. No spurious tuples should be generated by doing a natural join of any relations. • Only join on equality with primary or foreign keys.
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 • 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 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) • FDs are derived from the real-world constraints on the attributes
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 determine the hours per week that the employee works on the project {SSN, PNUMBER} -> HOURS
Examples of FD Constraints - 2 • 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 t 1[K]=t 2[K])
Normal Forms Based on Primary Keys • Normalization of Relations • Practical Use of Normal Forms • Definitions of Keys and Attributes Participating in Keys • First Normal Form • Second Normal Form • Third Normal Form
Normalization of Relations - 1 • Normalization: The process of decomposing unsatisfactory "bad" relations by breaking up their attributes into smaller relations • Normal form: A condition using keys and FDs of a relation to certify whether a relation schema is in a particular normal form
Normalization of Relations - 2 • 2 NF, 3 NF, BCNF based on keys and FDs of a relation schema • 4 NF based on keys, multi-valued dependencies • 5 NF based on keys, join dependencies • Additional properties are often needed to ensure a good relational design (lossless join, dependency preservation)
Practical Use of Normal Forms • Normalization is carried out in practice so that the resulting designs meet the desirable properties • The practical utility of these normal forms becomes questionable when the constraints on which they are based are hard to understand or to detect • DB designers need not normalize to the highest possible normal form (usually up to 3 NF, BCNF or 4 NF is ok) • Denormalization: the process of storing the join of higher normal form relations as a base relation—which is in a lower normal form
Definitions of Keys and Attributes Participating in Keys - 1 • 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.
Definitions of Keys and Attributes Participating in Keys - 2 • If a relation schema has more than one key, each is called a candidate key. 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.
First Normal Form No complex attributes: A Relation is in First Normal Form if it disallows composite attributes, multivalued attributes, and nested relations: attributes whose values for an individual tuple are non-atomic • This is considered to be part of the definition of relation
Normalization into 1 NF
Normalization nested relations into 1 NF
Second Normal Form - 1 No partial dependencies: 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 • Definitions: – Prime attribute - 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 • A Relation R can be decomposed into 2 NF relations via the process of 2 NF normalization
Second Normal Form - 2 Examples: - {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 a partial dependency ) since SSN -> ENAME also holds
Normalizing into 2 NF and 3 NF
Normalizing into 2 NF and 3 NF
Third Normal Form - 1 No transitive dependencies: A relation schema R is in third normal form (3 NF) if it is in 2 NF and no nonprime attribute A in R is transitively dependent on the primary key. • Transitive functional dependency: a FD X -> Z that can be derived from two FDs X -> Y and Y -> Z Examples: – SSN -> DMGRSSN is a transitive FD since SSN -> DNUMBER and DNUMBER -> DMGRSSN hold – SSN -> ENAME is non-transitive since there is no set of attributes X where SSN -> X and X -> ENAME
Third Normal Form - 2 • R can be decomposed into 3 NF relations via the process of 3 NF normalization • NOTE: 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 ). Here, SSN -> Emp# -> Salary but Emp# is a candidate key, so it’s okay.
General Normal Form Definitions (Multiple Keys) - 1 • The above definitions consider the primary key only • The following more general definitions take into account relations with multiple candidate keys • A relation schema R is in second normal form (2 NF) if every non-prime attribute A in R is fully functionally dependent on every key of R
General Normal Form Definitions-2 • Definition: – Superkey of relation schema R - a set of attributes S of R that contains a key of R • A relation schema R is in third normal form (3 NF) if whenever a FD X -> A holds in R, then either: (a) X is a superkey of R, or (b) A is a prime attribute of R NOTE: Boyce-Codd normal form disallows condition (b)
BCNF (Boyce-Codd Normal Form) 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 • Each normal form is strictly stronger than the previous one – Every 2 NF relation is in 1 NF – Every 3 NF relation is in 2 NF – Every BCNF relation is in 3 NF • There exist relations that are in 3 NF but not in BCNF • The goal, in DB design, is to have each relation in BCNF (or 3 NF)
Boyce-Codd Normal Form
Achieving BCNF by Decomposition - 1 • Two FDs exist in the relation TEACH: fd 1: {student, course} -> instructor fd 2: instructor -> course • {student, course} is a candidate key for this relation and that the dependencies shown follow the pattern in Figure b (previous slide). So this relation is in 3 NF but not in BCNF • A relation NOT in BCNF should be decomposed to meet this property, while possibly forgoing the preservation of all functional dependencies in the decomposed relations.
Achieving BCNF by Decomposition - 2 • Three possible decompositions for relation TEACH – {student, instructor} and {student, course} – {course, instructor } and {course, student} – {instructor, course } and {instructor, student} • All 3 decompositions will lose fd 1. • Out of the above three, only the 3 rd decomposition will not generate spurious tuples after join (and hence has the non-additivity, or lossless join, property).
Summary of Normal Forms NF Test Remedy 1 NF Relation should have no nonatomic attributes or nested relations. Form new relations for each nonatomic attribute or nested relation. 2 NF For relns where PK contains multiple attrs, no nonkey attr should be functionally dependent on a part of the PK. Decompose and set up a new relns for each partial key with its dependent attr(s). Make sure to keep a reln with the original PK and any attrs that are fully functionally dependent on it. 3 NF Reln should not have a nonkey attr functionally determined by another nonkey attr (or set of nonkey attrs). There should be no transitive dependency of a nonkey attr on the PK. Decompose and set up a reln that includes the nonkey attr(s) that functionally determine(s) other nonkey attr(s).
Exercise • This table represents the hours worked per week for temporary staff at each branch of a company. Staff# Branch Address Name Position Hrs/Week S 4555 B 002 City Center Plaza, Seattle, WA 98122 Ellen Layman Assistant 16 S 4555 B 004 16 -14 th Ave. , Seattle, WA 98128 Ellen Layman Assistant 9 S 4612 B 002 City Center Plaza, Seattle, WA 98122 Dave Sinclair Assistant 14 S 4612 B 004 16 -14 th Ave. , Seattle, WA 98128 Dave Sinclair Assistant 10 • Identify the functional dependencies represented by the data shown in the table. State any assumptions.
Exercise - 2 • Using the functional dependencies you just identified, describe and illustrate the process of normalization by converting the table to 3 NF. • Identify the primary and foreign keys in your resulting relations.
- Slides: 47