Design Theory for Relational Databases

Similar documents
Chapter 3 Design Theory for Relational Databases

CS54100: Database Systems

Design Theory for Relational Databases

Chapter 3 Design Theory for Relational Databases

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

Relational Database Design

Design theory for relational databases

Database Design and Implementation

CSC 261/461 Database Systems Lecture 11

CS 464/564 Introduction to Database Management System Instructor: Abdullah Mueen

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

SCHEMA NORMALIZATION. CS 564- Fall 2015

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

CSC 261/461 Database Systems Lecture 13. Spring 2018

Design Theory for Relational Databases. Spring 2011 Instructor: Hassan Khosravi

INF1383 -Bancos de Dados

Lossless Joins, Third Normal Form

Schema Refinement and Normal Forms

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

Schema Refinement. Feb 4, 2010

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

CS54100: Database Systems

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

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

Relational Design Theory

CSC 261/461 Database Systems Lecture 12. Spring 2018

Databases 2012 Normalization

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

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

Schema Refinement and Normal Forms

Relational Database Design Theory Part II. Announcements (October 12) Review. CPS 116 Introduction to Database Systems

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

Practice and Applications of Data Management CMPSCI 345. Lecture 16: Schema Design and Normalization

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

Consider a relation R with attributes ABCDEF GH and functional dependencies S:

CSE 344 AUGUST 3 RD NORMALIZATION

Consider a relation R with attributes ABCDEF GH and functional dependencies S:

Lectures 6. Lecture 6: Design Theory

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. Example: Constraints on Entity Set. Functional Dependencies (FDs) Refining an ER Diagram

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

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

Schedule. Today: Jan. 17 (TH) Jan. 24 (TH) Jan. 29 (T) Jan. 22 (T) Read Sections Assignment 2 due. Read Sections Assignment 3 due.

Schema Refinement & Normalization Theory

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

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

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

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

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

DECOMPOSITION & SCHEMA NORMALIZATION

Information Systems (Informationssysteme)

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

Introduction to Data Management CSE 344

Schema Refinement and Normal Forms

CS322: Database Systems Normalization

CSE 303: Database. Outline. Lecture 10. First Normal Form (1NF) First Normal Form (1NF) 10/1/2016. Chapter 3: Design Theory of Relational Database

Schema Refinement and Normalization

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

CSE 544 Principles of Database Management Systems

Introduction to Management CSE 344

Functional Dependency and Algorithmic Decomposition

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

CSE 344 MAY 16 TH NORMALIZATION

Schema Refinement and Normal Forms Chapter 19

Schema Refinement and Normal Forms. Why schema refinement?

Problem about anomalies

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

Functional Dependencies and Normalization

Functional Dependency Theory II. Winter Lecture 21

CSE 132B Database Systems Applications

Background: Functional Dependencies. æ We are always talking about a relation R, with a æxed schema èset of attributesè and a

Chapter 8: Relational Database Design

Databases Lecture 8. Timothy G. Griffin. Computer Laboratory University of Cambridge, UK. Databases, Lent 2009

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

Chapter 11, Relational Database Design Algorithms and Further Dependencies

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

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

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

Constraints: Functional Dependencies

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

Relational-Database Design

CSE 344 AUGUST 6 TH LOSS AND VIEWS

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

Functional Dependencies & Normalization. Dr. Bassam Hammo

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

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

HKBU: Tutorial 4

Relational Database Design

Normal Forms Lossless Join.

Constraints: Functional Dependencies

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

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

Relational Design Theory I. Functional Dependencies: why? Redundancy and Anomalies I. Functional Dependencies

Functional Dependencies and Normalization

Database Design and Normalization

Functional Dependencies

Normaliza)on and Func)onal Dependencies

FUNCTIONAL DEPENDENCY THEORY II. CS121: Relational Databases Fall 2018 Lecture 20

Transcription:

Design Theory for Relational Databases FUNCTIONAL DEPENDENCIES DECOMPOSITIONS NORMAL FORMS 1

Functional Dependencies X ->Y is an assertion about a relation R that whenever two tuples of R agree on all the attributes of X, then they must also agree on all attributes in set Y. Say X ->Y holds in R. Convention:, X, Y, Z represent sets of attributes; A, B, C, represent single attributes. Convention: no set formers in sets of attributes, just ABC, rather than {A,B,C}. 2

