Schema Refinement and Normal Forms

Similar documents
Schema Refinement and Normal Forms. The Evils of Redundancy. Functional Dependencies (FDs) [R&G] Chapter 19

The Evils of Redundancy. Schema Refinement and Normal Forms. Example: Constraints on Entity Set. Functional Dependencies (FDs) Refining an ER Diagram

The Evils of Redundancy. Schema Refinement and Normal Forms. Example: Constraints on Entity Set. Functional Dependencies (FDs) Example (Contd.

The Evils of Redundancy. Schema Refinement and Normal Forms. Functional Dependencies (FDs) Example: Constraints on Entity Set. Example (Contd.

Schema Refinement and Normal Forms. The Evils of Redundancy. Functional Dependencies (FDs) CIS 330, Spring 2004 Lecture 11 March 2, 2004

Schema Refinement and Normal Forms

The Evils of Redundancy. Schema Refinement and Normalization. Functional Dependencies (FDs) Example: Constraints on Entity Set. Refining an ER Diagram

Schema Refinement and Normal Forms. The Evils of Redundancy. Schema Refinement. Yanlei Diao UMass Amherst April 10, 2007

Schema Refinement and Normalization

Schema Refinement and Normal Forms

Schema Refinement and Normal Forms Chapter 19

Schema Refinement and Normal Forms. Case Study: The Internet Shop. Redundant Storage! Yanlei Diao UMass Amherst November 1 & 6, 2007

Schema Refinement and Normal Forms. Chapter 19

Schema Refinement and Normal Forms

Schema Refinement & Normalization Theory

CS 186, Fall 2002, Lecture 6 R&G Chapter 15

CAS CS 460/660 Introduction to Database Systems. Functional Dependencies and Normal Forms 1.1

Schema Refinement & Normalization Theory: Functional Dependencies INFS-614 INFS614, GMU 1

Schema Refinement. Yanlei Diao UMass Amherst. Slides Courtesy of R. Ramakrishnan and J. Gehrke

Functional Dependencies

CS122A: Introduction to Data Management. Lecture #13: Relational DB Design Theory (II) Instructor: Chen Li

Introduction to Data Management. Lecture #6 (Relational DB Design Theory)

12/3/2010 REVIEW ALGEBRA. Exam Su 3:30PM - 6:30PM 2010/12/12 Room C9000

Introduction to Data Management. Lecture #7 (Relational DB Design Theory II)

Introduction to Data Management. Lecture #6 (Relational Design Theory)

Introduction to Data Management. Lecture #9 (Relational Design Theory, cont.)

Lecture #7 (Relational Design Theory, cont d.)

Normal Forms (ii) ICS 321 Fall Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa

Schema Refinement and Normal Forms. Why schema refinement?

Normal Forms 1. ICS 321 Fall Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa

Introduction. Normalization. Example. Redundancy. What problems are caused by redundancy? What are functional dependencies?

Introduction to Data Management. Lecture #10 (DB Design Wrap-up)

MIS Database Systems Schema Refinement and Normal Forms

Functional Dependencies, Schema Refinement, and Normalization for Relational Databases. CSC 375, Fall Chapter 19

MIS Database Systems Schema Refinement and Normal Forms

Schema Refinement. Feb 4, 2010

Introduction to Data Management. Lecture #10 (Relational Design Theory, cont.)

Relational Database Design

Normalization. October 5, Chapter 19. CS445 Pacific University 1 10/05/17

SCHEMA NORMALIZATION. CS 564- Fall 2015

UVA UVA UVA UVA. Database Design. Relational Database Design. Functional Dependency. Loss of Information

Functional Dependencies & Normalization. Dr. Bassam Hammo

FUNCTIONAL DEPENDENCY THEORY. CS121: Relational Databases Fall 2017 Lecture 19

Constraints: Functional Dependencies

Database Design and Normalization

CSC 261/461 Database Systems Lecture 10 (part 2) Spring 2018

Functional Dependencies and Normalization

Database Normaliza/on. Debapriyo Majumdar DBMS Fall 2016 Indian Statistical Institute Kolkata

Lecture 15 10/02/15. CMPSC431W: Database Management Systems. Instructor: Yu- San Lin

Lectures 6. Lecture 6: Design Theory

CS54100: Database Systems

Database Design and Implementation

Constraints: Functional Dependencies

CMPT 354: Database System I. Lecture 9. Design Theory

Functional Dependency and Algorithmic Decomposition

INF1383 -Bancos de Dados

CSC 261/461 Database Systems Lecture 13. Spring 2018

Information Systems (Informationssysteme)

10/12/10. Outline. Schema Refinements = Normal Forms. First Normal Form (1NF) Data Anomalies. Relational Schema Design

CSC 261/461 Database Systems Lecture 8. Spring 2017 MW 3:25 pm 4:40 pm January 18 May 3 Dewey 1101

Design Theory: Functional Dependencies and Normal Forms, Part I Instructor: Shel Finkelstein

Design Theory for Relational Databases

CS322: Database Systems Normalization

A few details using Armstrong s axioms. Supplement to Normalization Lecture Lois Delcambre

Information Systems for Engineers. Exercise 8. ETH Zurich, Fall Semester Hand-out Due

Func8onal Dependencies

Normal Forms. Dr Paolo Guagliardo. University of Edinburgh. Fall 2016

Design theory for relational databases

Chapter 8: Relational Database Design

DESIGN THEORY FOR RELATIONAL DATABASES. csc343, Introduction to Databases Renée J. Miller and Fatemeh Nargesian and Sina Meraji Winter 2018

Design Theory. Design Theory I. 1. Normal forms & functional dependencies. Today s Lecture. 1. Normal forms & functional dependencies

Functional Dependencies

Normaliza)on and Func)onal Dependencies

