1 Normalization Anomalies Boyce-Codd Normal Form 3 rd Normal Form Source: Slides by Jeffrey Ullman.

27
1 Normalization Anomalies Boyce-Codd Normal Form 3 rd Normal Form Source: Slides by Jeffrey Ullman
  • date post

    20-Dec-2015
  • Category

    Documents

  • view

    236
  • download

    3

Transcript of 1 Normalization Anomalies Boyce-Codd Normal Form 3 rd Normal Form Source: Slides by Jeffrey Ullman.

Page 1: 1 Normalization Anomalies Boyce-Codd Normal Form 3 rd Normal Form Source: Slides by Jeffrey Ullman.

1

Normalization

AnomaliesBoyce-Codd Normal Form

3rd Normal Form

Source: Slides by Jeffrey Ullman

Page 2: 1 Normalization Anomalies Boyce-Codd Normal Form 3 rd Normal Form Source: Slides by Jeffrey Ullman.

2

Anomalies

Goal of relational schema design is to avoid anomalies and redundancy. Update anomaly : one occurrence of a

fact is changed, but not all occurrences.

Deletion anomaly : a valid fact is lost when a tuple is deleted.

Page 3: 1 Normalization Anomalies Boyce-Codd Normal Form 3 rd Normal Form Source: Slides by Jeffrey Ullman.

3

Example of Bad Design

Consumers(name, addr, candiesLiked, manf, favCandy)

name addr candiesLiked manf favCandyJaneway Voyager Twizzlers Hershey SmartiesJaneway ??? Smarties Pete’s ???Spock Enterprise Twizzlers ??? Twizzlers

Data is redundant, because each of the ???’s can be figuredout by using the FD’s name -> addr favCandy andcandiesLiked -> manf.

Page 4: 1 Normalization Anomalies Boyce-Codd Normal Form 3 rd Normal Form Source: Slides by Jeffrey Ullman.

4

This Bad Design AlsoExhibits Anomalies

name addr candiesLiked manf favCandyJaneway Voyager Twizzlers Hershey SmartiesJaneway Voyager Smarties Nestle SmartiesSpock Enterprise Twizzlers Hershey Twizzlers

• Update anomaly: if Janeway is transferred to Intrepid, will we remember to change each of her tuples?• Deletion anomaly: If nobody likes Twizzlers, we lose track of the fact that Hershey manufactures Twizzlers.

Page 5: 1 Normalization Anomalies Boyce-Codd Normal Form 3 rd Normal Form Source: Slides by Jeffrey Ullman.

5

Problem & Solution Conceptual modeling unnaturally puts

attributes of two different entity sets in same relation schema

Use decompositions to break up schema involving such an “unhappy union” into several relation schemas

Must do this decomposition carefully: Various normal forms have been proposed

for relations If a schema is in one of these normal

forms, then we now that certain kinds of problems do not occur

Page 6: 1 Normalization Anomalies Boyce-Codd Normal Form 3 rd Normal Form Source: Slides by Jeffrey Ullman.

6

Properties of a Decomposition

Lossless join: any tuple of the original relation can be reconstructed from corresponding tuples of the resulting relations

Dependency preservation: we can enforce any constraint on the original relation by enforcing some constraints on the resulting relations

Page 7: 1 Normalization Anomalies Boyce-Codd Normal Form 3 rd Normal Form Source: Slides by Jeffrey Ullman.

7

When to Decompose?

If you frequently want the full information available in instances of the original relation, don’t decompose Expensive to reconstruct original

information Instead, put up with the storage

redundancy and put extra checks in apps to avoid anomalies

Page 8: 1 Normalization Anomalies Boyce-Codd Normal Form 3 rd Normal Form Source: Slides by Jeffrey Ullman.

8

Boyce-Codd Normal Form

We say a relation R is in BCNF if whenever X ->A is a nontrivial FD that holds in R, X is a superkey. Remember: nontrivial means A is

not a member of set X. Remember, a superkey is any

superset of a key (not necessarily a proper superset).

Page 9: 1 Normalization Anomalies Boyce-Codd Normal Form 3 rd Normal Form Source: Slides by Jeffrey Ullman.