Splitting Right Sides of FD s X->A 1 A 2 A n holds for R exactly when each of X->A 1, X->A 2,, X->A n hold for R. Example: A->BC is equivalent to A->B and A->C. There is no splitting rule for left sides. We ll generally express FD s with singleton right sides. 3

Example: FD s Drinkers(name, addr, beersliked, manf, favbeer) Reasonable FD s to assert: 1. name -> addr, favbeer Note this FD is the same as name -> addr and name -> favbeer. 2. beersliked -> manf 4

Example: Possible Data name addr beersliked manf favbeer Janeway Voyager Bud A.B. WickedAle Janeway Voyager WickedAle Pete s WickedAle Spock Enterprise Bud A.B. Bud Because name -> addr Because name -> favbeer Because beersliked -> manf 5

DB Administrator Task Assume we have a table/view (e.g., with..., postal code, city, province, country, date, year, month, day) It is denormalized (Data Warehouse) e.g., a materialized view (joining: sales, location and date/time table) Describe what are the reasonable FDs to assert in that table/view 6

Keys of Relations Set of attributes K is a superkey for relation R if K functionally determines all attributes of R. K is a key for R if K is a superkey, but no proper subset of K is a superkey. 7

Example: Superkey Drinkers(name, addr, beersliked, manf, favbeer) Is {name, beersliked} a superkey? 8

Example: Superkey Drinkers(name, addr, beersliked, manf, favbeer) {name, beersliked} is a superkey because together these attributes determine all the other attributes. name, beersliked -> addr, favbeer, beersliked, manf, favbeer 9

Example: Key Is {name, beersliked} a key? 10

Example: Key {name, beersliked} is a key because neither {name} nor {beersliked} is a superkey. name doesn t -> manf; beersliked doesn t -> addr. There are no other keys, but lots of superkeys. Any superset that contains {name, beersliked}. 11

Where Do Keys Come From? 1. Just assert a key K. The only FD s are K -> A for all attributes A. 2. Assert FD s and deduce the keys by systematic exploration. 12

More FD s From Physics Example: no two courses can meet in the same room at the same time tells us: hour, room -> course. 13

Inferring FD s We are given FD s X 1 -> A 1, X 2 -> A 2,, X n -> A n, and we want to know whether an FD Y -> B must hold in any relation that satisfies the given FD s. Example: If A -> B and B -> C hold, surely A -> C holds, even if we don t say so. Important for design of good relation schemas. 14

Inference Test To test if Y -> B, start by assuming two tuples agree in all attributes of Y. Y 0000000... 0 00000??...? 15

Inference Test (2) Use the given FD s to infer that these tuples must also agree in certain other attributes. If B is one of these attributes, then Y -> B is true. Otherwise, the two tuples, with any forced equalities, form a two-tuple relation that proves Y -> B does not follow from the given FD s. 16

Example/Task Assume ABC -> D, D -> F, F -> GH, I -> J. Is it true ABC -> G? Is it true ABC -> GH? Is it true ABC -> GHI? Use inference test to provide justification to your answer. 17

Closure Test An easier way to test is to compute the closure of Y, denoted Y +. Closure Y + is the set of attributes that Y functionally determines. Basis: Y + = Y. Induction: Look for an FD s left side X that is a subset of the current Y +. If the FD is X -> A, add A to Y +. 18

X A Y + new Y + 19

Example Closure Test Assume ABCD -> F, ABC -> D, F -> GH, I -> JGH. What is the closure of ABC, ABC +? 20

Example Closure Test Assume ABCD -> F, ABC -> D, F -> GH, I -> JGH. What is the closure of ABC, ABC +? 1. Basis ABC + = ABC 21

Example Closure Test Assume ABCD -> F, ABC -> D, F -> GH, I -> JGH. What is the closure of ABC, ABC +? 1. Basis ABC + = ABC 2. ABC + = ABC + union D, since ABC is a subset of ABC + and ABC -> D 22

Example Closure Test Assume ABCD -> F, ABC -> D, F -> GH, I -> JGH. What is the closure of ABC, ABC +? 1. Basis ABC + = ABC 2. ABC + = ABC + union D, since ABC is a subset of ABC + and ABC -> D 3. ABC + = ABC + union F, since ABCD is a subset of ABC + and ABCD -> F 23