Functional Dependencies. Getting a good DB design Lisa Ball November 2012

DECOMPOSITION & SCHEMA NORMALIZATION

Chapter 3 Design Theory for Relational Databases

11/6/11. Relational Schema Design. Relational Schema Design. Relational Schema Design. Relational Schema Design (or Logical Design)

11/1/12. Relational Schema Design. Relational Schema Design. Relational Schema Design. Relational Schema Design (or Logical Design)

CSE 344 MAY 16 TH NORMALIZATION

COSC 430 Advanced Database Topics. Lecture 2: Relational Theory Haibo Zhang Computer Science, University of Otago

CSC 261/461 Database Systems Lecture 12. Spring 2018

CSE 132B Database Systems Applications

Chapter 7: Relational Database Design

Introduction to Database Systems CSE 414. Lecture 20: Design Theory

Introduction to Data Management CSE 344

Functional Dependencies. Applied Databases. Not all designs are equally good! An example of the bad design

Relational Design: Characteristics of Well-designed DB

Review: Keys. What is a Functional Dependency? Why use Functional Dependencies? Functional Dependency Properties

CSE 344 AUGUST 3 RD NORMALIZATION

Relational Design Theory

Chapter 7: Relational Database Design. Chapter 7: Relational Database Design

CSE 344 AUGUST 6 TH LOSS AND VIEWS

Functional Dependencies and Normalization. Instructor: Mohamed Eltabakh

Chapter 10. Normalization Ext (from E&N and my editing)

Functional Dependency Theory II. Winter Lecture 21

CSC 261/461 Database Systems Lecture 11

Schema Refinement: Other Dependencies and Higher Normal Forms

CSE 544 Principles of Database Management Systems

Functional Dependencies and Normalization

CSIT5300: Advanced Database Systems

Transcription:

Schema Refinement and Normal Forms Chapter 19 Quiz #2 Next Thursday Comp 521 Files and Databases Fall 2012 1

