Lossless Decomposition (2) Prof. Sin-Min Lee Department of Computer Science San Jose State University.

Slides:



Advertisements
Similar presentations
Schema Refinement: Normal Forms
Advertisements

Schema Refinement: Canonical/minimal Covers
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 16 Relational Database Design Algorithms and Further Dependencies.
Lecture 21 CS 157 B Revision of Midterm3 Prof. Sin-Min Lee.
Chapter 3 Notes. 3.1 Functional Dependencies A functional dependency is a statement that – two tuples of a relation that agree on some particular set.
1 Chapter 6 Relational Normalization Theory. 2 Limitations of E-R Designs Provides a set of guidelines, does not result in a unique database schema Does.
1 Relational Normalization Theory Chapter 8. 2 Limitations of E-R Designs Provides a set of guidelines, does not result in a unique database schema Does.
Relational Normalization Theory. Limitations of E-R Designs Provides a set of guidelines, does not result in a unique database schema Does not provide.
Lossless Decomposition (2) Prof. Sin-Min Lee Department of Computer Science San Jose State University.
1 Relational Normalization Theory. 2 Limitations of E-R Designs Provides a set of guidelines, does not result in a unique database schema Does not provide.
Relational Normalization Theory
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 227 Database Systems I Design Theory for Relational Databases.
CS Algorithm : Decomposition into 3NF  Obviously, the algorithm for lossless join decomp into BCNF can be used to obtain a lossless join decomp.
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.
The principal problem that we encounter is redundancy, where a fact is repeated in more than one tuple. Most common cause: attempts to group into one relation.
1 CMSC424, Spring 2005 CMSC424: Database Design Lecture 9.
Winter 2002Arthur Keller – CS 1804–1 Schedule Today: Jan. 15 (T) u Normal Forms, Multivalued Dependencies. u Read Sections Assignment 1 due. Jan.
1 Functional Dependency and Normalization Informal design guidelines for relation schemas. Functional dependencies. Normal forms. Normalization.
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.
Department of Computer Science and Engineering, HKUST Slide 1 7. Relational Database Design.
©Silberschatz, Korth and Sudarshan7.1Database System Concepts Chapter 7: Relational Database Design First Normal Form Pitfalls in Relational Database Design.
Chapter 10 Functional Dependencies and Normalization for Relational Databases.
CS 405G: Introduction to Database Systems 16. Functional Dependency.
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.
Normalization Goal = BCNF = Boyce-Codd Normal Form = all FD’s follow from the fact “key  everything.” Formally, R is in BCNF if for every nontrivial FD.
Copyright © Curt Hill Schema Refinement III 4 th NF and 5 th NF.
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.
Chapter 8: Relational Database Design First Normal Form First Normal Form Functional Dependencies Functional Dependencies Decomposition Decomposition Boyce-Codd.
Schema Refinement and Normalization. Functional Dependencies (Review) A functional dependency X  Y holds over relation schema R if, for every allowable.
Lecture 6 Normalization: Advanced forms. Objectives How inference rules can identify a set of all functional dependencies for a relation. How Inference.
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.
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.
BCNF & Lossless Decomposition Prof. Sin-Min Lee Department of Computer Science.
Functional Dependencies and Normalization for Relational Databases.
Computing & Information Sciences Kansas State University Tuesday, 27 Feb 2007CIS 560: Database System Concepts Lecture 18 of 42 Tuesday, 27 February 2007.
Chapter 6 Database Design with the Relational Normalization Theory.
1 Functional Dependencies and Normalization Chapter 15.
IST 210 Normalization 2 Todd Bacastow IST 210. Normalization Methods Inspection Closure Functional dependencies are key.
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:
Copyright, Harris Corporation & Ophir Frieder, The Process of Normalization.
1 CSE 480: Database Systems Lecture 18: Normal Forms and Normalization.
Design Process - Where are we?
Database Management Systems, 3ed, R. Ramakrishnan and J. Gehrke1 Schema Refinement and Normal Forms Chapter 19.
Normalization.
3 Spring Chapter Normalization of Database Tables.
CS 405G: Introduction to Database Systems Instructor: Jinze Liu Fall 2009.
CS 338Database Design and Normal Forms9-1 Database Design and Normal Forms Lecture Topics Measuring the quality of a schema Schema design with normalization.
1 Schema Refinement and Normal Forms Chapter The Evils of Redundancy  Redundancy is at the root of several problems associated with relational.
Ch 7: Normalization-Part 1
Rensselaer Polytechnic Institute CSCI-4380 – Database Systems David Goldschmidt, Ph.D.
Huffman code and Lossless Decomposition Prof. Sin-Min Lee Department of Computer Science.
Database Management Systems, 3ed, R. Ramakrishnan and J. Gehrke1 Schema Refinement and Normal Forms Chapter 19.
© 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 8 Relational Database Design. 2 Relational Database Design: Goals n Reduce data redundancy (undesirable replication of data values) n Minimize.
Normalization and FUNctional Dependencies. Redundancy: root of several problems with relational schemas: –redundant storage, insert/delete/update anomalies.
Normal Forms Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems June 18, 2016 Some slide content courtesy of Susan Davidson.
Chapter 14 Functional Dependencies and Normalization Informal Design Guidelines for Relational Databases –Semantics of the Relation Attributes –Redundant.
CSC 411/511: DBMS Design Dr. Nan Wang 1 Schema Refinement and Normal Forms Chapter 19.
11/16/2016CENG352 Database Management Systems1 Relational Normalization Theory.
Schema Refinement and Normal Forms
CS 480: Database Systems Lecture 22 March 6, 2013.
Module 5: Overview of Normalization
Relational Normalization Theory
Chapter 7a: Overview of Database Design -- Normalization
Presentation transcript:

Lossless Decomposition (2) Prof. Sin-Min Lee Department of Computer Science San Jose State University

Normal form and normalization A normal form is a property of a relational database. When a relation is non-normalized (that is, does not satisfy a normal form), then it presents redundancies and produces undesirable behavior during update operations. This principle can be used to carry out quality analysis and constitutes a useful tool for database design. Normalization is a procedure that allows the non-normalized schemas to be transformed into new schemas for which the satisfaction of a normal form is guaranteed.

Example of a relation with anomalies Employee Salary Project Budget Function Brown 20 Mars 2 technician Green 35 Jupiter 15 designer Green 35 Venus 15 designer Hoskins 55 Venus 15 manager Hoskins 55 Jupiter 15 consultant Hoskins 55 Mars 2 consultant Moore 48 Mars 2 manager Moore 48 Venus 15 designer Kemp 48 Venus 15 designer Kemp 48 Jupiter 15 manager The key is made up of the attributes Employee and Project

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)

Anomalies in the example relation The value of the salary of each employee is repeated in all the tuples relating to it: therefore there is a redundancy. If the salary of an employee changes, we have to modify the value in all the corresponding tuples. This problem is known as the update anomaly. If an employee stops working on all the projects but does not leave the company, all the corresponding tuples are deleted and so, even the basic information, name and salary is lost. This problem is known as the deletion anomaly. If we have information on a new employee, we cannot insert it until the employee is assigned to a project. This is known as the insertion anomaly.

Why these undesirable phenomena? Intuitive explanation: we have used a single relation to represent items of information of different types. In particular, the following independent real-world concepts are represented in the relation: – employees with their salaries, – projects with their budgets, – participation of the employees in the projects with their functions. To systematically study the principles introduced informally, it is necessary to use a specific notion: the functional dependency.

Functional dependencies Given a relation r on a schema R(X) and two non-empty subsets Y and Z of the attributes X, we say that there is a functional dependency on r between Y and Z, if, for each pair of tuples t 1 and t 2 of r having the same values on the attributes Y, t 1 and t 2 also have the same values of the Z attributes. A functional dependency between the attributes Y and Z is indicated by the notation Y -> Z

Functional dependencies in the example schema Employee -> Salary the salary of each employee is unique and thus each time a certain employee appears in a tuple, the value of his or her salary always remains the same. Project -> Budget the budget of each project is unique and thus each time a certain project appears in a tuple, the value of its budget always remains the same.

Non-trivial functional dependencies We then say that a functional dependency Y-> Z is non-trivial if no attribute in Z appears among the attributes of Y. – Employee -> Salary is a non-trivial functional dependency – Employee Project -> Project is a trivial functional dependency

Anomalies and functional dependencies In our example, the two properties causing anomalies correspond exactly to attributes involved in functional dependencies: – the property ‘ the salary of each employee is unique and depends only on the employee’ corresponds to the functional dependency Employee -> Salary; – the property ‘ the budget of each project is unique and depends only on the project’ corresponds to the functional dependency Project -> Budget. Moreover, the following property can be formalized by means of a functional dependency: – the property ‘ in each project, each of the employees involved can carry out only one function’ corresponds to the functional dependency Employee Project -> Function.