Example Closure Test Assume ABCD -> F, ABC -> D, F -> GH, I -> JGH. What is the closure of ABC, ABC +? 1. Basis ABC + = ABC 2. ABC + = ABC + union D, since ABC is a subset of ABC + and ABC -> D 3. ABC + = ABC + union F, since ABCD is a subset of ABC + and ABCD -> F 4. ABC + = ABC + plus GH, since F is a subset of ABC + and F -> GH Therefore ABC + = ABCDFGH. Hence, for instance, ABC -> H is true but ABC -> I is not 24

Task Assume AB -> CH, C -> D, HD -> A, ACH -> EFG, I -> A. What is the closure of CH, CH +? Is it true that CH -> G and CH -> B?. 25

Finding All Implied FD s Motivation: normalization, the process where we break a relation schema into two or more schemas. Example: ABCD with FD s AB ->C, C ->D, and D ->A. Assume we decompose into ABC, AD. What FD s hold in ABC? AB ->C holds but how about C ->A? Perform Inference Test OR Closure Test 26

Finding All Implied FD s Motivation: normalization, the process where we break a relation schema into two or more schemas. Example: ABCD with FD s AB ->C, C ->D, and D ->A. Decompose into ABC, AD. What FD s hold in ABC? Not only AB ->C, but also C ->A? C+ = CDA Yes! 27

Basic Idea for decomposing table 1. Start with given FD s and find all FD s that follow from the given FD s. 2. Restrict to those FD s that involve only attributes of the projected schema. 28

Simple, Exponential Algorithm 1. For each set of attributes X, compute X +. 2. Finally, use only FD s involving projected attributes. 29

A Few Tricks No need to compute the closure of the set of all attributes. If we find X + = all attributes, so is the closure of any superset of X. 30

Example: Projecting FD s ABC with FD s A ->B and B ->C. Project onto AC. A + =ABC ; yields A ->B, A ->C. We do not need to compute AB + or AC + or ABC +. B + =BC ; yields B ->C. C + =C ; yields nothing. BC + =BC ; yields nothing. 31

Example -- Continued Resulting FD s: A ->B, A ->C, and B ->C. Projection onto AC : A ->C. Only FD that involves a subset of {A,C }. 32

A Geometric View of FD s Imagine the set of all instances of a particular relation. That is, all finite sets of tuples that have the proper number of components. Each instance is a point in this space. 33

Example: R(A,B) {(1,2), (3,4)} {} {(5,1)} {(1,2), (3,4), (1,3)} 34

An FD is a Subset of Instances For each FD X -> A there is a subset of all instances that satisfy the FD. We can represent an FD by a region in the space. Trivial FD = an FD that is represented by the entire space. Example: A -> A. 35

Example: A -> B for R(A,B) {(1,2), (3,4)} {} A -> B {(5,1)} {(1,2), (3,4), (1,3)} 36

Representing Sets of FD s If each FD is a set of relation instances, then a collection of FD s corresponds to the intersection of those sets. Intersection = all instances that satisfy all of the FD s. 37

Example Instances satisfying A->B, B->C, and CD->A A->B B->C CD->A 38

Implication of FD s If an FD Y -> B follows from FD s X 1 -> A 1,,X n -> A n, then the region in the space of instances for Y -> B must include the intersection of the regions for the FD s X i -> A i. That is, every instance satisfying all the FD s X i -> A i surely satisfies Y -> B. 39

Example A->B A->C B->C 40

Relational Schema Design 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 : valid fact is lost when a tuple is deleted. 41

Example of Bad Design Drinkers(name, addr, beersliked, manf, favbeer) name addr beersliked manf favbeer Janeway Voyager Bud A.B. WickedAle Janeway??? WickedAle Pete s??? Spock Enterprise Bud??? Bud Data is redundant, because each of the??? s can be figured out by using the FD s name -> addr favbeer and beersliked -> manf. 42

This Bad Design Also Exhibits Anomalies name addr beersliked manf favbeer Janeway Voyager Bud A.B. WickedAle Janeway Voyager WickedAle Pete s WickedAle Spock Enterprise Bud A.B. Bud Update anomaly: if Janeway is transferred to Intrepid, will we remember to change each of her tuples? Deletion anomaly: If nobody likes Bud, we lose track of the fact that Anheuser-Busch manufactures Bud. 43