9

Example

Consumers(name, addr, candiesLiked, manf, favCandy)

FD’s: name->addr favCandy, candiesLiked->manf

Only key is {name, candiesLiked}. In each FD, the left side is not a

superkey. Any one of these FD’s shows,

Consumers is not in BCNF

Page 10: 1 Normalization Anomalies Boyce-Codd Normal Form 3 rd Normal Form Source: Slides by Jeffrey Ullman.

10

Another Example

Candies(name, manf, manfAddr)FD’s: name->manf, manf->manfAddr Only key is {name} . name->manf does not violate BCNF,

but manf->manfAddr does.

Page 11: 1 Normalization Anomalies Boyce-Codd Normal Form 3 rd Normal Form Source: Slides by Jeffrey Ullman.

11

Properties of BCNF

If a relation is in BCNF, then every attribute of every tuple records a piece of information that cannot be inferred, using FD’s, from any other information in the relation Eliminates redundancy

If a relation is not in BCNF, there is always a lossless-join decomposition into a collection of BCNF relations But may not be a dependency-preserving

decomposition

Page 12: 1 Normalization Anomalies Boyce-Codd Normal Form 3 rd Normal Form Source: Slides by Jeffrey Ullman.

12

Decomposition into BCNF Given: relation R with set of FD’s F. Compute keys for R (sets of attributes that

functionally determine all other attributes) Look among the given FD’s for a BCNF

violation X ->B. If any FD following from F violates BCNF, then

there will surely be an FD in F itself that violates BCNF.

Compute X +. Won't be all attributes, or else X would be a

superkey and X ->B would not be a violation

Page 13: 1 Normalization Anomalies Boyce-Codd Normal Form 3 rd Normal Form Source: Slides by Jeffrey Ullman.

13

Decompose R Using X -> B

Replace R by relations R1 and R2 with schemas:

R1 : X + (all attributes "reachable" from X) R2 : R – (X + – X ) (X plus all attributes not

"reachable" from X) Project given FD’s F onto the two new