Dependencies generating anomalies The first two dependencies generate undesirable redundancies and anomalies. The third dependency however never generates redundancies because, having Employee and Project as a key, the relation cannot contain two tuples with the same values of these attributes. The difference is that Employee Project is a key of the relation.

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

(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 (AcctNum, ClientId, OfficeId) –The FD AcctNum  OfficeId does not satisfy BCNF requirements since keys are (ClientId, OfficeId) and (AcctNum, ClientId); not AcctNum.

Redundancy Suppose R has a FD A  B, and A is not a superkey. 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

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

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)

3NF (Non) 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

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

Decomposition Schema R = (R, F) –R is set a of attributes –F is a set of functional dependencies over R Each key is described by a FD 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

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 = { }

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 ……

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

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

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

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

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 )

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

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.

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

Example Schema: (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

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

Simple Example HasAccountHasAccount : (ClientId, OfficeId, AcctNum) (ClientId, AcctNum) BCNF (only trivial FDs) Decompose using AcctNum  OfficeId : (OfficeId, AcctNum) BCNF: AcctNum is key FD: AcctNum  OfficeId ClientId,OfficeId  AcctNum AcctNum  OfficeId

A Larger Example Given: R = (R; F) where R = ABCDEGHK and F = {ABH  C, A  DE, BGH  K, K  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, F 1 ={A  DE }) R 2 = (ABCGHK; F 1 ={ABH  C, BGH  K, K  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 K  D and BH  E are not in F 1 or F 2. But both can be derived from F 1  F 2 (E.g., K  A and A  D implies K  D) Hence, decomposition is dependency preserving.

Example (con’t) Given: R 2 = (ABCGHK; {ABH  C, BGH  K, K  AH, BH  G}) step 1: Find a FD that violates BCNF. Not ABH  C or BGH  K, since BH is a key of R 2 K  AH violates BCNF since K is not a superkey (K + =AH) step 2: Split R 2 into: R 21 = (KAH, F 21 ={K  AH}) R 22 = (BCGK; F 22 ={}) Note 1: Both R 21 and R 22 are in BCNF. Note 2: The decomposition is lossless (since K is a key of R 21 ) Note 3: FDs ABH  C, BGH  K, BH  G are not in F 21 or F 22, and they can’t be derived from F 1  F 21  F 22. Hence the decomposition is not dependency-preserving

Properties of BCNF Decomposition Algorithm Let X  Y violate BCNF in R = (R,F) and R 1 = (R 1,F 1 ), R 2 = (R 2,F 2 ) is the resulting decomposition. Then: There are fewer violations of BCNF in R 1 and R 2 than there were in R –X  Y implies X is a key of R 1 –Hence X  Y  F 1 does not violate BCNF in R 1 and, since X  Y  F 2, does not violate BCNF in R 2 either –Suppose f is X ’  Y ’ and f  F doesn’t violate BCNF in R. If f  F 1 or F 2 it does not violate BCNF in R 1 or R 2 either since X ’ is a superkey of R and hence also of R 1 and R 2.

Properties of BCNF Decomposition Algorithm A BCNF decomposition is not necessarily dependency preserving But always lossless: since R 1  R 2 = X, X  Y, and R 1 = XY HasAccountBCNF+lossless+dependency preserving is sometimes unachievable (recall HasAccount)

Boyce–Codd Normal Form (BCNF) A relation r is in Boyce–Codd normal form if for every (non-trivial) functional dependency X -> Y defined on it, X contains a key K of r. That is, X is a superkey for r. Anomalies and redundancies, as discussed above, do not appear in databases with relations in Boyce–Codd normal form, because the independent pieces of information are separate, one per relation.

Decomposition into Boyce–Codd normal form Given a relation that does not satisfy Boyce–Codd normal form, we can often replace it with one or more normalized relations using a process called normalization. We can eliminate redundancies and anomalies for the example relation if we replace it with the three relations, obtained by projections on the sets of attributes corresponding to the three functional dependencies. The keys of the relations we obtain are the left hand side of a functional dependency: the satisfaction of the Boyce–Codd normal form is therefore guaranteed.