Chapter 8: Relational Database Design First Normal Form First Normal Form Functional Dependencies Functional Dependencies Decomposition Decomposition Boyce-Codd.

Slides:



Advertisements
Similar presentations
Schema Refinement: Normal Forms
Advertisements

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 16 Relational Database Design Algorithms and Further Dependencies.
Normalization 1 Instructor: Mohamed Eltabakh Part II.
Normalization Decomposition techniques for ensuring: Lossless joins Dependency preservation Redundancy avoidance We will look at some normal forms: Boyce-Codd.
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.
Chapter 7: Relational Database Design. ©Silberschatz, Korth and Sudarshan7.2Database System Concepts Chapter 7: Relational Database Design First Normal.
Dr. Kalpakis CMSC 461, Database Management Systems URL: Relational Database Design.
1 Normalization. 2 Normal Forms v If a relation is in a certain normal form (BCNF, 3NF etc.), it is known that certain kinds of redundancies are avoided/minimized.
©Silberschatz, Korth and Sudarshan Relational Database Design First Normal Form Pitfalls in Relational Database Design Functional Dependencies Decomposition.
7.1 Chapter 7: Relational Database Design. 7.2 Chapter 7: Relational Database Design Features of Good Relational Design Atomic Domains and First Normal.
Chapter 7: Relational Database Design. ©Silberschatz, Korth and Sudarshan7.2Database System Concepts Chapter 7: Relational Database Design First Normal.
Chapter 7: Relational Database Design. ©Silberschatz, Korth and Sudarshan7.2Database System Concepts Chapter 7: Relational Database Design First Normal.
Recap of Feb 13: SQL, Relational Calculi, Functional Dependencies SQL: multiple group bys, having, lots of examples Tuple Calculus Domain Calculus Functional.
Chapter 7: Relational Database Design. ©Silberschatz, Korth and Sudarshan7.2Database System Concepts Chapter 7: Relational Database Design First Normal.
Chapter 7: Relational Database Design. ©Silberschatz, Korth and Sudarshan7.2Database System Concepts Chapter 7: Relational Database Design First Normal.
Slides adapted from A. Silberschatz et al. Database System Concepts, 5th Ed. Relational Database Design - part 2 - Database Management Systems I Alex Coman,
Normal Form Design addendum by C. Zaniolo. ©Silberschatz, Korth and Sudarshan7.2Database System Concepts Normal Form Design Compute the canonical cover.
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.
1 CMSC424, Spring 2005 CMSC424: Database Design Lecture 9.
Relational Database Design Theory Lecture 6. The Banking Schema branch = (branch_name, branch_city, assets) customer = (customer_id, customer_name, customer_street,
1 Schema Refinement and Normal Forms Chapter 19 Raghu Ramakrishnan and J. Gehrke (second text book) In Course Pick-up box tomorrow.
Schema Refinement and Normalization Nobody realizes that some people expend tremendous energy merely to be normal. Albert Camus.
Chapter 7: Relational Database Design. ©Silberschatz, Korth and Sudarshan7.2Database System Concepts Chapter 7: Relational Database Design First Normal.
Normalization. FarkasCSCE 5202 Reading Assignments  Database Systems The Complete Book: Chapters 3.6, 3.7, 3.8  Following lecture slides are modified.
1 Triggers: Correction. 2 Mutating Tables (Explanation) The problems with mutating tables are mainly with FOR EACH ROW triggers STATEMENT triggers can.
Department of Computer Science and Engineering, HKUST Slide 1 7. Relational Database Design.
Boyce-Codd Normal Form By: Thanh Truong. Boyce-Codd Normal Form Eliminates all redundancy that can be discovered by functional dependencies But, we can.
Chapter 8: Relational Database Design First Normal Form First Normal Form Functional Dependencies Functional Dependencies Decomposition Decomposition Boyce-Codd.
Relational Database Design
©Silberschatz, Korth and Sudarshan7.1Database System Concepts Chapter 7: Relational Database Design First Normal Form Pitfalls in Relational Database Design.
Chapter 7: Relational Database Design. ©Silberschatz, Korth and Sudarshan7.2 Chapter 7: Relational Database Design First Normal Form Goals of Relational.
Chapter 7: Relational Database Design. 7.2Unite International CollegeDatabase Management Systems Chapter 7: Relational Database Design Features of Good.
 Features of Good Relational Design  Atomic Domains and First Normal Form  Decomposition Using Functional Dependencies  Functional Dependency Theory.
FUNCTIONAL DEPENDENCIES. Chapter Outline 1 Informal Design Guidelines for Relational Databases 1.1Semantics of the Relation Attributes 1.2 Redundant Information.
Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 7: Relational.
Chapter 7: Relational Database Design. 7.2Unite International CollegeDatabase Management Systems Chapter 7: Relational Database Design Features of Good.
Database Systems Normal Forms. Decomposition Suppose we have a relation R[U] with a schema U={A 1,…,A n } – A decomposition of U is a set of schemas.
Chapter 7: Relational Database Design. ©Silberschatz, Korth and Sudarshan7.2Database System Concepts Chapter 7: Relational Database Design Pitfalls in.
Relational Database Design by Relational Database Design by Dr.S.Sridhar, Ph.D.(JNUD), RACI(Paris, NICE), RMR(USA), RZFM(Germany) DIRECTOR ARUNAI ENGINEERING.
Schema Refinement and Normalization. Functional Dependencies (Review) A functional dependency X  Y holds over relation schema R if, for every allowable.
Functional Dependencies An example: loan-info= Observe: tuples with the same value for lno will always have the same value for amt We write: lno  amt.
SCUJ. Holliday - coen 1784–1 Schedule Today: u Normal Forms. u Section 3.6. Next u Relational Algebra. Read chapter 5 to page 199 After that u SQL Queries.
Computing & Information Sciences Kansas State University Tuesday, 27 Feb 2007CIS 560: Database System Concepts Lecture 18 of 42 Tuesday, 27 February 2007.
Functional Dependencies and Normalization 1 Instructor: Mohamed Eltabakh
Computing & Information Sciences Kansas State University Wednesday, 04 Oct 2006CIS 560: Database System Concepts Lecture 17 of 42 Wednesday, 04 October.
Chapter 7: Relational Database Design. ©Silberschatz, Korth and Sudarshan7.2Database System Concepts Chapter 7: Relational Database Design First Normal.
Database System Concepts ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 7: Relational Database.
Chapter 7: Relational Database Design. ©Silberschatz, Korth and Sudarshan7.2Database System Concepts Chapter 7: Relational Database Design First Normal.
Temple University – CIS Dept. CIS331– Principles of Database Systems V. Megalooikonomou Database Design and Normalization (based on notes by Silberchatz,Korth,
CS 338Database Design and Normal Forms9-1 Database Design and Normal Forms Lecture Topics Measuring the quality of a schema Schema design with normalization.
Rensselaer Polytechnic Institute CSCI-4380 – Database Systems David Goldschmidt, Ph.D.
© D. Wong Functional Dependencies (FD)  Given: relation schema R(A1, …, An), and X and Y be subsets of (A1, … An). FD : X  Y means X functionally.
Chapter 7: Relational Database Design. ©Silberschatz, Korth and Sudarshan7.2Database System Concepts Chapter 7: Relational Database Design First Normal.
Chapter 8 Relational Database Design. 2 Relational Database Design: Goals n Reduce data redundancy (undesirable replication of data values) n Minimize.
1 Dept. of CIS, Temple Univ. CIS661 – Principles of Data Management V. Megalooikonomou Database design and normalization (based on slides by C. Faloutsos.
Normal Forms Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems June 18, 2016 Some slide content courtesy of Susan Davidson.
Module 5: Overview of Database Design -- Normalization
Normalization First Normal Form (1NF) Boyce-Codd Normal Form (BCNF)
Relational Database Design by Dr. S. Sridhar, Ph. D
Relational Database Design
CS 480: Database Systems Lecture 22 March 6, 2013.
Relational Database Design
Chapter 7: Relational Database Design
Functional Dependencies and Normalization
Module 5: Overview of Normalization
Chapter 7: Relational Database Design
Instructor: Mohamed Eltabakh
Chapter 7a: Overview of Database Design -- Normalization
Functional Dependencies and Normalization
Functional Dependencies and Normalization
Presentation transcript:

Chapter 8: Relational Database Design First Normal Form First Normal Form Functional Dependencies Functional Dependencies Decomposition Decomposition Boyce-Codd Normal Form Boyce-Codd Normal Form Third Normal Form Third Normal Form

Goals of Normalization Decide whether a particular relation R is in “good” form. Decide whether a particular relation R is in “good” form. In the case that a relation R is not in “good” form, decompose it into a set of relations {R 1, R 2,..., R n } such that In the case that a relation R is not in “good” form, decompose it into a set of relations {R 1, R 2,..., R n } such that the decomposition is a lossless-join decomposition the decomposition is a lossless-join decomposition each relation is in good form each relation is in good form

Decomposition Decompose the relation schema Lending-schema into: Decompose the relation schema Lending-schema into: Branch-schema = (branch-name, branch-city,assets) Loan-info-schema = (customer-name, loan-number, branch-name, amount) All attributes of an original schema (R) must appear in the decomposition (R 1, R 2 ): All attributes of an original schema (R) must appear in the decomposition (R 1, R 2 ): R = R 1  R 2 Lossless-join decomposition. For all possible relations r on schema R Lossless-join decomposition. For all possible relations r on schema R r =  R1 (r)  R2 (r)

Example of Lossy-Join Decomposition Lossy-join decompositions result in information loss. Lossy-join decompositions result in information loss. Example: Decomposition of R = (A, B) R 1 = (A)R 2 = (B) Example: Decomposition of R = (A, B) R 1 = (A)R 2 = (B) AB  A  B 1212 r A(r)A(r)  B(r)  A (r)  B (r) AB 

A decomposition of R into R 1 and R 2 is lossless join if and only if at least one of the following dependencies is in F + : A decomposition of R into R 1 and R 2 is lossless join if and only if at least one of the following dependencies is in F + : R 1  R 2  R 1 R 1  R 2  R 1or R 1  R 2  R 2 R 1  R 2  R 2

Relation is in good form If the relations R i preferably should be in either Boyce-Codd Normal Form (BCNF) or Third (3NF) Normal Form If the relations R i preferably should be in either Boyce-Codd Normal Form (BCNF) or Third (3NF) Normal Form BCNF and 3NF eliminate redundancy BCNF and 3NF eliminate redundancy What is BCNF? What is BCNF? What is 3NF? What is 3NF?

Boyce-Codd Normal Form     is trivial (i.e.,    )     is trivial (i.e.,    )  is a superkey for R  is a superkey for R A relation schema R is in BCNF with respect to a set F of functional dependencies if for all functional dependencies in F + of the form    , where   R and   R, at least one of the following holds:

Example R = (A, B, C) F = {A  B, B  C} Key = {A} R = (A, B, C) F = {A  B, B  C} Key = {A} R is not in BCNF R is not in BCNF Decomposition R 1 = (A, B), R 2 = (B, C) Decomposition R 1 = (A, B), R 2 = (B, C) R 1 and R 2 in BCNF R 1 and R 2 in BCNF Lossless-join decomposition Lossless-join decomposition

Testing for BCNF To check if a non-trivial dependency     causes a violation of BCNF To check if a non-trivial dependency     causes a violation of BCNF 1. compute  + (the attribute closure of  ), and 2. verify that it includes all attributes of R, that is, it is a superkey of R. Simplified test: To check if a relation schema R is in BCNF, it suffices to check only the dependencies in the given set F for violation of BCNF, rather than checking all dependencies in F +. Simplified test: To check if a relation schema R is in BCNF, it suffices to check only the dependencies in the given set F for violation of BCNF, rather than checking all dependencies in F +. If none of the dependencies in F causes a violation of BCNF, then none of the dependencies in F + will cause a violation of BCNF either. If none of the dependencies in F causes a violation of BCNF, then none of the dependencies in F + will cause a violation of BCNF either.

However, using only F is incorrect when testing a relation in a decomposition of R However, using only F is incorrect when testing a relation in a decomposition of R E.g. Consider R (A, B, C, D), with F = { A  B, B  C} E.g. Consider R (A, B, C, D), with F = { A  B, B  C} Decompose R into R 1 (A,B) and R 2 (A,C,D) Decompose R into R 1 (A,B) and R 2 (A,C,D) Neither of the dependencies in F contain only attributes from (A,C,D) so we might be mislead into thinking R 2 satisfies BCNF. Neither of the dependencies in F contain only attributes from (A,C,D) so we might be mislead into thinking R 2 satisfies BCNF. In fact, dependency A  C in F + shows R 2 is not in BCNF. In fact, dependency A  C in F + shows R 2 is not in BCNF.

BCNF Decomposition Algorithm result := {R}; done := false; compute F + ; while (not done) do if (there is a schema R i in result that is not in BCNF) then begin let     be a nontrivial functional dependency that holds on R i such that    R i is not in F +, and    =  ; result := (result – R i )  (R i –  )  ( ,  ); end else done := true; dependency that holds on R i such that    R i is not in F +, and    =  ; result := (result – R i )  (R i –  )  ( ,  ); end else done := true;

Testing if Ri is in BCNF To check if a relation R i in a decomposition of R is in BCNF To check if a relation R i in a decomposition of R is in BCNF Use the original set of dependencies F that hold on R, but with the following test: Use the original set of dependencies F that hold on R, but with the following test: for every set of attributes   R i, check that  + (the attribute closure of  ) either includes no attribute of R i - , or includes all attributes of R i. for every set of attributes   R i, check that  + (the attribute closure of  ) either includes no attribute of R i - , or includes all attributes of R i. If the condition is violated by some     in F, the dependency    (  + -   )  R i can be shown to hold on R i, and R i violates BCNF. If the condition is violated by some     in F, the dependency    (  + -   )  R i can be shown to hold on R i, and R i violates BCNF. We use above dependency to decompose R i We use above dependency to decompose R i

Example of BCNF Decomposition R = (branch-name, branch-city, assets, R = (branch-name, branch-city, assets, customer-name, loan-number, amount) F = {branch-name  assets, branch-city loan-number  amount, branch-name} Key = {loan-number, customer-name} Given: loan-number  branch-name, amount loan-number  branch-name, amount branch-name  branch-city, assets branch-name  branch-city, assets customer-name,loan-number  Lending-schema customer-name,loan-number  Lending-schema Decomposition Decomposition R 1 = (branch-name, branch-city, assets) R 1 = (branch-name, branch-city, assets) R 2 = (branch-name, customer-name, loan-number, amount) R 2 = (branch-name, customer-name, loan-number, amount) R 3 = (branch-name, loan-number, amount) R 3 = (branch-name, loan-number, amount) R 4 = (customer-name, loan-number) R 4 = (customer-name, loan-number) Final decomposition R 1, R 3, R 4 Final decomposition R 1, R 3, R 4

Additional Constraint of Decomposition Dependency preservation: Let F i be the set of dependencies F + that include only attributes in R i. Dependency preservation: Let F i be the set of dependencies F + that include only attributes in R i. Preferably the decomposition should be dependency preserving, that is, (F 1  F 2  …  F n ) + = F + Preferably the decomposition should be dependency preserving, that is, (F 1  F 2  …  F n ) + = F + Otherwise, checking updates for violation of functional dependencies may require computing joins, which is expensive. Otherwise, checking updates for violation of functional dependencies may require computing joins, which is expensive.

Example R = (A, B, C), F = {A  B, B  C) R = (A, B, C), F = {A  B, B  C) Can be decomposed in two different ways Can be decomposed in two different ways R 1 = (A, B), R 2 = (B, C) R 1 = (A, B), R 2 = (B, C) Lossless-join decomposition: Lossless-join decomposition: R 1  R 2 = {B} and B  BC R 1  R 2 = {B} and B  BC Dependency preserving Dependency preserving R 1 = (A, B), R 2 = (A, C) R 1 = (A, B), R 2 = (A, C) Lossless-join decomposition: Lossless-join decomposition: R 1  R 2 = {A} and A  AB R 1  R 2 = {A} and A  AB Not dependency preserving (cannot check B  C without computing R 1 R 2 ) Not dependency preserving (cannot check B  C without computing R 1 R 2 )

Testing for Dependency Preservation To check if a dependency  is preserved in a decomposition of R into R 1, R 2, …, R n we apply the following simplified test (with attribute closure done w.r.t. F) To check if a dependency  is preserved in a decomposition of R into R 1, R 2, …, R n we apply the following simplified test (with attribute closure done w.r.t. F) result =  while (changes to result) do for each R i in the decomposition t = (result  R i ) +  R i result = result  t result =  while (changes to result) do for each R i in the decomposition t = (result  R i ) +  R i result = result  t If result contains all attributes in , then the functional dependency    is preserved. If result contains all attributes in , then the functional dependency    is preserved.

BCNF and Dependency Preservation R = (J, K, L) F = {JK  L, L  K} Two candidate keys = JK and JL R = (J, K, L) F = {JK  L, L  K} Two candidate keys = JK and JL R is not in BCNF R is not in BCNF Any decomposition of R will fail to preserve JK  L Any decomposition of R will fail to preserve JK  L It is not always possible to get a BCNF decomposition that is dependency preserving

Third Normal Form: Motivation There are some situations where There are some situations where BCNF is not dependency preserving, and BCNF is not dependency preserving, and efficient checking for FD violation on updates is important efficient checking for FD violation on updates is important Solution: define a weaker normal form, called Third Normal Form. Solution: define a weaker normal form, called Third Normal Form. Allows some redundancy (with resultant problems; we will see examples later) Allows some redundancy (with resultant problems; we will see examples later) But FDs can be checked on individual relations without computing a join. But FDs can be checked on individual relations without computing a join. There is always a lossless-join, dependency- preserving decomposition into 3NF. There is always a lossless-join, dependency- preserving decomposition into 3NF.

Third Normal Form A relation schema R is in third normal form (3NF) if for all: A relation schema R is in third normal form (3NF) if for all:    in F + at least one of the following holds:    is trivial (i.e.,    )    is trivial (i.e.,    )  is a superkey for R  is a superkey for R Each attribute A in  –  is contained in a candidate key for R. Each attribute A in  –  is contained in a candidate key for R. (NOTE: each attribute may be in a different candidate key) (NOTE: each attribute may be in a different candidate key)

If a relation is in BCNF it is in 3NF (since in BCNF one of the first two conditions above must hold). If a relation is in BCNF it is in 3NF (since in BCNF one of the first two conditions above must hold). Third condition is a minimal relaxation of BCNF to ensure dependency preservation (will see why later). Third condition is a minimal relaxation of BCNF to ensure dependency preservation (will see why later).

Example R = (J, K, L) F = {JK  L, L  K} R = (J, K, L) F = {JK  L, L  K} Two candidate keys: JK and JL Two candidate keys: JK and JL R is in 3NF R is in 3NF JK  LJK is a superkey L  KK is contained in a candidate key n BCNF decomposition has (JL) and (LK) n Testing for JK  L requires a join There is some redundancy in this schema There is some redundancy in this schema

Testing for 3NF Optimization: Need to check only FDs in F, need not check all FDs in F +. Optimization: Need to check only FDs in F, need not check all FDs in F +. Use attribute closure to check for each dependency   , if  is a superkey. Use attribute closure to check for each dependency   , if  is a superkey. If  is not a superkey, we have to verify if each attribute in  is contained in a candidate key of R If  is not a superkey, we have to verify if each attribute in  is contained in a candidate key of R testing for 3NF has been shown to be NP-hard testing for 3NF has been shown to be NP-hard Interestingly, decomposition into third normal form (described shortly) can be done in polynomial time Interestingly, decomposition into third normal form (described shortly) can be done in polynomial time

3NF Decomposition Algorithm Let F c be a canonical cover for F; i := 0; for each functional dependency    in F c do if none of the schemas R j, 1  j  i contains   then begin i := i + 1; R i :=   end if none of the schemas R j, 1  j  i contains a candidate key for R then begin i := i + 1; R i := any candidate key for R; end return (R 1, R 2,..., R i )

3NF Decomposition Algorithm Above algorithm ensures: Above algorithm ensures: each relation schema R i is in 3NF each relation schema R i is in 3NF decomposition is dependency preserving and lossless- join decomposition is dependency preserving and lossless- join We skip the proof of correctness We skip the proof of correctness

Example Relation schema: Relation schema: Banker-info-schema = (branch-name, customer- name, banker-name, office-number) The functional dependencies for this relation schema are: banker-name  branch-name,office-number customer-name, branch-name  banker-name The functional dependencies for this relation schema are: banker-name  branch-name,office-number customer-name, branch-name  banker-name The key is: The key is: {customer-name, branch-name}

Applying 3NF to Banker-info- schema The for loop in the algorithm causes us to include the following schemas in our decomposition: The for loop in the algorithm causes us to include the following schemas in our decomposition: Banker-office-schema = (banker-name, branch- name, office-number) Banker-schema = (customer-name, branch-name, banker-name) Since Banker-schema contains a candidate key for Banker-info-schema, we are done Since Banker-schema contains a candidate key for Banker-info-schema, we are done

Comparison of BCNF and 3NF It is always possible to decompose a relation into relations in 3NF and It is always possible to decompose a relation into relations in 3NF and the decomposition is lossless the decomposition is lossless the dependencies are preserved the dependencies are preserved It is always possible to decompose a relation into relations in BCNF and It is always possible to decompose a relation into relations in BCNF and the decomposition is lossless the decomposition is lossless it may not be possible to preserve dependencies. it may not be possible to preserve dependencies.

Comparison of BCNF and 3NF J j 1 j 2 j 3 null L l1l1l1l2l1l1l1l2 K k1k1k1k2k1k1k1k2 A schema that is in 3NF but not in BCNF has the problems of repetition of information (e.g., the relationship l 1, k 1 ) need to use null values (e.g., to represent the relationship l 2, k 2 where there is no corresponding value for J). Example of problems due to redundancy in 3NF Example of problems due to redundancy in 3NF R = (J, K, L) F = {JK  L, L  K} R = (J, K, L) F = {JK  L, L  K}

Design Goals Goal for a relational database design is: Goal for a relational database design is: BCNF. BCNF. Lossless join. Lossless join. Dependency preservation. Dependency preservation. If we cannot achieve this, we accept one of If we cannot achieve this, we accept one of Lack of dependency preservation Lack of dependency preservation Redundancy due to use of 3NF Redundancy due to use of 3NF