Download presentation
Presentation is loading. Please wait.
Published byMoris Singleton Modified over 9 years ago
1
CS 405G: Introduction to Database Systems 16. Functional Dependency
2
9/8/2015Chen Qian @ Univ of Kentucky Today’s Topic Functional Dependency. Normalization Decomposition BCNF
3
9/8/2015Chen Qian @ 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 123410John SmithDBA 11239Ben LiuNETA 12349John SmithNETB 112310Ben LiuDBC 102310Susan SidhukDBB
4
9/8/2015Chen Qian @ Univ of Kentucky SIDSname 1234John Smith 1123Ben Liu 1023Susan Sidhuk SIDCIDgrade 123410A 11239A 12349B 112310C 102310B CIDCname 9NET 10DB
5
9/8/2015Chen Qian @ 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 123410John SmithDBA 11239Ben LiuNETA 12349John SmithNETB 112310Ben LiuDBC 102310Susan SidhukDBB
6
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 Qian @ Univ of Kentucky
7
9/8/2015Chen Qian @ 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
8
9/8/2015Chen Qian @ 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 = ?
9
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 Qian @ Univ of Kentucky
10
9/8/2015Chen Qian @ 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
11
9/8/2015Chen Qian @ 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?
12
9/8/2015Chen Qian @ 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
13
9/8/2015Chen Qian @ Univ of Kentucky A more complex example WorkOn(EID, Ename, email, PID, Pname, Hours) EID -> Ename, email email -> EID PID -> Pname EID, PID -> Hours (Not a good design, and we will see why later)
14
9/8/2015Chen Qian @ Univ of Kentucky Example of computing closure F includes: EID -> Ename, email email -> EID PID -> Pname EID, PID -> Hours { PID, email } + = ? Starting from: closure = { PID, email } email -> EID Add EID; closure is now { PID, email, EID } EID -> Ename, email Add Ename, email; closure is now { PID, email, EID, Ename } PID -> Pname Add Pname; close is now { PID, Pname, email, EID, Ename } EID, PID -> hours Add hours; closure is now all the attributes in WorksOn
15
9/8/2015Chen Qian @ 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
16
9/8/2015Chen Qian @ 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
17
9/8/2015Chen Qian @ 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, email; email -> EID; EID, PID -> Hours, Pid -> Pname PID, email -> hours? email -> EID (given in F ) PID, email -> PID, EID (augmentation) PID, EID -> hours (given in F ) PID, email -> hours (transitivity)
18
9/8/2015Chen Qian @ Univ of Kentucky Example of redundancy WorkOn (EID, Ename, email, PID, hour) We say X -> Y is a partial dependency if there exist a X’ X such that X’ -> Y e.g. EID, email -> Ename, email Otherwise, X -> Y is a full dependency e.g. EID, PID -> hours EIDPIDEnameemailPnameHours 123410John Smithjsmith@ac.comB2B platform10 11239Ben Liubliu@ac.comCRM40 12349John Smithjsmith@ac.comCRM30 102310Susan Sidhukssidhuk@ac.comB2B platform40
19
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 Qian @ University of Kentucky19
20
9/8/2015Chen Qian @ 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
21
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 Qian @ University of Kentucky21
22
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 Qian @ University of Kentucky22
23
9/8/2015Chen Qian @ Univ of Kentucky
24
9/8/2015Chen Qian @ 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
25
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 Qian @ University of Kentucky25
26
9/8/2015Chen Qian @ Univ of Kentucky
27
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 Qian @ University of Kentucky27
28
9/8/2015Chen Qian @ University of Kentucky28 Decomposition Decomposition eliminates redundancy To get back to the original relation, use natural join. EIDPIDEnameemailPnameHours 123410John Smithjsmith@ac.comB2B platform10 11239Ben Liubliu@ac.comCRM40 12349John Smithjsmith@ac.comCRM30 102310Susan Sidhukssidhuk@ac.comB2B platform40 Decomposition EIDEnameemail 1234John Smithjsmith@ac.com 1123Ben Liubliu@ac.com 1023Susan Sidhukssidhuk@ac.com EIDPIDPnameHours 123410B2B platform10 11239CRM40 12349CRM30 102310B2B platform40 Foreign key
29
9/8/2015Chen Qian @ University of Kentucky29 Decomposition Decomposition may be applied recursively EIDPIDPnameHours 123410B2B platform10 11239CRM40 12349CRM30 102310B2B platform40 PIDPname 10B2B platform 9CRM EIDPIDHours 123410 1123940 1234930 10231040
30
9/8/2015Chen Qian @ University of Kentucky30 Unnecessary decomposition Fine: join returns the original relation Unnecessary: no redundancy is removed, and now EID is stored twice-> EIDEnameemail 1234John Smithjsmith@ac.com 1123Ben Liubliu@ac.com 1023Susan Sidhukssidhuk@ac.com EIDEname 1234John Smith 1123Ben Liu 1023Susan Sidhuk EIDemail 1234jsmith@ac.com 1123bliu@ac.com 1023ssidhuk@ac.com
31
9/8/2015Chen Qian @ University of Kentucky31 Bad decomposition Association between PID and hours is lost Join returns more rows than the original relation EIDPIDHours 123410 1123940 1234930 10231040 EIDPID 123410 11239 12349 102310 EIDHours 123410 112340 123430 102340
32
9/8/2015Chen Qian @ 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
33
9/8/2015Chen Qian @ 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 123410 1123940 1234930 10231040 EIDPID 123410 11239 12349 102310 EIDHours 123410 112340 123430 102340
34
9/8/2015Chen Qian @ University of Kentucky34 Questions about decomposition When to decompose How to come up with a correct decomposition (i.e., lossless join decomposition)
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.