CS 405G: Introduction to Database Systems 16. Functional Dependency
9/8/2015Chen Univ of Kentucky Today’s Topic Functional Dependency. Normalization Decomposition BCNF
9/8/2015Chen Univ of Kentucky Motivation How do we tell if a design is bad, e.g., Enroll(SID, Sname, CID, Cname, grade)? This design has redundancy, because the name of an employee is recorded multiple times, once for each project the employee is taking SIDCIDSnameCnamegrade John SmithDBA 11239Ben LiuNETA 12349John SmithNETB Ben LiuDBC Susan SidhukDBB
9/8/2015Chen Univ of Kentucky SIDSname 1234John Smith 1123Ben Liu 1023Susan Sidhuk SIDCIDgrade A 11239A 12349B C B CIDCname 9NET 10DB
9/8/2015Chen Univ of Kentucky Why redundancy is bad? Waste disk space. What if we want to perform update operations to the relation INSERT an new project that no employee has been assigned to it yet. UPDATE the name of “John Smith” to “John L. Smith” DELETE the last employee who works for a certain project SIDCIDSnameCnamegrade John SmithDBA 11239Ben LiuNETA 12349John SmithNETB Ben LiuDBC Susan SidhukDBB
Functional Dependency Functional dependencies (FDs) are used to specify formal measures of the "goodness" of relational designs FDs are constraints that are derived from the meaning and interrelationships of the data attributes FDs and keys are used to define normal forms for relations 9/8/2015Chen Univ of Kentucky
9/8/2015Chen Univ of Kentucky Functional dependencies A functional dependency (FD) has the form X -> Y, where X and Y are sets of attributes in a relation R X -> Y means that whenever two tuples in R agree on all the attributes in X, they must also agree on all attributes in Y t 1 [X] = t 2 [X] t 1 [Y] = t 2 [Y] XYZ abc a?? XYZ abc ab? Must be “b” Could be anything, e.g. d XYZ abc abd
9/8/2015Chen Univ of Kentucky FD examples Address (street_address, city, state, zip) street_address, city, state -> zip zip -> city, state zip, state -> zip? This is a trivial FD Trivial FD: LHS RHS zip -> state, zip? This is non-trivial, but not completely non-trivial Completely non-trivial FD: LHS ∩ RHS = ?
Functional Dependencies 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]) 9/8/2015Chen Univ of Kentucky
9/8/2015Chen Univ of Kentucky Keys redefined using FD’s Let attr(R) be the set of all attributes of R, a set of attributes K is a (candidate) key for a relation R if K -> attr(R) - K, and That is, K is a “super key” No proper subset of K satisfies the above condition That is, K is minimal (full functional dependent) Address (street_address, city, state, zip) {street_address, city, state, zip} {street_address, city, zip} {street_address, zip} {zip} Super key Key Non-key
9/8/2015Chen Univ of Kentucky Reasoning with FDs Given a relation R and a set of FDs F Does another FD follow from F ? Are some of the FDs in F redundant (i.e., they follow from the others)? Is K a key of R? What are all the keys of R?
9/8/2015Chen Univ of Kentucky Attribute closure Given R, a set of FDs F that hold in R, and a set of attributes Z in R: The closure of Z (denoted Z + ) with respect to F is the set of all attributes {A 1, A 2, …} functionally determined by Z (that is, Z -> A 1 A 2 …) Algorithm for computing the closure Start with closure = Z If X -> Y is in F and X is already in the closure, then also add Y to the closure Repeat until no more attributes can be added
9/8/2015Chen Univ of Kentucky A more complex example WorkOn(EID, Ename, , PID, Pname, Hours) EID -> Ename, -> EID PID -> Pname EID, PID -> Hours (Not a good design, and we will see why later)
9/8/2015Chen Univ of Kentucky Example of computing closure F includes: EID -> Ename, -> EID PID -> Pname EID, PID -> Hours { PID, } + = ? Starting from: closure = { PID, } -> EID Add EID; closure is now { PID, , EID } EID -> Ename, Add Ename, ; closure is now { PID, , EID, Ename } PID -> Pname Add Pname; close is now { PID, Pname, , EID, Ename } EID, PID -> hours Add hours; closure is now all the attributes in WorksOn
9/8/2015Chen Univ of Kentucky Using attribute closure Given a relation R and set of FDs F Does another FD X -> Y follow from F ? Compute X + with respect to F If Y X +, then X -> Y follow from F Is K a super key of R? Compute K + with respect to F If K + contains all the attributes of R, K is a super key Is a super key K a key of R? Test where K’ = K – { a | a K} is a superkey of R for all possible a
9/8/2015Chen Univ of Kentucky Rules of FDs Armstrong’s axioms Reflexivity: If Y X, then X -> Y Augmentation: If X -> Y, then XZ -> YZ for any Z Transitivity: If X -> Y and Y -> Z, then X -> Z Rules derived from axioms Splitting: If X -> YZ, then X -> Y and X -> Z Combining: If X -> Y and X -> Z, then X -> YZ
9/8/2015Chen Univ of Kentucky Using rules of FD’s Given a relation R and set of FDs F Does another FD X -> Y follow from F ? Use the rules to come up with a proof Example: F includes: EID -> Ename, ; -> EID; EID, PID -> Hours, Pid -> Pname PID, -> hours? -> EID (given in F ) PID, -> PID, EID (augmentation) PID, EID -> hours (given in F ) PID, -> hours (transitivity)
9/8/2015Chen Univ of Kentucky Example of redundancy WorkOn (EID, Ename, , PID, hour) We say X -> Y is a partial dependency if there exist a X’ X such that X’ -> Y e.g. EID, -> Ename, Otherwise, X -> Y is a full dependency e.g. EID, PID -> hours EIDPIDEname PnameHours John platform Ben 12349John Susan platform40
Database normalization relates to the level of redundancy in a relational database’s structure. The key idea is to reduce the chance of having multiple different version of the same data. Well-normalized databases have a schema that reflects the true dependencies between tracked quantities. Any increase in normalization generally involves splitting existing tables into multiple ones, which must be re-joined each time a query is issued. Database Normalization 9/8/2015Chen University of Kentucky19
9/8/2015Chen University of Kentucky20 Normalization A normalization is the process of organizing the fields and tables of a relational database to minimize redundancy and dependency. A normal form is a certification that tells whether a relation schema is in a particular state
Normal Forms Edgar F. Codd originally established three normal forms: 1NF, 2NF and 3NF. 3NF is widely considered to be sufficient. Normalizing beyond 3NF can be tricky with current SQL technology as of 2005 Full normalization is considered a good exercise to help discover all potential internal database consistency problems. 9/8/2015Chen University of Kentucky21
First Normal Form ( 1NF ) NF is to characterize a relation (not an attribute, a key, etc…) We can only say “this relation or table is in 1NF” A relation is in first normal form if the domain of each attribute contains only atomic values, and the value of each attribute contains only a single value from that domain. 9/8/2015Chen University of Kentucky22
9/8/2015Chen Univ of Kentucky
9/8/2015Chen University of Kentucky24 2 nd Normal Form An attribute A of a relation R is a nonprimary attribute if it is not part of any key in R, otherwise, A is a primary attribute. R is in (general) 2 nd normal form if every nonprimary attribute A in R is not partially functionally dependent on any key of R
Redundancy Example If a key will result a partial dependency of a nonprimary attribute. e.g. EID, PID -> Ename In this case, the attribute (Ename) should be separated with its full dependency key (EID) to be a new table. So, to check whether a table includes redundancy. Try every nonprimary attribute and check whether it fully depends on any key. 9/8/2015Chen University of Kentucky25
9/8/2015Chen Univ of Kentucky
Second normal Form ( 2NF ) 2NF prescribes full functional dependency on the primary key. It most commonly applies to tables that have composite primary keys, where two or more attributes comprise the primary key. It requires that there are no non-trivial functional dependencies of a non-key attribute on a part (subset) of a candidate key. A table is said to be in the 2NF if and only if it is in the 1NF and every non-key attribute is irreducibly dependent on the primary key 9/8/2015Chen University of Kentucky27
9/8/2015Chen University of Kentucky28 Decomposition Decomposition eliminates redundancy To get back to the original relation, use natural join. EIDPIDEname PnameHours John platform Ben 12349John Susan platform40 Decomposition EIDEname 1234John 1123Ben 1023Susan EIDPIDPnameHours B2B platform CRM CRM B2B platform40 Foreign key
9/8/2015Chen University of Kentucky29 Decomposition Decomposition may be applied recursively EIDPIDPnameHours B2B platform CRM CRM B2B platform40 PIDPname 10B2B platform 9CRM EIDPIDHours
9/8/2015Chen University of Kentucky30 Unnecessary decomposition Fine: join returns the original relation Unnecessary: no redundancy is removed, and now EID is stored twice-> EIDEname 1234John 1123Ben 1023Susan EIDEname 1234John Smith 1123Ben Liu 1023Susan Sidhuk EID
9/8/2015Chen University of Kentucky31 Bad decomposition Association between PID and hours is lost Join returns more rows than the original relation EIDPIDHours EIDPID EIDHours
9/8/2015Chen University of Kentucky32 Lossless join decomposition Decompose relation R into relations S and T attrs(R) = attrs(S) attrs(T) S = π attrs(S) ( R ) T = π attrs(T) ( R ) The decomposition is a lossless join decomposition if, given known constraints such as FD’s, we can guarantee that R = S T Any decomposition gives R S T (why?) A lossy decomposition is one with R S T
9/8/2015Chen University of Kentucky33 Loss? But I got more rows-> “Loss” refers not to the loss of tuples, but to the loss of information Or, the ability to distinguish different original tuples EIDPIDHours EIDPID EIDHours
9/8/2015Chen University of Kentucky34 Questions about decomposition When to decompose How to come up with a correct decomposition (i.e., lossless join decomposition)