Boyce-Codd Normal Form We say a relation R is in BCNF if whenever X ->Y is a nontrivial FD that holds in R, X is a superkey. nontrivial means Y is not contained in X. Remember, a superkey is any superset of a key 44

Example Drinkers(name, addr, beersliked, manf, favbeer) FD s: name->addr, favbeer, beersliked->manf In each FD, the left side is not a superkey. Any one of these FD s shows Drinkers is not in BCNF 45

Another Example Beers(name, manf, manfaddr) FD s: name->manf, manf->manfaddr Only key is {name}. name->manf does not violate BCNF because name is a superkey, but manf- >manfaddr does because manf is not a superkey. 46

Decomposition into BCNF Given: relation R with FD s F. Look among the given FD s for a BCNF violation: X ->Y. Compute X + 47

Decompose R Using X -> Y Replace R by relations with schemas: 1. R 1 = X +. 2. R 2 = R (X + X ) = R X + + X. Project given FD s F onto the two new relations. 48

Decomposition Picture R 1 R-X + X X + -X R 2 R 49

Example: BCNF Decomposition Drinkers(name, addr, beersliked, manf, favbeer) F = name->addr, name -> favbeer, beersliked->manf Pick BCNF violation name->addr. Closure of the left side: {name} + = {name, addr, favbeer}. Decomposed relations: 1. Drinkers1(name, addr, favbeer) 2. Drinkers2(name, beersliked, manf) 50

Example -- Continued We are not done; we need to check Drinkers1 and Drinkers2 for BCNF. Projecting FD s is easy here. For Drinkers1(name, addr, favbeer), relevant FD s are name->addr and name->favbeer. Thus, Drinkers1 is in BCNF. 51

Example -- Continued For Drinkers2(name, beersliked, manf), one of the FDs is beersliked- >manf but beersliked is not a superkey. Violation of BCNF. beersliked + = {beersliked, manf}, so we decompose Drinkers2 into: 1. Drinkers3(beersLiked, manf) 2. Drinkers4(name, beersliked) 52

Example -- Concluded The resulting decomposition of Drinkers : 1. Drinkers1(name, addr, favbeer) 2. Drinkers3(beersLiked, manf) 3. Drinkers4(name, beersliked) Notice: Drinkers1 tells us about drinkers, Drinkers3 tells us about beers, and Drinkers4 tells us the relationship between drinkers and the beers they like. 53

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, There are two keys, {A,B} and {A,C}. C = zip code. C ->B is a BCNF violation, so we must decompose into AC, BC. 54

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. 55

An Unenforceable FD street zip 545 Tech Sq. 02138 545 Tech Sq. 02139 city zip Cambridge 02138 Cambridge 02139 Join tuples with equal zip codes. street city zip 545 Tech Sq. Cambridge 02138 545 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. 56

3NF Let Us Avoid This Problem 3 rd 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. 57

Example: 3NF 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. 58

What 3NF and BCNF Give You There are two important properties of a decomposition: 1. 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 given FD s are satisfied. 59

3NF and BCNF -- Continued We can get (1) with a BCNF decomposition. 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. 60

3NF Synthesis Algorithm We can always construct a decomposition into 3NF relations with a lossless join and dependency preservation. Need minimal basis for the FD s: 1. Right sides are single attributes. 2. No FD can be removed. 3. No attribute can be removed from a left side. 61

Constructing a Minimal Basis 1. Split right sides. 2. Repeatedly try to remove an FD and see if the remaining FD s are equivalent to the original. 3. Repeatedly try to remove an attribute from a left side and see if the resulting FD s are equivalent to the original. 62

3NF Synthesis (2) One relation for each FD in the minimal basis. Schema is the union of the left and right sides. If no key is contained in an FD, then add one relation whose schema is some key. 63

Example: 3NF Synthesis Relation R = ABCD. FD s A->BC is equivalent to: FD s A->B and A->C. Decomposition: AB and AC from the FD s, plus AD for a key. 64

Why It Works Preserves dependencies: each FD from a minimal basis is contained in a relation, thus preserved. Lossless Join: yes 3NF: yes. 65

Actions Review slides! Read Chapters 3.1. 3.5 (Design Theory for Relational Databases). 66