relations (ignore FD's containing attributes no longer in the new relation).

Check if procedure must be repeated with R1 and/or R2.

Page 14: 1 Normalization Anomalies Boyce-Codd Normal Form 3 rd Normal Form Source: Slides by Jeffrey Ullman.

14

Decomposition Picture

R-X + X X +-X

R2

R1

R

Page 15: 1 Normalization Anomalies Boyce-Codd Normal Form 3 rd Normal Form Source: Slides by Jeffrey Ullman.

15

Example

Consumers(name, addr, candiesLiked, manf, favCandy)

F = name->addr, name -> favCandy,candiesLiked->manf

Pick BCNF violation name->addr. Close the left side: {name}+ = {name,

addr, favCandy}. Decomposed relations:

Consumers1(name, addr, favCandy) Consumers2(name, candiesLiked, manf)

Page 16: 1 Normalization Anomalies Boyce-Codd Normal Form 3 rd Normal Form Source: Slides by Jeffrey Ullman.

16

Example, Continued

We are not done; we need to check Consumers1 and Consumers2 for BCNF.

Projecting FD’s is easy here. For Consumers1(name, addr,

favCandy), relevant FD’s are name->addr and name->favCandy. Thus, {name} is the only key and

Consumers1 is in BCNF.

Page 17: 1 Normalization Anomalies Boyce-Codd Normal Form 3 rd Normal Form Source: Slides by Jeffrey Ullman.

17

Example, Continued

For Consumers2(name, candiesLiked, manf), the only FD is candiesLiked -> manf, and the only key is {name, candiesLiked}.

Violation of BCNF. candiesLiked+ = {candiesLiked,

manf}, so we decompose Consumers2 into:

Consumers3(candiesLiked, manf) Consumers4(name, candiesLiked)

Page 18: 1 Normalization Anomalies Boyce-Codd Normal Form 3 rd Normal Form Source: Slides by Jeffrey Ullman.

18

Example, Concluded

The resulting decomposition of Consumers :

Consumers1(name, addr, favCandy)Consumers3(candiesLiked, manf)Consumers4(name, candiesLiked)

Notice: Consumers1 tells us about consumers, Consumers3 tells us about candies, and Consumers4 tells us the relationship between consumers and the candies they like.

Page 19: 1 Normalization Anomalies Boyce-Codd Normal Form 3 rd Normal Form Source: Slides by Jeffrey Ullman.

19

BCNF Decomposition Not Unique

Suppose several dependencies violate BCNF.

Depending on which is used to guide the next decomposition, might get different collections of decomposed relations

Normalization theory does not help us decide among alternatives: designer must consider alternatives and choose based on semantics of the application

Page 20: 1 Normalization Anomalies Boyce-Codd Normal Form 3 rd Normal Form Source: Slides by Jeffrey Ullman.

20

Third Normal Form - Motivation

There is one structure of FD’s that causes trouble when we decompose.

AB ->C and C ->B. Example: A = street address, B = city, C = zip code.

There are two keys, {A,B } and {A,C }. Think about why.

C ->B is a BCNF violation (why?), so we must decompose into AC, BC.

Page 21: 1 Normalization Anomalies Boyce-Codd Normal Form 3 rd Normal Form Source: Slides by Jeffrey Ullman.

21

We Cannot Enforce FD’s

The problem is that if we use AC and BC as our database schema, we cannot enforce the FD AB ->C by checking FD’s in these decomposed relations.

Example with A = street, B = city, and C = zip on the next slide.

Page 22: 1 Normalization Anomalies Boyce-Codd Normal Form 3 rd Normal Form Source: Slides by Jeffrey Ullman.

22

An Unenforceable FD

Suppose we have relation Addr with FD's: street city -> zip zip -> city

Keys are {street, city} and {street, zip} After decomposing, we have relations

Addr1(street,zip) with no FD (!) Addr2(city,zip) with FD zip -> city

Consider instances on next slide…

Page 23: 1 Normalization Anomalies Boyce-Codd Normal Form 3 rd Normal Form Source: Slides by Jeffrey Ullman.

23

An Unenforceable FD (cont'd)

street zip545 Tech Sq. 02138545 Tech Sq. 02139

city zipCambridge 02138Cambridge 02139

Join tuples with equal zip codes.

street city zip545 Tech Sq. Cambridge 02138545 Tech Sq. Cambridge 02139

Although no FD’s were violated in the decomposed relations,FD street city -> zip is violated by the database as a whole.

Addr1: Addr2:

Page 24: 1 Normalization Anomalies Boyce-Codd Normal Form 3 rd Normal Form Source: Slides by Jeffrey Ullman.

24

3NF Lets Us Avoid This Problem

3rd Normal Form (3NF) modifies the BCNF condition so we do not have to decompose in this problem situation.

An attribute is prime if it is a member of any key.

X ->A violates 3NF if and only if X is not a superkey, and also A is not prime.

Page 25: 1 Normalization Anomalies Boyce-Codd Normal Form 3 rd Normal Form Source: Slides by Jeffrey Ullman.

25

Example

In our problem situation with FD’s AB ->C and C ->B, we have keys AB and AC.

Thus A, B, and C are each prime. Although C ->B violates BCNF, it

does not violate 3NF. So no need to decompose.

Page 26: 1 Normalization Anomalies Boyce-Codd Normal Form 3 rd Normal Form Source: Slides by Jeffrey Ullman.

26

What 3NF and BCNF Give You

There are two important properties of a decomposition:

1. Recovery (aka Lossless Join): it should be possible to project the original relations onto the decomposed schema, and then reconstruct the original.

2. Dependency Preservation : it should be possible to check in the projected relations whether all the (original) given FD’s are satisfied.

Page 27: 1 Normalization Anomalies Boyce-Codd Normal Form 3 rd Normal Form Source: Slides by Jeffrey Ullman.

27

3NF and BCNF, Continued

We can get (1) with a BCNF decomposition. Explanation needs to wait for relational

algebra. We can get both (1) and (2) with a 3NF

decomposition. But we can’t always get (1) and (2) with a

BCNF decomposition. street-city-zip is an example.