The Evils of Redundancy v Redundancy is at the root of several problems associated with relational schemas: redundant storage, insert/delete/update anomalies v Integrity constraints, in particular functional dependencies, can be used to identify schemas with such problems and to suggest refinements. v Main refinement technique: decomposition (replacing ABCD with, say, AB and BCD, or ACD and ABD). v Decomposition should be used judiciously: Is there reason to decompose a relation? What problems (if any) does the decomposition cause? Comp 521 Files and Databases Fall 2012 2

Functional Dependencies (FDs) v A functional dependency Xà Y holds over relation R if, for every allowable instance r of R: for all t 1 r, t 2 r, π X (t 1 ) = π X (t 2 ) implies π Y (t 1 ) = π Y (t 2 ) i.e., given two tuples in r, if the X values agree, then the Y values must also agree. (X and Y are sets of attributes.) v An FD is a statement about all allowable relations. Must be identified based on semantics of application. Given some allowable instance r 1 of R, we can check if it violates some FD f, but we cannot tell if f holds over R! v K is a candidate key for R means that K à R However, K à R does not require K to be minimal! Comp 521 Files and Databases Fall 2012 3

Example: Constraints on Entity Set v Consider relation obtained from Hourly_Emps: Hourly_Emps (ssn, name, lot, rating, hrly_wages, hrs_worked) v Notation: We will denote this relation schema by listing the attributes as a single letter: SNLRWH This is really the set of attributes {S,N,L,R,W,H}. Sometimes, we will refer to all attributes of a relation by using the relation name. (e.g., Hourly_Emps for SNLRWH) v Some FDs on Hourly_Emps: ssn is the key: S à SNLRWH rating determines hrly_wages: R à W Comp 521 Files and Databases Fall 2012 4

Example (Contd.) v Problems due to R à W : Update anomaly: Can we change W in just the 1st tuple of SNLRWH? Insertion anomaly: What if we want to insert an employee and don t know the hourly wage for his rating? Deletion anomaly: If we delete all employees with rating 5, we lose the information about the wage for rating 5! Will 2 smaller tables be better? Hourly_Emps S N L R W H 123-22-3666 Attishoo 48 8 10 40 231-31-5368 Smiley 22 8 10 30 131-24-3650 Smethurst 35 5 7 30 434-26-3751 Guldu 35 5 7 32 612-67-4134 Madayan 35 8 10 40 Hourly_Emps2 S N L R H 123-22-3666 Attishoo 48 8 40 231-31-5368 Smiley 22 8 30 131-24-3650 Smethurst 35 5 30 434-26-3751 Guldu 35 5 32 612-67-4134 Madayan 35 8 40 Wages R W 8 10 Comp 521 Files and Databases Fall 2012 5 5 7

Reasoning About FDs v Given some FDs, we can usually infer additional FDs: ssn à did, did à lot implies ssn à lot v An FD f is implied by a set of FDs F if f holds whenever all FDs in F hold. F + = closure of F is the set of all FDs that are implied by F. v Armstrong s Axioms (X, Y, Z are sets of attributes): Reflexivity: If X Y, then Y à X Augmentation: If X à Y, then XZ à YZ for any Z Transitivity: If X à Y and Y à Z, then X à Z v These are sound and complete inference rules for FDs! sound: they will generate only FDs in F + complete: repeated applications will generate all FDs in F + Comp 521 Files and Databases Fall 2012 6

Reasoning About FDs (Contd.) v Couple of additional rules (that follow from AA): Union: If X à Y and X à Z, then X à YZ Decomposition: If X à YZ, then X à Y and X à Z v Example: Contracts(cid,sid,jid,did,pid,qty,value), and: C is the key: C à CSJDPQV Projects purchase each part using single contract: JP à C Dept purchase at most one part from a supplier: SD à P v JP à C, C à CSJDPQV imply JP à CSJDPQV v SD à P implies SDJ à JP v SDJ à JP, JP à CSJDPQV imply SDJ à CSJDPQV Comp 521 Files and Databases Fall 2012 7

