Download presentation
Presentation is loading. Please wait.
1
Nov 11, 2003Murali Mani Normalization B term 2004: lecture 7, 8, 9
2
Nov 11, 2003Murali Mani Anomalies Insert, delete and update anomalies sNumbersNamepNumberpName s1Davep1MM s2Gregp2ER Student Note: We cannot insert a professor who has no students. Insert Anomaly
3
Nov 11, 2003Murali Mani Delete Anomaly sNumbersNamepNumberpName s1Davep1MM s2Gregp2ER Student Note: We cannot delete a student that is the only student of a professor. Note: In both cases, minimum cardinality of Professor in the corresponding ER schema is 0
4
Nov 11, 2003Murali Mani Update Anomaly sNumbersNamepNumberpName s1Davep1MM s2Gregp1MM Student Note: To update the name of a professor, we have to update in multiple tuples. Update anomalies are due to redundancy.
5
Nov 11, 2003Murali Mani Keys (primary, candidate and superkeys) A key for a relation R (a1, a2, …, an) is a set of attributes that uniquely determine the values for all attributes of R. A key K is minimal if no subset of K is a key. One of the minimal keys is assigned as primary key, and others are candidate keys. A superkey need not be minimal Note: a primary or candidate key is by default a superkey, but a superkey need not be a primary or candidate key. Note: For a relation R, the set of all its attributes is a superkey.
6
Nov 11, 2003Murali Mani Keys: Example sNumbersNameaddress 1Dave144FL 2Greg320FL Student Primary Key: sNumber Candidate key: sName Some superkeys: {,,, }
7
Nov 11, 2003Murali Mani From Set Theory: More on Functions Consider a function f : X Y, where X is domain, and Y is range f is one-to-one if no two x X, are related to the same y Y (also called injection) f is onto if for every y Y, there is some x X that it is related to (also called surjection) f is said to be a bijection if it is both one-to- one and onto
8
Nov 11, 2003Murali Mani Functional Dependencies (FDs) sNumbersNameaddress 1Dave144FL 2Greg320FL Student Suppose we have the FD sName address for any two rows in the Student relation with the same value for sName, the value for address must be the same. Note: we cannot have a tuple such as (3, null, a1), that is we cannot have a tuple with address value, but with no sName value. (this looks similar to onto functions, but not really). Note: Any key (primary, candidate or superkey) of a relation R functionally determines all attributes of R
9
Nov 11, 2003Murali Mani Properties of FDs A B, A C, then A BC If A BC, then A B, A C (assumption: no nulls) Reflexive (also called trivial FD), if A B, then A B is always true Transitive, if A B, and B C, then A C If A B, then AZ BZ is always true
10
Nov 11, 2003Murali Mani Inferring FDs Why? Suppose we have a relation R (A, B, C) and we have functional dependencies A B, B C, C A what is a key for R? Should we split R into multiple relations? We can infer A BC, B AC, C AB. Hence A, B, C are all keys.
11
Nov 11, 2003Murali Mani Algorithm for inference of FDs Based on transitivity, i.e., if A B and B C, then A C Computing the closure of set of attributes {A1, A2, …, An}, denoted {A1, A2, …, An} + 1. Let X = {A1, A2, …, An} 2. If there exists a FD B1, B2, …, Bm C, such that every Bi X, then X = X C 3. Repeat step 2 till no more attributes can be added. 4. {A1, A2, …, An} + = X
12
Nov 11, 2003Murali Mani Inferring FDs: Example Given R (A, B, C), and FDs A B, B C, C A, what are possible keys for R We can computer the closure of attributes as {A} + = {B, C} {B} + = {A, C} {C} + = {A, B} So minimal keys are,,
13
Nov 11, 2003Murali Mani Decomposing Relations sNumbersNamepNumberpName s1Davep1MM s2Gregp2MM Student FDs: pNumber pName sNumbersNamepNumber s1Davep1 s2Gregp2 Student pNumberpName p1MM p2MM Professor
14
Nov 11, 2003Murali Mani Bad Decomposition Example Generation of spurious tuples (also called lossless join property) sNumbersNamepName S1DaveMM S2GregMM Student pNumberpName p1MM p2MM Professor sNumbersNamepNumberpName s1Davep1MM s1Davep2MM s2Gregp1MM s2Gregp2MM Student
15
Nov 11, 2003Murali Mani Normalization Step Consider relation R with set of attributes A R. Consider a FD A B (such that no other attribute in (A R – A – B) is functionally determined by A). Let A be not a key for R. We may decompose R as: Remove attributes B from R, in other words create R’ (A R – B) Create R’’ with attributes A B Key for R’’ = A
16
Nov 11, 2003Murali Mani Normal Forms First Normal Form (1NF): Attributes of a relation are simple. Second Normal Form (2NF): No non-prime attribute a is partially dependent on any minimal key (i.e., no subset of a minimal key functionally determines a). Note: An attribute is prime attribute if it is part of some minimal key.
17
Nov 11, 2003Murali Mani 2NF - example Lot (propNo, county, lotNum, area, price, taxRate) Candidate key: FDs: county taxRate area price The FD county taxRate violates 2NF Decomposition: Lot (propNo, county, lotNum, area, price) County (county, taxRate)
18
Nov 11, 2003Murali Mani Normal Forms Third Normal Form (3NF): For every non- trivial FD X a in R, either a is a prime attribute or X is a superkey of R. Boyce Codd Normal Form (BCNF): For every non-trivial FD X a in R, X is a superkey of R.
19
Nov 11, 2003Murali Mani 3NF Example Lot (propNo, county, lotNum, area, price) County (county, taxRate) Candidate key for Lot: FDs: area price The FD area price violates 3NF Decomposition: Lot (propNo, county, lotNum, area) County (county, taxRate) Area (area, price)
20
Nov 11, 2003Murali Mani BCNF example SCI (student, course, instructor) FDs: student, course instructor instructor course Decomposition: SI (student, instructor) Instructor (instructor, course)
21
Nov 11, 2003Murali Mani Dependency Preservation We might want to ensure that all specified FDs are captured. BCNF does not necessarily preserve FDs. 2NF, 3NF preserve FDs. studentinstructor DaveMM GregER SI instructorcourse MMDB 1 ERDB 1 Instructor studentinstructorcourse DaveMMDB 1 DaveERDB 1 GregMMDB 1 GregERDB 1 SCI (from SI and Instructor) SCI violates the FD student, course instructor
22
Nov 11, 2003Murali Mani Worst Case Example Consider relation R (A, B, C, D) with primary key (A, B, C), and FDs B D, and C D. We see that R violates 2NF. Decomposing it, we get 3 relations as: R1 (A, B, C), R2 (B, D), R3 (C, D) Let us consider an instance where we need these 3 relations and how we do a natural join ⋈ ABC a1b1c1 a2b2c1 a3b1c2 BD b1d1 b2d2 R1 R2 CD c1d1 c2d2 R3 ABCD a1b1c1d1 a2b2c1d2 a3b1c2d1 R1 ⋈ R2: violates C D ABCD a1b1c1d1 R1 ⋈ R2 ⋈ R3: no FD is violated
23
Nov 11, 2003Murali Mani Define Foreign Keys? Consider the normalization step: A relation R with set of attributes A R, and a FD A B, and A is not a key for R, we may decompose R as: Remove attributes B from R, in other words create R’ (A R – B) Create R’’ with attributes A B Key for R’’ = A We can also define foreign key R’ (A) references R’’ (A)
24
Nov 11, 2003Murali Mani Multivalued Dependencies (MVDs) Consider relation R with attributes A R. Consider A = {A1, A2, …, An}, and B = {B1, B2, …, Bm}, where A, B A R. We say that A ↠ B if the set of values of B is dependent only on the value of A. cNumbertitleprofessortextbook 3431DB 1MMUW 3431DB 1MMGR 3431DB 1ERUW 3431DB 1ERGR CPT cNumber ↠ professor cNumber ↠ textbook
25
Nov 11, 2003Murali Mani Properties of MVDs Consider a relation R with set of attributes A R. Consider A, B A R. If we have the MVD A ↠ B, then the MVD A ↠ A R – A – B is true. If A ↠ B and A ↠ C, then A ↠ BC However, if A ↠ BC, then A ↠ B and A ↠ C is not true (contrast with FDs) Any FD is a MVD: so reflexive rule holds, i.e, if A B, then A ↠ B is always true (Transitive): If A ↠ B and B ↠ C, then A ↠ C
26
Nov 11, 2003Murali Mani 4NF (Fourth Normal Form) Consider a relation R with attributes A R. If there exists a MVD A ↠ B, where A, B A R, A B A R, A B = , and A is not a superkey, then R is not in 4NF. If R is not in 4NF, we decompose R as: Create R’ with attributes (A R – B) Create R’’ with attributes (A, B), the multivalued dependency is defined in R’’. Key for R’’ is (A, B)
27
Nov 11, 2003Murali Mani 4NF Example CPT Decomposing, we get 3 relations as: cNumbertitle 3431DB 1 R1 cNumberprofessor 3431MM 3431ER R2 coursetextbook 3431UW 3431GR R3 cNumbertitleprofessortextbook 3431DB 1MMUW 3431DB 1MMGR 3431DB 1ERUW 3431DB 1ERGR cNumber ↠ professor cNumber ↠ textbook
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.