Download presentation
Presentation is loading. Please wait.
Published byLeslie Quinn Modified over 8 years ago
1
11/16/2016CENG352 Database Management Systems1 Relational Normalization Theory
2
11/16/2016CENG352 Database Management Systems2 Redundancy Dependencies between attributes cause redundancy –Eg. All addresses in the same town have the same zip code SSN Name Town Zip 1234 Joe Stony Brook 11790 4321 Mary Stony Brook 11790 5454 Tom Stony Brook 11790 …………………. Redundancy
3
11/16/2016CENG352 Database Management Systems3 Redundancy and Other Problems Set valued attributes in the E-R diagram result in multiple rows in corresponding table PersonExample: Person (SSN, Name, Address, Hobbies) Person –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 PersonThe relation Person can’t describe people without hobbies
4
11/16/2016CENG352 Database Management Systems4 Example SSN Name Address Hobby 1111 Joe 123 Main biking 1111 Joe 123 Main hiking ……………. SSN Name Address Hobby 1111 Joe 123 Main {biking, hiking} ER Model Relational Model Redundancy
5
11/16/2016CENG352 Database Management Systems5 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
6
11/16/2016CENG352 Database Management Systems6 Decomposition PersonSolution: use two relations to store Person information –Person1 –Person1 (SSN, Name, Address) –Hobbies –Hobbies (SSN, Hobby) The decomposition is more general: people with hobbies can now be described No update anomalies: –Name and address stored once –A hobby can be separately supplied or deleted
7
11/16/2016CENG352 Database Management Systems7 Normalization Theory Result of E-R analysis need further refinement Appropriate decomposition can solve problems normalization theory functional dependencies multi-valued dependenciesThe underlying theory is referred to as normalization theory and is based on functional dependencies (and other kinds, like multi-valued dependencies)
8
11/16/2016CENG352 Database Management Systems8 Functional Dependencies functional dependencyDefinition: A functional dependency (FD) on a relation schema R is a constraint X Y, where X and Y are subsets of attributes of R. satisfiedAn FD X Y is satisfied in an instance r of R if for every pair of tuples, t and s: if t and s agree on all attributes in X then they must agree on all attributes in Y –Key constraint is a special kind of functional dependency: all attributes of relation occur on the right-hand side of the FD: SSN SSN, Name, Address
9
11/16/2016CENG352 Database Management Systems9 Functional Dependencies (Examples) Address ZipCode –Stony Brook’s ZIP is 11733 ArtistName BirthYear –Picasso was born in 1881 Autobrand Manufacturer, Engine type –Pontiac is built by General Motors with gasoline engine Author, Title PublDate –Shakespeare’s Hamlet published in 1600
10
11/16/2016CENG352 Database Management Systems10 Functional Dependency - Example Brokerage firm allows multiple clients to share an account, but each account is managed from a single office and a client can have no more than one account in an office –HasAccount –HasAccount (AcctNum, ClientId, OfficeId) keys are (ClientId, OfficeId), (AcctNum, ClientId) –Client, OfficeId AcctNum –AcctNum OfficeId Thus, attribute values need not depend only on key values Write an SQL assertion that prevents a violation of this constraint.
11
11/16/2016CENG352 Database Management Systems11 Entailment, Closure, Equivalence entailsDefinition: If F is a set of FDs on schema R and f is another FD on R, then F entails f if every instance r of R that satisfies every FD in F also satisfies f –Ex: F = {A B, B C} and f is A C If Streetaddr Town and Town Zip then Streetaddr Zip closureDefinition: The closure of F, denoted F +, is the set of all FDs entailed by F equivalentDefinition: F and G are equivalent if F entails G and G entails F
12
11/16/2016CENG352 Database Management Systems12 Entailment (cont’d) Closure, entailment, and equivalence are semantic concepts – defined in terms of the actual relations in the “real world.” –They define what these notions are, not how to compute them How to check if F entails f or if F and G are equivalent? Solution: find algorithmic, syntactic ways to compute these notions Important: The syntactic solution must be “correct” with respect to the semantic definitions soundnesscompletenessCorrectness has two aspects: soundness and completeness
13
11/16/2016CENG352 Database Management Systems13 Armstrong’s Axioms for FDs This is the syntactic way of computing/testing the various properties of FDs Reflexivity: If Y X then X Y (trivial FD) –Name, Address Name Augmentation: If X Y then X Z YZ –If Town Zip then Town, Name Zip, Name Transitivity: If X Y and Y Z then X Z
14
11/16/2016CENG352 Database Management Systems14 Soundness soundAxioms are sound: If an FD f: X Y can be derived from a set of FDs F using the axioms, then f holds in every relation that satisfies every FD in F. Example: Given X Y and X Z then –Thus, X Y Z is satisfied in every relation where both X Y and X Y are satisfied union ruleTherefore, we have derived the union rule for FDs: we can take the union of the RHSs of FDs that have the same LHS X XY Augmentation by X YX YZ Augmentation by Y X YZ Transitivity
15
11/16/2016CENG352 Database Management Systems15 Derived inference rules Union: if X Y and X Z, then X YZ. Decomposition: if X YZ, then X Y and X Z. Pseudotransitivity: if X Y and WY Z, then WX Z. These additional rules are not essential; their soundness can be proved using Armstrong’s Axioms. Exercise: Prove rules Decomposition and Pseudotransitivity using A.A.
16
11/16/2016CENG352 Database Management Systems16 Completeness completeAxioms are complete: If F entails f, then f can be derived from F using the axioms A consequence of completeness is the following (naïve) algorithm to determining if F entails f: –Algorithm –Algorithm: Use the axioms in all possible ways to generate F + (the set of possible FD’s is finite so this can be done) and see if f is in F +
17
11/16/2016CENG352 Database Management Systems17 Generating F + F AB C AB BCD A D AB BD AB BCDE AB CDE D E BCD BCDE Thus, AB BD, AB BCD, AB BCDE, and AB CDE are all elements of F + union aug trans aug decomp
18
11/16/2016CENG352 Database Management Systems18 Attribute Closure Calculating attribute closure leads to a more efficient way of checking entailment attribute closureThe attribute closure of a set of attributes, X, with respect to a set of functional dependencies, F, (denoted X + F ) is the set of all attributes, A, such that X A –X + F1 is not necessarily the same as X + F2 if F1 F2 Attribute closure and entailment: –Algorithm –Algorithm: Given a set of FDs, F, then X Y if and only if X + F Y
19
11/16/2016CENG352 Database Management Systems19 Example - Computing Attribute Closure F: AB C A D D E AC B X X F + A {A, D, E} AB {A, B, C, D, E} (Hence AB is a key) B {B} D {D, E} Is AB E entailed by F? Yes Is D C entailed by F? No Result: X F + allows us to determine FDs of the form X Y entailed by F
20
11/16/2016CENG352 Database Management Systems20 Computation of Attribute Closure X + F closure := X; // since X X + F repeat old := closure; if there is an FD Z V in F such that Z closure and V closure then closure := closure V until old = closure – If T closure then X T is entailed by F
21
11/16/2016CENG352 Database Management Systems21 Example: Computation of Attribute Closure AB C (a) A D (b) D E (c) AC B (d) Problem: Compute the attribute closure of AB with respect to the set of FDs : Initially closure = {AB} Using (a) closure = {ABC} Using (b) closure = {ABCD} Using (c) closure = {ABCDE} Solution:
22
11/16/2016CENG352 Database Management Systems22 Normal Forms Each normal form is a set of conditions on a schema that guarantees certain properties (relating to redundancy and update anomalies) First normal form (1NF) is the same as the definition of relational model (relations = sets of tuples; each tuple = sequence of atomic values) Second normal form (2NF) – a research lab accident; has no practical or theoretical value – won’t discuss third normal formBoyce-Codd normal formThe two commonly used normal forms are third normal form (3NF) and Boyce-Codd normal form (BCNF)
23
11/16/2016CENG352 Database Management Systems23 BCNF Definition: A relation schema R is in BCNF if for every FD X Y associated with R either –Y X (i.e., the FD is trivial) or –X is a superkey of R Person1Example: Person1(SSN, Name, Address) –The only FD is SSN Name, Address Person1 –Since SSN is a key, Person1 is in BCNF
24
11/16/2016CENG352 Database Management Systems24 (non) BCNF Examples PersonPerson (SSN, Name, Address, Hobby) –The FD SSN Name, Address does not satisfy requirements of BCNF since the key is (SSN, Hobby) HasAccountHasAccount (AccountNumber, ClientId, OfficeId) –The FD AcctNum OfficeId does not satisfy BCNF requirements since keys are (ClientId, OfficeId) and (AcctNum, ClientId)
25
11/16/2016CENG352 Database Management Systems25 Redundancy Suppose R has a FD A B. If an instance has 2 rows with same value in A, they must also have same value in B (=> redundancy, if the A-value repeats twice) If A is a superkey, there cannot be two rows with same value of A –Hence, BCNF eliminates redundancy SSN Name, Address SSN Name Address Hobby 1111 Joe 123 Main stamps 1111 Joe 123 Main coins redundancy
26
11/16/2016CENG352 Database Management Systems26 Third Normal Form A relational schema R is in 3NF if for every FD X Y associated with R either: –Y X (i.e., the FD is trivial); or –X is a superkey of R; or –Every A Y is part of some key of R 3NF is weaker than BCNF (every schema that is in BCNF is also in 3NF) BCNF conditions
27
11/16/2016CENG352 Database Management Systems27 3NF Example HasAccountHasAccount (AcctNum, ClientId, OfficeId) –ClientId, OfficeId AcctNum OK since LHS contains a key –AcctNum OfficeId OK since RHS is part of a key HasAccountHasAccount is in 3NF but it might still contain redundant information due to AcctNum OfficeId (which is not allowed by BCNF)
28
11/16/2016CENG352 Database Management Systems28 3NF Example HasAccountHasAccount might store redundant data: ClientId OfficeId AcctNum 1111 Stony Brook 28315 2222 Stony Brook 28315 3333 Stony Brook 28315 ClientId AcctNum 1111 28315 2222 28315 3333 28315 BCNF (only trivial FDs) Decompose to eliminate redundancy: OfficeId AcctNum Stony Brook 28315 BCNF: AcctNum is key FD: AcctNum OfficeId 3NF: OfficeId part of key FD: AcctNum OfficeId redundancy
29
11/16/2016CENG352 Database Management Systems29 (Non) 3NF Example PersonPerson (SSN, Name, Address, Hobby) –(SSN, Hobby) is the only key. –SSN Name violates 3NF conditions since Name is not part of a key and SSN is not a superkey
30
11/16/2016CENG352 Database Management Systems30 Decompositions Goal: Eliminate redundancy by decomposing a relation into several relations in a higher normal form losslessDecomposition must be lossless: it must be possible to reconstruct the original relation from the relations in the decomposition We will see why
31
11/16/2016CENG352 Database Management Systems31 Decomposition Schema R = (R, F) –R is a set of attributes –F is a set of functional dependencies over R decompositionof schemaThe decomposition of schema R is a collection of schemas R i = (R i, F i ) where –R = i R i for all i (no new attributes) –F i is a set of functional dependences involving only attributes of R i –F entails F i for all i (no new FDs) decomposition of an instanceThe decomposition of an instance, r, of R is a set of relations r i = R i (r) for all i
32
11/16/2016CENG352 Database Management Systems32 Example Decomposition Schema (R, F) where R = {SSN, Name, Address, Hobby} F = {SSN Name, Address} can be decomposed into R 1 = {SSN, Name, Address} F 1 = {SSN Name, Address} and R 2 = {SSN, Hobby} F 2 = { }
33
11/16/2016CENG352 Database Management Systems33 Lossless Schema Decomposition A decomposition should not lose information losslessA decomposition (R 1,…,R n ) of a schema, R, is lossless if every valid instance, r, of R can be reconstructed from its components: where each r i = Ri (r) r = r 1 r2r2 rnrn ……
34
11/16/2016CENG352 Database Management Systems34 Lossy Decomposition r r1r r1 r2r2... rnrn SSN Name Address SSN Name Name Address 1111 Joe 1 Pine 1111 Joe Joe 1 Pine 2222 Alice 2 Oak 2222 Alice Alice 2 Oak 3333 Alice 3 Pine 3333 Alice Alice 3 Pine r r1r r1 r2r2 rnrn... r1r1 r2r2 r The following is always the case (Think why?) : But the following is not always true : Example : The tuples (2222, Alice, 3 Pine) and (3333, Alice, 2 Oak) are in the join, but not in the original
35
11/16/2016CENG352 Database Management Systems35 Lossy Decompositions: What is Actually Lost? In the previous example, the tuples (2222, Alice, 3 Pine) and (3333, Alice, 2 Oak) were gained, not lost! –Why do we say that the decomposition was lossy? What was lost is information: –That 2222 lives at 2 Oak: In the decomposition, 2222 can live at either 2 Oak or 3 Pine –That 3333 lives at 3 Pine: In the decomposition, 3333 can live at either 2 Oak or 3 Pine
36
11/16/2016CENG352 Database Management Systems36 Testing for Losslessness A (binary) decomposition of R = (R, F) into R 1 = (R 1, F 1 ) and R 2 = (R 2, F 2 ) is lossless if and only if : –either the FD (R 1 R 2 ) R 1 is in F + –or the FD (R 1 R 2 ) R 2 is in F +
37
11/16/2016CENG352 Database Management Systems37 Example Schema (R, F) where R = {SSN, Name, Address, Hobby} F = {SSN Name, Address} can be decomposed into R 1 = {SSN, Name, Address} F 1 = {SSN Name, Address} and R 2 = {SSN, Hobby} F 2 = { } Since R 1 R 2 = SSN and SSN R 1 the decomposition is lossless
38
11/16/2016CENG352 Database Management Systems38 Intuition Behind the Test for Losslessness Suppose R 1 R 2 R 2. Then a row of r 1 can combine with exactly one row of r 2 in the natural join (since in r 2 a particular set of values for the attributes in R 1 R 2 defines a unique row) R 1 R 2 R 1 R 2 …………. a a ………... ………… a b …………. ………… b c …………. ………… c r 1 r 2
39
11/16/2016CENG352 Database Management Systems39 If R 1 R 2 R 2 then card (r 1 Proof of Lossless Condition r r 1 r 2 – this is true for any decomposition r 2 ) = card (r 1 ) But card (r) card (r 1 ) (since r 1 is a projection of r) and therefore card (r) card (r 1 r 2 ) Hence r = r 1 r2r2 r r 1 r2r2 (since each row of r 1 joins with exactly one row of r 2 )
40
11/16/2016CENG352 Database Management Systems40 Dependency Preservation Consider a decomposition of R = (R, F) into R 1 = (R 1, F 1 ) and R 2 = (R 2, F 2 ) –An FD X Y of F is in F i iff X Y R i –An FD, f F may be in neither F 1, nor F 2, nor even (F 1 F 2 ) + Checking that f is true in r 1 or r 2 is (relatively) easy Checking f in r 1 r 2 is harder – requires a join Ideally: want to check FDs locally, in r 1 and r 2, and have a guarantee that every f F holds in r 1 r 2 dependency preservingThe decomposition is dependency preserving iff the sets F and F 1 F 2 are equivalent: F + = (F 1 F 2 ) + –Then checking all FDs in F, as r 1 and r 2 are updated, can be done by checking F 1 in r 1 and F 2 in r 2
41
11/16/2016CENG352 Database Management Systems41 Dependency Preservation If f is an FD in F, but f is not in F 1 F 2, there are two possibilities: –f (F 1 F 2 ) + If the constraints in F 1 and F 2 are maintained, f will be maintained automatically. –f (F 1 F 2 ) + f can be checked only by first taking the join of r 1 and r 2. This is costly.
42
11/16/2016CENG352 Database Management Systems42 Example Schema (R, F) where R = {SSN, Name, Address, Hobby} F = {SSN Name, Address} can be decomposed into R 1 = {SSN, Name, Address} F 1 = {SSN Name, Address} and R 2 = {SSN, Hobby} F 2 = { } Since F = F 1 F 2 the decomposition is dependency preserving
43
11/16/2016CENG352 Database Management Systems43 Example Schema: R= (ABC; F), F = {A B, B C, C B} Decomposition: –(AC, F 1 ), F 1 = {A C} Note: A C F, but in F + –(BC, F 2 ), F 2 = {B C, C B} A B (F 1 F 2 ), but A B (F 1 F 2 ) +. –So F + = (F 1 F 2 ) + and thus the decompositions is still dependency preserving
44
11/16/2016CENG352 Database Management Systems44 Example HasAccountHasAccount (AccountNumber, ClientId, OfficeId) f 1 : AccountNumber OfficeId f 2 : ClientId, OfficeId AccountNumber Decomposition: AcctOffice AcctOffice = (AccountNumber, OfficeId; {AccountNumber OfficeId}) AcctClient AcctClient = (AccountNumber, ClientId; {}) Decomposition is lossless: R 1 R 2 = {AccountNumber} and AccountNumber OfficeId In BCNF Not dependency preserving: f 2 (F 1 F 2 ) + HasAccountHasAccount does not have BCNF decompositions that are both lossless and dependency preserving! (Check, eg, by enumeration) Hence: BCNF+lossless+dependency preserving decompositions are not always achievable!
45
11/16/2016CENG352 Database Management Systems45 BCNF Decomposition Algorithm Input: R = (R; F) Decomp := R while there is S = (S; F’) Decomp and S not in BCNF do Find X Y F’ that violates BCNF // X isn’t a superkey in S Replace S in Decomp with S 1 = (XY; F 1 ), S 2 = (S - (Y - X); F 2 ) // F 1 = all FDs of F’ involving only attributes of XY // F 2 = all FDs of F’ involving only attributes of S - (Y - X) end return Decomp
46
11/16/2016CENG352 Database Management Systems46 Example Given: R = (R; T) where R = ABCDEFGH and T = {ABH C, A DE, BGH F, F ADH, BH GE} step 1: Find a FD that violates BCNF Not ABH C since (ABH) + includes all attributes (BH is a key) A DE violates BCNF since A is not a superkey (A + =ADE) step 2: Split R into: R 1 = (ADE, {A DE }) R 2 = (ABCFGH; {ABH C, BGH F, F AH, BH G}) Note 1: R 1 is in BCNF Note 2: Decomposition is lossless since A is a key of R 1. Note 3: FDs F D and BH E are not in T 1 or T 2. But both can be derived from T 1 T 2 (E.g., F A and A D implies F D) Hence, decomposition is dependency preserving.
47
11/16/2016CENG352 Database Management Systems47 Example (con’t) Given: R 2 = (ABCFGH; {ABH C, BGH F, F AH, BH G}) step 1: Find a FD that violates BCNF. Not ABH C or BGH F, since BH is a key of R 2 F AH violates BCNF since F is not a superkey (F + =AH) step 2: Split R 2 into: R 21 = (FAH, {F AH}) R 22 = (BCFG; {}) Note 1: Both R 21 and R 22 are in BCNF. Note 2: The decomposition is lossless (since F is a key of R 21 ) Note 3: FDs ABH C, BGH F, BH G are not in T 21 or T 22, and they can’t be derived from T 1 T 21 T 22. Hence the decomposition is not dependency-preserving
48
11/16/2016CENG352 Database Management Systems48 Properties of BCNF Decomposition Algorithm A BCNF decomposition is not necessarily dependency preserving But always lossless HasAccountBCNF+lossless+dependency preserving is sometimes unachievable (recall HasAccount)
49
11/16/2016CENG352 Database Management Systems49 Third Normal Form Compromise – Not all redundancy removed, but dependency preserving decompositions are always possible (and, of course, lossless) 3NF decomposition is based on a minimal cover
50
11/16/2016CENG352 Database Management Systems50 Minimal Cover minimal coverA minimal cover of a set of dependencies, T, is a set of dependencies, U, such that: –U is equivalent to T (T + = U + ) –All FDs in U have the form X A where A is a single attribute –It is not possible to make U smaller (while preserving equivalence) by Deleting an FD Deleting an attribute from an FD (either from LHS or RHS) redundant –FDs and attributes that can be deleted in this way are called redundant
51
11/16/2016CENG352 Database Management Systems51 Computing Minimal Cover Example: T = {ABH CK, A D, C E, BGH F, F AD, E F, BH E} step 1: Make RHS of each FD into a single attribute –Algorithm: Use the decomposition inference rule for FDs –Example: F AD replaced by F A, F D ; ABH CK by ABH C, ABH K step 2: Eliminate redundant attributes from LHS. –Algorithm: If FD XB A T (where B is a single attribute) and X A is entailed by T, then B was unnecessary –Example: Can an attribute be deleted from ABH C ? Compute AB + T, AH + T, BH + T. Since C (BH) + T, BH C is entailed by T and A is redundant in ABH C.
52
11/16/2016CENG352 Database Management Systems52 Computing Minimal Cover (con’t) step 3: Delete redundant FDs from T –Algorithm: If T – {f} entails f, then f is redundant If f is X A then check if A X + T-{f} –Example: BH F is entailed by E F, BH E, so it is redundant Note: The order of steps 2 and 3 cannot be interchanged!!
53
11/16/2016CENG352 Database Management Systems53 3NF Decomposition step 1: Compute a minimal cover, U, of T. The decomposition is based on U, but since U + = T + the same functional dependencies will hold –A minimal cover for T={ABH CK, A D, C E, BGH F, F AD, E F, BH E} is U={BH C, BH K, A D, C E, F A, E F} Starting with a schema R = (R, T)
54
11/16/2016CENG352 Database Management Systems54 3NF Decomposition (con’t) step 2: Partition U into sets U 1, U 2, … U n such that the LHS of all elements of U i are the same –U 1 = {BH C, BH K}, U 2 = {A D}, U 3 = {C E}, U 4 = {F A}, U 5 = {E F}
55
11/16/2016CENG352 Database Management Systems55 3NF Decomposition (con’t) step 3: For each U i form schema R i = (R i, U i ), where R i is the set of all attributes mentioned in U i –Each FD of U will be in some R i. Hence the decomposition is dependency preserving –R 1 = (BHCK; BH C, BH K), R 2 = (AD; A D), R 3 = (CE; C E), R 4 = (FA; F A), R 5 = (EF; E F)
56
11/16/2016CENG352 Database Management Systems56 3NF Decomposition (con’t) step 4: If no R i is a superkey of R, add schema R 0 = (R 0,{}) where R 0 is a key of R. –R 0 = (BGH, {}) R 0 might be needed when not all attributes are necessarily contained in R 1 R 2 … R n –A missing attribute, A, must be part of all keys (since it’s not in any FD of U, deriving a key constraint from U involves the augmentation axiom) R 0 might be needed even if all attributes are accounted for in R 1 R 2 … R n –Example: (ABCD; {A B, C D}). Step 3 decomposition: R 1 = (AB; {A B}), R 2 = (CD; {C D}). Lossy! Need to add (AC; { }), for losslessness –Step 4 guarantees lossless decomposition.
57
11/16/2016CENG352 Database Management Systems57 BCNF Design Strategy The resulting decomposition, R 0, R 1, … R n, is –Dependency preserving (since every FD in U is a FD of some schema) –Lossless (although this is not obvious) –In 3NF (although this is not obvious) Strategy for decomposing a relation –Use 3NF decomposition first to get lossless, dependency preserving decomposition –If any resulting schema is not in BCNF, split it using the BCNF algorithm (but this may yield a non- dependency preserving result)
58
58 Multivalued Dependencies Fourth Normal Form Reasoning About FD’s + MVD’s
59
59 Definition of MVD A multivalued dependency (MVD) on R, X ->->Y, says that if two tuples of R agree on all the attributes of X, then their components in Y may be swapped, and the result will be two tuples that are also in the relation. i.e., for each value of X, the values of Y are independent of the values of R-X-Y.
60
60 Example: MVD Drinkers(name, addr, phones, beersLiked) A drinker’s phones are independent of the beers they like. –name->->phones and name ->->beersLiked. Thus, each of a drinker’s phones appears with each of the beers they like in all combinations. This repetition is unlike FD redundancy. –name->addr is the only FD.
61
61 Tuples Implied by name->->phones If we have tuples: nameaddrphones beersLiked sueap1 b1 sueap2 b2 sueap2 b1 sueap1 b2 Then these tuples must also be in the relation.
62
62 MVD Rules Every FD is an MVD (promotion ). –If X ->Y, then swapping Y ’s between two tuples that agree on X doesn’t change the tuples. –Therefore, the “new” tuples are surely in the relation, and we know X ->->Y. Complementation : If X ->->Y, and Z is all the other attributes, then X ->->Z.
63
63 Splitting Doesn’t Hold Like FD’s, we cannot generally split the left side of an MVD. But unlike FD’s, we cannot split the right side either --- sometimes you have to leave several attributes on the right side.
64
64 Example: Multiattribute Right Sides Drinkers(name, areaCode, phone, beersLiked, manf) A drinker can have several phones, with the number divided between areaCode and phone (last 7 digits). A drinker can like several beers, each with its own manufacturer.
65
65 Example Continued Since the areaCode-phone combinations for a drinker are independent of the beersLiked- manf combinations, we expect that the following MVD’s hold: name ->-> areaCode phone name ->-> beersLiked manf
66
66 Example Data Here is possible data satisfying these MVD’s: nameareaCodephonebeersLikedmanf Sue650555-1111BudA.B. Sue650555-1111WickedAlePete’s Sue415555-9999BudA.B. Sue415555-9999WickedAlePete’s But we cannot swap area codes or phones by themselves. That is, neither name->->areaCode nor name->->phone holds for this relation.
67
67 Fourth Normal Form The redundancy that comes from MVD’s is not removable by putting the database schema in BCNF. There is a stronger normal form, called 4NF, that (intuitively) treats MVD’s as FD’s when it comes to decomposition, but not when determining keys of the relation.
68
68 4NF Definition A relation R is in 4NF if: whenever X ->->Y is a nontrivial MVD, then X is a superkey. –Nontrivial MVD means that: 1.Y is not a subset of X, and 2.X and Y are not, together, all the attributes. –Note that the definition of “superkey” still depends on FD’s only.
69
69 BCNF Versus 4NF Remember that every FD X ->Y is also an MVD, X ->->Y. Thus, if R is in 4NF, it is certainly in BCNF. –Because any BCNF violation is a 4NF violation (after conversion to an MVD). But R could be in BCNF and not 4NF, because MVD’s are “invisible” to BCNF.
70
70 Decomposition and 4NF If X ->->Y is a 4NF violation for relation R, we can decompose R using the same technique as for BCNF. 1.XY is one of the decomposed relations. 2.All but Y – X is the other.
71
71 Example: 4NF Decomposition Drinkers(name, addr, phones, beersLiked) FD: name -> addr MVD’s: name ->-> phones name ->-> beersLiked Key is {name, phones, beersLiked}. All dependencies violate 4NF.
72
72 Example Continued Decompose using name -> addr: 1.Drinkers1(name, addr) uIn 4NF; only dependency is name -> addr. 2.Drinkers2(name, phones, beersLiked) uNot in 4NF. MVD’s name ->-> phones and name ->-> beersLiked apply. No FD’s, so all three attributes form the key.
73
73 Example: Decompose Drinkers2 Either MVD name ->-> phones or name ->- > beersLiked tells us to decompose to: –Drinkers3(name, phones) –Drinkers4(name, beersLiked)
74
11/16/2016CENG352 Database Management Systems74 Normalization Drawbacks By limiting redundancy, normalization helps maintain consistency and saves space But performance of querying can suffer because related information that was stored in a single relation is now distributed among several Example: A join is required to get the names and grades of all students taking CS305 in S2002. SELECT S.Name, T.Grade StudentTranscript FROM Student S, Transcript T WHERE S.Id = T.StudId AND T.CrsCode = ‘CS305’ AND T.Semester = ‘S2002’
75
11/16/2016CENG352 Database Management Systems75 Denormalization Tradeoff: Judiciously introduce redundancy to improve performance of certain queries TranscriptExample: Add attribute Name to Transcript –Join is avoided Transcript –If queries are asked more frequently than Transcript is modified, added redundancy might improve average performance Transcript ’ –But, Transcript ’ is no longer in BCNF since key is (StudId, CrsCode, Semester) and StudId Name SELECT T.Name, T.Grade Transcript ’ FROM Transcript ’ T WHERE T.CrsCode = ‘CS305’ AND T.Semester = ‘S2002’
76
76 Exercises 1) Consider the following table. Give an example of update anomaly, an example of deletion anomaly and an example of insertion anomaly knowing that –A product has many suppliers and can have many other products as a substitute (i.e. a product can be replaced by its substitute). –The purchase price is determined by a supplier for a product, while the sale price is for a given product regardless of the supplier. –The quantity is for a given product, again regardless of the supplier.
77
77 Solution Update Anomaly: - Changing the quantity of a product implies updating the quantity for as many suppliers and substitutes there is for the product. Deletion Anomaly: - By deleting the only substitute of a product, the whole product entry needs to be removed. Insertion Anomaly: - We can’t add a substitute of a product if we do not know the supplier of the product.
78
78 Exercises (cont.) 2) Give a schema of a decomposition that avoids such anomalies. Solution: Product(ProductID, Quantity, SalePrice) Suppliers( ProductID, SupplierID, PurchasePrice) Substitutes( ProductID, Substitute)
79
79 3) Assume we have a relation schema R= (player, salary, team, city). An example relation instance: player salary team city Jeter 15,600,000 Yankees New York Garciaparra 10,500,000 Red Sox Boston We expect the following functional dependencies to hold: player →salary,player → team,team →city Argue that R is currently not in BCNF. Decompose R into BCNF. Argue that the decomposition is lossless-join, and that it preserves dependencies. Find an alternative decomposition of R into BCNF which is still lossless-join, but which not preserve dependencies. (State which dependency it does not preserve.) Show, by means of an example, that a decomposition into (player, salary, city) and (team, city) is not lossless-join.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.