Reasoning About FDs (Contd.) v Computing the closure of a set of FDs can be expensive. (Size of closure is exponential in # attrs!) v Typically, we just want to check if a given FD X à Y is in the closure of a set of FDs F. An efficient check: Compute attribute closure of X (denoted X + ) wrt F: Set of all attributes A such that X à A is in F + There is a linear time algorithm to compute this. Check if Y is in X + Comp 521 Files and Databases Fall 2012 8

Example Check X? Y Z W U FDs: X à W X à Z WZ à U U à Y v Does F = {A à B, B à C, C D à E } imply A à E? i.e, is A à E in the closure F +? Equivalently, is E in A +? Comp 521 Files and Databases Fall 2012 9

Normal Forms v To eliminate redundancy and potential update anomalies, one can identify generic templates called normal forms v If a relation is in a certain normal form (BCNF, 3NF etc.), it is known that certain kinds of problems are avoided/minimized. This can be used to help us decide whether decomposing the relation will help. v Role of FDs in detecting redundancy: Consider a relation R with 3 attributes, ABC. No FDs hold: There is no redundancy here. Given A à B: Several tuples could have the same A value, and if so, they ll all have the same B value! Comp 521 Files and Databases Fall 2012 10

Normal Form Hierarchy v An increasingly stringent hierarchy of Normal Forms v Each outer form trivially satisfies the requirements of inner forms 4NF BCNF 3NF 2NF 1NF v The 1 st normal form (1NF) is part of the definition of the relational model. Relations must be sets (unique) and all attributes atomic (not multiple fields or variable length records). v The 2 nd normal form (2NF) requires schemas not have any FD, X à Y, where X as a strict subset of the schema s key. Comp 521 Files and Databases Fall 2012 11

Boyce-Codd Normal Form (BCNF) v Relation R with FDs F is in BCNF if, for all X à A in F + A X (called a trivial FD), or X contains a key for R. v In other words, R is in BCNF if the only non-trivial FDs that hold over R are key constraints. v BCNF considers all domain keys, not just the primary one v BCNF schemas do not contain redundant information that arise from FDs Comp 521 Files and Databases Fall 2012 12

BCNF Examples v In BCNF Person(First, Last, Address, Phone) Functional Dependencies: FL à A, FL à P v Not in BCNF Person(First, Last, Address, Phone, Email) An attempt to allow a person to have multiple emails. Functional Dependencies: FL à A, FL à P Comp 521 Files and Databases Fall 2012 13

Third Normal Form (3NF) v Reln R with FDs F is in 3NF if, for all X à A in F + A X (called a trivial FD), or X contains a key for R, or A is part of some key for R. v Minimality of a key is crucial in third condition above! v If R is in BCNF, it is trivially in 3NF. v If R is in 3NF, some redundancy is possible. It is a compromise, used when BCNF not achievable (e.g., no good decomp, or performance considerations). Lossless-join, dependency-preserving decomposition of R into a collection of 3NF relations always possible. Comp 521 Files and Databases Fall 2012 14

3NF Examples v Phonebook where friends have multiple addresses v In 3NF, not in BCNF Person(First, Last, Addr, Phone) Functional Dependencies: FLA à P, P à A v Not in 3NF or BCNF Person(First, Last, Addr, Phone, Mobile) Functional Dependencies: FLA à P, P à A, FL à M Comp 521 Files and Databases Fall 2012 15

What Does 3NF Achieve? v If 3NF is violated by X à A, one of the following holds: X is a proper subset of some key K (partial dependency) We store (X, A) pairs redundantly. X is not a proper subset of any key (transitive dependency). There is a chain of FDs K à X à A, which means that we cannot associate an X value with a K value unless we also associate an A value with an X value. v But, even if relation is in 3NF, problems can arise. Comp 521 Files and Databases Fall 2012 16

Lingering 3NF Redundancies v Revisiting an old Schema Reserves(Sailor, Boat, Date, CreditCardNo) FDs: Cà S v In 3NF, but database likely stores many redundant copies of the (C, S) tuple v Thus, 3NF is indeed a compromise relative to BCNF. Comp 521 Files and Databases Fall 2012 17

Decomposition of a Relation Scheme v Suppose that relation R contains attributes A1... An. A decomposition of R consists of replacing R by two or more relations such that: Each new relation scheme contains a subset of the attributes of R (and no attributes that do not appear in R), and Every attribute of R appears as an attribute of one of the new relations. v Intuitively, decomposing R means we will store instances of the relation schemes produced by the decomposition, instead of instances of R. v E.g., Can decompose SNLRWH into SNLRH and RW. Comp 521 Files and Databases Fall 2012 18

Example Decomposition v Decompositions should be used only when needed. SNLRWH has FDs S à SNLRWH and R à W Second FD causes violation of 3NF; W values repeatedly associated with R values. Easiest way to fix this is to create a relation RW to store these associations, and to remove W from the main schema: i.e., we decompose SNLRWH into SNLRH and RW v The information to be stored consists of SNLRWH tuples. If we just store the projections of these tuples onto SNLRH and RW, are there any potential problems that we should be aware of? Comp 521 Files and Databases Fall 2012 19

Problems with Decompositions v v There are three potential problems to consider: Problem 1) Some queries become more expensive. e.g., How much did Joe earn? (salary = W*H) Problem 2) Given instances of the decomposed relations, we may not be able to reconstruct the corresponding original relation! Fortunately, not in the SNLRWH example. Problem 3) Checking some dependencies may require joining the instances of the decomposed relations. Fortunately, not in the SNLRWH example. Tradeoff: Must consider these issues vs. redundancy. Comp 521 Files and Databases Fall 2012 20

