Functional Dependencies Definition: If two tuples agree on the attributes A1, A2, … A n then they must also agree on the attributes B1, B2, … B m Formally: A1, A2, … A n B1, B2, … B m Motivating example for the study of functional dependencies: Name Social Security Number Phone Number Examples Product: name Person: ssn Company: name price, manufacturer name, age stock price, president Key of a relation is a set of attributes that: - functionally determines all the attributes of the relation - none of its subsets determines all the attributes. Superkey: a set of attributes that contains a key. Finding the Attributes of a Relation Given a relation constructed from an E/R diagram, what is its key? Rules: 1. If the relation comes from an entity set, the key of the relation is the set of attributes which is the key of the entity set. Person address name ssn Rules for Binary Relationships name Product price Person buys name ssn Several cases are possible for a binary relationship E1 - E2: 1. Many-many: the key includes the key of E1 together with the key of E2. What happens for: 2. Many-one: 3. One-one: Rules for Multiway Relationships None, really. Except: if there is an arrow from the relationship to E, then we don’t need the key of E as part of the relation key. Product Purchase Payment Method Person Store Some Properties of FD’s A1, A2, … A n B1, B2, … B m Is equivalent to A1, A2, … A n B1 A1, A2, … A n B2 … A1, A2, … A n A1, A2, … A n A i Bm Always holds. Splitting rule and Combing rule Comparing Functional Dependencies Functional dependencies: a statement about the set of allowable databases. Entailment and equivalence: comparing sets of functional dependencies Entailment: a set of functional dependencies S1 entails a set S2 if: any database that satisfies S1 much also satisfy S2. Example: {A B, entails A C B C} Equivalence: two sets of FD’s are equivalent if each entails the other. {A B, B C } is equivalent to {A B, A C, B C} Closure of a set of Attributes Given a set of attributes A and a set of dependencies C, we want to find all the other attributes that are functionally determined by A. In other words, we want to find the maximal set of attributes B, such that for every B in B, C entails A B. Closure Algorithm Start with Closure=A. Until closure doesn’t change do: if A1, A2, … A n A1, A2, … A n B is in C, and are all in the closure, and B is not in Closure then add B to closure. Example A B A D B A F C E D B Closure of {A,B}: Closure of {A, F}: Problems in Designing Schema Name Fred Fred Joe Joe Problems: - redundancy - update anomalies - deletion anomalies SSN 123-321-99 123-321-99 909-438-44 909-438-44 Phone Number (201) (206) (908) (212) 555-1234 572-4312 464-0028 555-4000 Relation Decomposition Break the relation into two relations: Name Fred Joe SSN 123-321-99 909-438-44 Name Phone Number Fred Fred Joe Joe (201) (206) (908) (212) 555-1234 572-4312 464-0028 555-4000 Decompositions in General Let R be a relation with attributes A , A , … A 1 2 n Create two relations R1 and R2 with attributes B1, B2, … B m Such that: B1, B2, … B m C1, C2, … C l C1, C2, … C l = And -- R1 is the projection of R on -- R2 is the projection of R on A1, A2, … A n B1, B2, … B m C1, C2, … C l Boyce-Codd Normal Form A simple condition for removing anomalies from relations: A relation R is in BCNF if and only if: Whenever there is a nontrivial dependency A1, A2, … A n for R , it is the case that { A , A , … A } 1 2 n a super-key for R. B In English (though a bit vague): Whenever a set of attributes of R is determining another attribute, should determine all the attributes of R. Example Name Fred Fred Joe Joe SSN 123-321-99 123-321-99 909-438-44 909-438-44 What are the dependencies? What are the keys? Is it in BCNF? Phone Number (201) (206) (908) (212) 555-1234 572-4312 464-0028 555-4000 And Now? SSN 123-321-99 909-438-44 SSN 123-321-99 123-321-99 909-438-44 909-438-44 Name Fred Joe Phone Number (201) (206) (908) (212) 555-1234 572-4312 464-0028 555-4000 What About This? Name Gizmo Price Category $19.99 gadgets Question: Find an example of a 2-attribute relation that is not in BCNF. More Decompositions Name Address Name Address Name Move-Date What’s wrong? Move-Date More Careful Strategy Find a dependency that violates the BCNF condition: A1, A2, … A n Others R1 B1, B2, … B m A’s B’s R2 Example Decomposition Name Social-security-number Age Eye Color Phone Number Functional dependencies: Name + Social-security-number Age, Eye Color What if we also had an attribute Draft-worthy, and the FD: Age Draft-worthy Decomposition Based on BCNF is Necessarily Correct Attributes A, B, C. FD: A Relations R1[A,B] R2[A,C] C Tuples in R1: (a,b) Tuples in R2: (a,c), (a,d) Tuples in the join of R1 and R2: (a,b,c), (a,b,d) Can (a,b,d) be a bogus tuple? Multivalued Dependencies Name Fred Fred Fred Fred SSN Phone Number 123-321-99 123-321-99 123-321-99 123-321-99 (206) (206) (206) (206) 572-4312 572-4312 432-8954 432-8954 The multivalued dependencies are: Name, SSN Name, SSN Phone Number Course 4th Normal form: replace FD by MVD. Course CSE-444 CSE-341 CSE-444 CSE-341