Schema Refinement and Normal Forms

Slides:



Advertisements
Similar presentations
Schema Refinement and Normal Forms
Advertisements

Schema Refinement and Normal Forms Given a design, how do we know it is good or not? What is the best design? Can a bad design be transformed into a good.
Database Management Systems, 3ed, R. Ramakrishnan and J. Gehrke1 Schema Refinement and Normal Forms Chapter 19.
Murali Mani Normalization. Murali Mani What and Why Normalization? To remove potential redundancy in design Redundancy causes several anomalies: insert,
CS Schema Refinement and Normal Forms Chapter 19.
CS Algorithm : Decomposition into 3NF  Obviously, the algorithm for lossless join decomp into BCNF can be used to obtain a lossless join decomp.
Normalization DB Tuning CS186 Final Review Session.
Normalization DB Tuning CS186 Final Review Session.
H.Lu/HKUST L02: Logical Database Design  Introduction  Functional Dependencies  Normal Forms  Normalization by decomposition.
1 Normalization Chapter What it’s all about Given a relation, R, and a set of functional dependencies, F, on R. Assume that R is not in a desirable.
Schema Refinement and Normal Forms. The Evils of Redundancy v Redundancy is at the root of several problems associated with relational schemas: – redundant.
Schema Refinement and Normalization Nobody realizes that some people expend tremendous energy merely to be normal. Albert Camus.
Cs3431 Normalization Part II. cs3431 Attribute Closure : Example Consider R (A, B, C, D, E) with FDs A  B, B  C, CD  E Does A  E hold ? (Is A  E.
1 Schema Refinement and Normal Forms Yanlei Diao UMass Amherst April 10, 2007 Slides Courtesy of R. Ramakrishnan and J. Gehrke.
Database Management Systems, R. Ramakrishnan and J. Gehrke 1 Schema Refinement and Normal Forms Chapter 19 Instructor: Mirsad Hadzikadic.
Schema Refinement and Normal Forms Jianlin Feng School of Software SUN YAT-SEN UNIVERSITY courtesy of Joe Hellerstein for some slides.
Normal Forms1. 2 The Problems of Redundancy Redundancy is at the root of several problems associated with relational schemas: Wastes storage Causes problems.
CSCD34 - Data Management Systems - A. Vaisman1 Schema Refinement and Normal Forms.
Schema Refinement and Normalization. Functional Dependencies (Review) A functional dependency X  Y holds over relation schema R if, for every allowable.
Schema Refinement and Normal Forms Chapter 19 1 Database Management Systems 3ed, R.Ramakrishnan & J.Gehrke.
CS143 Review: Normalization Theory Q: Is it a good table design? We can start with an ER diagram or with a large relation that contain a sample of the.
1 Schema Refinement, Normalization, and Tuning. 2 Design Steps v The design steps: 1.Real-World 2. ER model 3. Relational Schema 4. Better relational.
Functional Dependencies and Normalization 1 Instructor: Mohamed Eltabakh
LECTURE 5: Schema Refinement and Normal Forms SOME OF THESE SLIDES ARE BASED ON YOUR TEXT BOOK.
Functional Dependencies and Normalization R&G Chapter 19 Lecture 26 Science is the knowledge of consequences, and dependence of one fact upon another.
Christoph F. Eick: Functional Dependencies, BCNF, and Normalization 1 Functional Dependencies, BCNF and Normalization.
Functional Dependencies And Normalization R&G Chapter 19.
Database Systems/COMP4910/Spring02/Melikyan1 Schema Refinement and Normal Forms.
1 Schema Refinement and Normal Forms Week 6. 2 The Evils of Redundancy  Redundancy is at the root of several problems associated with relational schemas:
Database Management Systems, R. Ramakrishnan and J. Gehrke1 Schema Refinement and Normal Forms Chapter 15.
Database Management Systems, 3ed, R. Ramakrishnan and J. Gehrke1 Schema Refinement and Normal Forms Chapter 19.
Schema Refinement and Normalization Nobody realizes that some people expend tremendous energy merely to be normal. Albert Camus.
1 Schema Refinement and Normal Forms Chapter The Evils of Redundancy  Redundancy is at the root of several problems associated with relational.
Database Management Systems, 3ed, R. Ramakrishnan and J. Gehrke1 Schema Refinement and Normal Forms Chapter 19.
1 Schema Refinement and Normal Forms Chapter The Evils of Redundancy  Redundancy causes several problems associated with relational schemas: 
Normalization and FUNctional Dependencies. Redundancy: root of several problems with relational schemas: –redundant storage, insert/delete/update anomalies.
CSC 411/511: DBMS Design Dr. Nan Wang 1 Schema Refinement and Normal Forms Chapter 19.
LECTURE 5: Schema Refinement and Normal Forms 1. Redundency Problem Redundent Storage Update Anomaly If one copy updated, an inconsistancy may occur Insertion.
1 CS122A: Introduction to Data Management Lecture #13: Relational DB Design Theory (II) Instructor: Chen Li.
CSC 411/511: DBMS Design Dr. Nan Wang 1 Schema Refinement and Normal Forms Chapter 19.
Functional Dependencies and Normalization 1 Instructor: Mohamed Eltabakh Part 2.
Schema Refinement & Normal Forms.
Functional Dependencies and Schema Refinement
Introduction to Database Systems Functional Dependencies
UNIT - 3 DATABASE DESIGN Functional Dependencies
Normalization First Normal Form (1NF) Boyce-Codd Normal Form (BCNF)
CS 480: Database Systems Lecture 22 March 6, 2013.
Handout 4 Functional Dependencies
Functional Dependencies and Normalization
Schema Refinement and Normalization
Module 5: Overview of Normalization
Normalization Murali Mani.
Schema Refinement and Normal Forms
Functional Dependencies and Normalization
Functional Dependencies and Normalization
LECTURE 5: Schema Refinement and Normal Forms
Normalization Part II cs3431.
Functional Dependencies and Normalization
Schema Refinement and Normalization
Normalization cs3431.
Instructor: Mohamed Eltabakh
Chapter 19 (part 1) Functional Dependencies
Schema Refinement and Normal Forms
Schema Refinement and Normalization
Instructor: Mohamed Eltabakh
Schema Refinement and Normal Forms
Chapter 7a: Overview of Database Design -- Normalization
Functional Dependencies and Normalization
Functional Dependencies and Normalization
CS4222 Principles of Database System
Presentation transcript:

Schema Refinement and Normal Forms Chapter 19 Integrity constraints, in particular functional dependencies, play an important role in the design of database schemas. In particular, they can shed light on potential redundancies (and the problems that go with redundancy) in a relational schema. Typically, they are used to analyze the relational schema obtained by converting an ER diagram. This chapter can be covered any time after the Foundations material (Chapters 1 to 5) is covered, at the instructor’s discretion. A good choice is to cover it after presenting all the implementation related material that is included in a course. This will allow a design sequence consisting of Chapters 19 and 20, and will enable the instructor to bring out the fact that design involves both redundancy analysis and performance considerations, and that these concerns should go hand-in-hand. 1

Schema Refinement : Normal Forms How decide if any refinement of schema is needed ? If a relation is in a certain normal form like BCNF, 3NF, etc. then certain problems are avoided. 22

Normal Forms Consider relation R with 3 attributes, ABC. No FDs hold: There is no redundancy here. Given A B: Several tuples could have same A value, and if so, they’ll all have the same B value repeated! 22

Normal Forms: BCNF Boyce Codd Normal Form (BCNF): For every non-trivial FD X  A in R, X is a (super)-key of R Note : trivial FD means A X Informally: R is in BCNF if the only (non-trivial) FDs that hold over R are all key constraints.

BCNF Example Is it in BCNF? SCI (student, course, instructor) FDs: instructor  course Is it in BCNF?

Third Normal Form (3NF) Relation R with FDs F is in 3NF if, for all X A in A X (called a trivial FD), or X contains a key for R, or A is a part of some key for R ( Minimality of key is crucial ) 24

3NF Example Lot (propNo, county, lotNum, area, price, taxRate) Candidate key: <county, lotNum> FDs: county  taxRate area  price Is it in 3NF?

3NF and BCNF ? If R is in BCNF, obviously R is in 3NF. If R is in 3NF, R may not be in BCNF. 24

3NF and BCNF ? If R is in 3NF, redundancy is possible. 3NF is a compromise used when BCNF not achievable, i.e., no ``good’’ decomposition exists, or, performance considerations Good decomposition of R into a collection of 3NF relations is always possible (where good means lossless-join and dependency-preserving ) 24

What Does 3NF Not Achieve? Even if relation is in 3NF, some problems could arise. Example: Reserves SBDC, S C, C S It is in 3NF? But for each reservation of sailor S, same (S, C) pair is stored. 3NF is a compromise relative to BCNF. 25

How get those Normal Forms? 26

How get those Normal Forms? Method: 1. analyze relation and its FDs 2. apply decomposition of R into smaller relations Decomposition of R replaces R by new relations so: Each new relation scheme contains a subset of attributes of R, and Every attribute of R appears as an attribute of one of the new relations. 26

Example Decomposition Revisited Decompositions should be used only when needed. SNLRWH has FDs S SNLRWH and R W Second FD causes violation of 3NF ! Thus W values repeatedly associated with R Fix this by decomposition: create a relation RW to store these associations i.e., decompose SNLHRW into SNLHR and RW 27

Careful When Decomposing ! Information stored consists of SNLRWH tuples; yet now stored across 2 tables. Any potential problems? 27

Decomposing Relations StudentProf sNumber sName pNumber pName s1 Dave p1 X s2 Greg p2 FDs: pNumber  pName Student Professor sNumber sName pNumber s1 Dave p1 s2 Greg p2 pNumber pName p1 X p2 Generating spurious tuples ?

Decomposition: Lossless Join Property! Student Professor sNumber sName pName S1 Dave X S2 Greg pNumber pName p1 X p2 FDs: pNumber  pName Generating spurious tuples ? StudentProf sNumber sName pNumber pName s1 Dave p1 X p2 s2 Greg

Problems with Decompositions Given instances of decomposed relations, not possible to reconstruct corresponding instance of original relation! Fortunately, not in SNLRWH example. Checking some dependencies may require joining the instances of the decomposed relations. Some queries become more expensive. e.g., How much did sailor Joe earn? (salary = W*H) Tradeoff: Must consider these issues vs. redundancy. 28

Lossless Join Decompositions All decompositions must be lossless! 29

Lossless Join Decompositions Decomposition of R into X and Y is lossless-join w.r.t. a set of FDs F if for every instance r that satisfies F: (r) (r) = r It is always true that r (r) (r) In general, the other direction may not hold! If it does, the decomposition is lossless-join. 29

Lossless Join: Necessary & Sufficient ! The decomposition of R into X and Y is lossless-join wrt F iff closure of F contains: X Y X, or X Y Y In particular, the decomposition of R into BC and R - C is lossless-join if B C holds over R. 30

Decomposition : Dependency Preserving ? Consider CSJDPQV, C is key, JP C and SD P. Decomposition: CSJDQV and SDP Is it lossless ? Yes ! Is it in BCNF ? Is it dependency preserving? Problem: Checking JP C requires a join! 3

Dependency Preserving Decomposition Property : Dependency preserving decomposition Intuition : If R is decomposed into X, Y and Z, and we enforce FDs that hold on X, Y and Z, separately, then all FDs that were given to hold on R must also hold. 3

Dependency Preserving Projection of set of FDs F: If R is decomposed into X, Y, ... then projection of F onto X (denoted FX ) is the set of FDs U V in F+ (closure of F ) such that U, V are in X. 3

Dependency Preserving Decompositions Formal Definition : Decomposition of R into X and Y is dependency preserving if (FX union FY ) + = F + Important to consider F +, not F : ABC, A B, B C, C A, decomposed into AB and BC. Is this dependency preserving? Is C A preserved ? 4

Dependency Preserving Decompositions Does dependency preserving imply lossless join? Example : ABC, A B, decomposed into AB and BC. Does lossless join imply dependency preserving ? Example: We saw a BCNF example earlier for that. 4

Algorithm : Decomposition into BCNF Consider relation R with FDs F. If X Y violates BCNF, then decompose R into R - Y and XY. Repeated application of this idea will result in: relations that are in BCNF; lossless join decomposition, and guaranteed to terminate. Note: In general, several dependencies may cause violation of BCNF. The order in which we ``deal with’’ them could lead to very different sets of relations! 5

Normalization Step Consider relation R with set of attributes AR. Consider a FD A  B such that no other attribute in (AR – A – B) is functionally determined by A. If A is not a superkey for R, we decompose R as: Create R’ (AR – B) Create R’’ with attributes A  B Key for R’’ = A

Algorithm : Decomposition into BCNF Example : CSJDPQV, key C, SD P, J S, JP C To deal with SD P, decompose into SDP, CSJDQV. To deal with J S, decompose CSJDQV into JS and CJDQV Result : Decomposition of CSJDQV into SDP, JS and CJDQV Is above decomposition lossless? Is above decomposition dependency-preserving ? 5

Algorithm : Decomposition into BCNF Example : CSJDPQV, key C, SD P, J S, JP C To deal with SD P, decompose into SDP, CSJDQV. To deal with J S, decompose CSJDQV into JS and CJDQV Result : Decomposition of CSJDQV into SDP, JS and CJDQV Is above decomposition lossless? Is above decomposition dependency-preserving ? 5

BCNF and Dependency Preservation In general, a dependency preserving decomposition into BCNF may not exist ! Example : CSZ, CS Z, Z C Not in BCNF. Can’t decompose while preserving 1st FD. 6

Decomposition into 3NF What about 3NF instead ? 7

Algorithm : Decomposition into 3NF Algorithm for lossless join decomp into BCNF can be used to obtain lossless join decomp into 3NF (typically, can stop earlier). But how to ensure dependency preservation? Idea 1: If X Y is not preserved, add relation XY. Problem is that XY may violate 3NF! Example : Consider the addition of CJP to `preserve’ JP C. What if we also have C J ? Idea 2 : Instead of the given set of FDs F, use a minimal cover for F. 7

Minimal Cover for a Set of FDs Minimal cover G for a set of FDs F: Closure of F = closure of G. Right hand side of each FD in G is a single attribute. If we modify G by deleting a FD or by deleting attributes from an FD in G, the closure changes. Intuition: every FD in G is needed, and ``as small as possible’’ in order to get the same closure as F. Example : If both J  C and JP  C, then only keep the first one. 8

Algorithm for Minimal Cover Decompose FD into one attribute on RHS Minimize left side of each FD Check each attribute on LHS to see if deleted while still preserving the equivalence to F+. Delete redundant FDs. Note: Several minimal covers may exist. 8

Example of Minimal Cover Given : A  B, ABCD  E, EF  GH, ACDF  EG Then the minimal cover is: A  B, ACD  E, EF  G and EF  H 8

Minimal Cover for a Set of FDs Theorem : Use minimum cover of FD+ in decomposition guarantees that the decomposition is Lossless-Join, Dep. Pres. Decomposition 8

3NF Decomposition Algorithm Compute minimal cover G of F Decompose R using minimal cover G of FD into lossless decomposition of R. Each Ri is in 3NF Fi is projection of F onto Ri (remember closure!) Identify dependencies in F not preserved now, X  A Create relation XA : New relation XA preserves X  A X is key of XA, because G is minimal cover. Hence no Y subset X exists, with Y  A If another dependency exists in XA; only attribute of X would be there. 8

Summary of Schema Refinement Step 1: BCNF is a good form for relation If a relation is in BCNF, it is free of redundancies that can be detected using FDs. Step 2 : If a relation is not in BCNF, we can try to decompose it into a collection of BCNF relations. Step 3: If a lossless-join, dependency preserving decomposition into BCNF is not possible (or unsuitable, given typical queries), then consider decomposition into 3NF. Note: Decompositions should be carried out and/or re-examined while keeping performance requirements in mind. 9