Lossless Join Decompositions v 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: π X π Y (r) (r) = r v It is always true that r π X (r) (r) π Y In general, the other direction does not hold! If equal, the decomposition is lossless-join. v Definition extended to decomposition into 3 or more relations in a straightforward way. v It is essential that all decompositions used to eliminate redundancy be lossless! (Avoids Problem 2) Comp 521 Files and Databases Fall 2012 21

More on Lossless Join v The decomposition of R into X and Y is lossless-join wrt F if and only if the closure of F contains: X Y à X, or X Y à Y (in other words the attributes common to X and Y must contain a key for either X or Y) v In particular, the decomposition of R into UV and R - V is lossless-join if U à V holds over R. ABC AB, BC A B C 1 2 3 4 5 6 7 2 8 A B C 1 2 3 4 5 6 7 2 8 1 2 8 7 2 3 A B 1 2 4 5 7 2 B C 2 3 5 6 2 8 Not lossless Comp 521 Files and Databases Fall 2012 22

More on Lossless Join v The decomposition of R into X and Y is lossless-join wrt F if and only if the closure of F contains: X Y à X, or X Y à Y (in other words the attributes common to X and Y must contain a key for either X or Y) v In particular, the decomposition of R into UV and R - V is lossless-join if U à V holds over R. ABC AB, AC A B C 1 2 3 4 5 6 7 2 8 A B C 1 2 3 4 5 6 7 2 8 A B 1 2 4 5 7 2 Lossless Comp 521 Files and Databases Fall 2012 23

Dependency Preserving Decomposition Contracts(Cid,Sid,Jid,Did,Pid,Qty,Value) v Consider CSJDPQV, C is key, JP à C and SD à P. BCNF decomposition: CSJDQV and SDP Problem: Checking JP à C requires a join! v Dependency preserving decomposition (Intuitive): If R is decomposed into X, Y and Z, and we enforce the FDs that hold on X, on Y and on Z, then all FDs that were given to hold on R must also hold. (Avoids Problem 3) v Projection of set of FDs F: If R is decomposed into X,... projection of F onto X (denoted F X ) is the set of FDs U à V in F + (closure of F ) such that U, V are in X. Comp 521 Files and Databases Fall 2012 24

Dependency Preserving Decomposition v Decomposition of R into X Y is dependency preserving if (F X union F Y ) + = F + i.e., if we consider only dependencies in the closure F + that can be checked in X without considering Y, and in Y without considering X, these imply all dependencies in F +. v MUST consider F +, (not just F), in this definition: ABC, A à B, B à C, C à A, decomposed into AB and BC. Is this dependency preserving? Is C à A preserved????? v Dependency preserving does not imply lossless join: ABC, A à B, decomposed into AB and BC. v And vice-versa! Comp 521 Files and Databases Fall 2012 25

Decomposition into BCNF v Consider relation R with FDs F. If X à Y violates BCNF, decompose R into R - Y and XY. Repeated applications of this rule gives relations in BCNF; lossless join decomposition, and is guaranteed to terminate. v Example: CSJDPQV, SD à P, J à S (new), (ignoring JP à C for now) To deal with SD à P, decompose into SDP, CSJDQV. To deal with J à S, decompose CSJDQV into JS and CJDQV v The order in which we deal with violations could lead to a different set of relations! Comp 521 Files and Databases Fall 2012 26

BCNF and Dependency Preservation v In general, there may not be a dependency preserving decomposition into BCNF. e.g., CSZ, CS à Z, Z à C Can t decompose while preserving 1st FD; not in BCNF. v Similarly, decomposition of CSJDQV into SDP, JS and CJDQV is not dependency preserving (w.r.t. the FDs: JP à C, SD à P and J à S). However, it is a lossless join decomposition. In this case, adding CJP to the collection of relations gives us a dependency preserving decomposition. JPC tuples stored only for checking FD! (Adds Redundancy!) Comp 521 Files and Databases Fall 2012 27

Decomposition into 3NF v Obviously, the algorithm for lossless join decomp into BCNF can be used to obtain a lossless join decomp into 3NF (typically, it can stop earlier). v To ensure dependency preservation, one idea: If X à Y is not preserved, add relation XY. Problem is that XY may violate 3NF! e.g., consider the addition of CJP to preserve JP à C. What if we also have J à C? v Refinement: Instead of the given set of FDs F, use a minimal cover for F. Comp 521 Files and Databases Fall 2012 28

Minimal Cover for a Set of FDs v Properties of a 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. v Intuitively, every FD in G is needed, and is as small as possible in order to get the same closure as F. v e.g., A à B, ABCD à E, EF à GH, ACDF à EG has the following minimal cover: A à B, ACD à E, EF à G and EF à H v M.C. Lossless-Join, Dep. Pres. Decomp!!! (in book) Comp 521 Files and Databases Fall 2012 29

Refining an ER Diagram v 1st diagram translated: Workers(S,N,L,D,S) Departments(D,M,B) Lots associated with workers. v Suppose all workers in a dept are assigned the same lot: D à L v Redundancy; fixed by: Workers2(S,N,D,S) Dept_Lots(D,L) v Can fine-tune this: Workers2(S,N,D,S) Departments(D,M,B,L) ssn Before: ssn name Employees After: name Employees Comp 521 Files and Databases Fall 2012 30 lot since Works_In since Works_In did did dname budget Departments dname budget Departments lot

Summary of Schema Refinement v If a relation is in BCNF, it is free of redundancies that can be detected using FDs. Thus, trying to ensure that all relations are in BCNF is a good heuristic. v If a relation is not in BCNF, we can try to decompose it into a collection of BCNF relations. Must consider whether all FDs are preserved. If a losslessjoin, dependency preserving decomposition into BCNF is not possible (or unsuitable, given typical queries), should consider decomposition into 3NF. Decompositions should be carried out and/or re-examined while keeping performance requirements in mind. Comp 521 Files and Databases Fall 2012 31