Relational Database Design

Similar documents
Chapter 8: Relational Database Design

CS322: Database Systems Normalization

Chapter 7: Relational Database Design

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

CSE 132B Database Systems Applications

Functional Dependency Theory II. Winter Lecture 21

Relational Database Design

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

Lossless Joins, Third Normal Form

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

SCHEMA NORMALIZATION. CS 564- Fall 2015

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

Shuigeng Zhou. April 6/13, 2016 School of Computer Science Fudan University

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

Relational Design: Characteristics of Well-designed DB

INF1383 -Bancos de Dados

Relational-Database Design

Lecture 6 Relational Database Design

CSC 261/461 Database Systems Lecture 11

CS54100: Database Systems

CSC 261/461 Database Systems Lecture 12. Spring 2018

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

Functional Dependencies and Normalization

Relational Design Theory II. Detecting Anomalies. Normal Forms. Normalization

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

Design theory for relational databases

DECOMPOSITION & SCHEMA NORMALIZATION

Databases 2012 Normalization

CSC 261/461 Database Systems Lecture 13. Spring 2018

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

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

Functional Dependencies

Database System Concepts, 5th Ed.! Silberschatz, Korth and Sudarshan See for conditions on re-use "

Database Design and Implementation

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

Chapter 3 Design Theory for Relational Databases

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

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

Relational Design Theory

Constraints: Functional Dependencies

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

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

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

CSIT5300: Advanced Database Systems

Chapter 11, Relational Database Design Algorithms and Further Dependencies

Design Theory for Relational Databases

Constraints: Functional Dependencies

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

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

Schema Refinement and Normal Forms. Why schema refinement?

CSE 344 AUGUST 6 TH LOSS AND VIEWS

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

Information Systems (Informationssysteme)

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

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

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

Normal Forms Lossless Join.

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

Schema Refinement and Normal Forms

Schema Refinement and Normal Forms. Chapter 19

Functional Dependency and Algorithmic Decomposition

Schema Refinement & Normalization Theory

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

Chapter 3 Design Theory for Relational Databases

Design Theory for Relational Databases

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

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

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

L14: Normalization. CS3200 Database design (sp18 s2) 3/1/2018

CSE 344 MAY 16 TH NORMALIZATION

Schema Refinement: Other Dependencies and Higher Normal Forms

Schema Refinement. Feb 4, 2010

L13: Normalization. CS3200 Database design (sp18 s2) 2/26/2018

Introduction to Data Management CSE 344

CSE 544 Principles of Database Management Systems

Functional Dependencies & Normalization. Dr. Bassam Hammo

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

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

Database Design: Normal Forms as Quality Criteria. Functional Dependencies Normal Forms Design and Normal forms

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

Lectures 6. Lecture 6: Design Theory

Functional Dependencies and Normalization

CSE 344 AUGUST 3 RD NORMALIZATION

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

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

Schema Refinement and Normal Forms Chapter 19

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

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

Functional Dependencies and Normalization. Instructor: Mohamed Eltabakh

Functional Dependencies

Database Design and Normalization

Introduction to Management CSE 344

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

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

Relational Database Design

Data Bases Data Mining Foundations of databases: from functional dependencies to normal forms

Normaliza)on and Func)onal Dependencies

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

Homework 2 (by Prashasthi Prabhakar) Due: Wednesday Sep 20, 11:59pm

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

Transcription:

CSL 451 Introduction to Database Systems Relational Database Design Department of Computer Science and Engineering Indian Institute of Technology Ropar Narayanan (CK) Chatapuram Krishnan!

Recap - Boyce-Codd Normal Form 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: α β is trivial (i.e., β α) α is a superkey for R 8.2

Testing for 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 +. 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, simplified test using only F is incorrect when testing a relation in a decomposition of R Consider R = (A, B, C, D, E), with F = { A B, BC D}! Decompose R into R 1 = (A,B) and R 2 = (A,C,D, E)! Neither of the dependencies in F contain only attributes from (A,C,D,E) so we might be mislead into thinking R 2 satisfies BCNF.! In fact, dependency AC D in F + shows R 2 is not in BCNF. 8.3

Testing Decomposition for BCNF To check if a relation R i in a decomposition of R is in BCNF, Either test R i for BCNF with respect to the restriction of F to R i (that is, all FDs in F + that contain only attributes from R i ) or 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.! 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 8.4

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; Note: each R i is in BCNF, and decomposition is lossless-join. 8.5

Example of BCNF Decomposition R = (A, B, C ) F = {A B B C} Key = {A} 8.6

Example of BCNF Decomposition class (course_id, title, dept_name, credits, sec_id, semester, year, building, room_number, capacity, time_slot_id) Functional dependencies: course_id title, dept_name, credits building, room_number capacity course_id, sec_id, semester, year building, room_number, time_slot_id A candidate key {course_id, sec_id, semester, year}. 8.7

8.19 Give a lossless-join decomposition into BCNF of schema r(a, B, C,D,E) with respect to the following set F of functional dependencies: A BC CD E B D E A 16/03/15! Department of Computer Science and Engineering! Indian Institute of Technology Ropar! 8!

8.29.e Consider the following set F of functional dependencies on the schema r(a, B, C,D,E, F) A BCD BC DE B D D A Give a BCNF decomposition of r. 16/03/15! Department of Computer Science and Engineering! Indian Institute of Technology Ropar! 9!

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

Third Normal Form 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 a superkey for R Each attribute A in β α is contained in a candidate key for R. (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). Third condition is a minimal relaxation of BCNF to ensure dependency preservation (will see why later). 8.11

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 /* Optionally, remove redundant relations */ repeat if any schema R j is contained in another schema R k then /* delete R j */ R j = R;; i=i-1; return (R 1, R 2,..., R i ) 8.12

3NF Decomposition: An Example Relation schema: cust_banker_branch = (customer_id, employee_id, branch_name, type ) The functional dependencies for this relation schema are: 1. customer_id, employee_id branch_name, type 2. employee_id branch_name 3. customer_id, branch_name employee_id We first compute a canonical cover branch_name is extraneous in the r.h.s. of the 1 st dependency No other attribute is extraneous, so we get F C = customer_id, employee_id type employee_id branch_name customer_id, branch_name employee_id 8.13

8.20 Give a lossless-join, dependency-preserving decomposition into 3NF of schema r(a, B, C,D,E) with respect to the following set F of functional dependencies: A BC CD E B D E A 16/03/15! Department of Computer Science and Engineering! Indian Institute of Technology Ropar! 14!

8.29.d Consider the following set F of functional dependencies on the schema r(a, B, C,D,E, F) A BCD BC DE B D D A Give a 3NF decomposition of r based on the canonical cover 16/03/15! Department of Computer Science and Engineering! Indian Institute of Technology Ropar! 15!

Consider the following set F of functional dependencies on the schema r(a, B, C,D,E, F) A B B C A C D E AD E Give a 3NF decomposition of r based on the canonical cover 16/03/15! Department of Computer Science and Engineering! Indian Institute of Technology Ropar! 16!

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

Design Goals Goal for a relational database design is: BCNF. Lossless join. Dependency preservation. If we cannot achieve this, we accept one of Lack of dependency preservation Redundancy due to use of 3NF Interestingly, SQL does not provide a direct way of specifying functional dependencies other than superkeys. Can specify FDs using assertions, but they are expensive to test, (and currently not supported by any of the widely used databases!) Even if we had a dependency preserving decomposition, using SQL we would not be able to efficiently test a functional dependency whose left hand side is not a key. 8.18

8.11.a In the BCNF decomposition algorithm, suppose you use a functional dependency α β to decompose a relation schema r(α, β, γ) into r1(α, β) and r2(α, γ). What primary and foreignkey constraint do you expect to hold on the decomposed relations? 16/03/15! Department of Computer Science and Engineering! Indian Institute of Technology Ropar! 19!

8.11.b In the BCNF decomposition algorithm, suppose you use a functional dependency α β to decompose a relation schema r(α, β, γ) into r1(α, β) and r2(α, γ). Give an example of an inconsistency that can arise due to an erroneous update, if the foreign-key constraint were not enforced on the decomposed relations. 16/03/15! Department of Computer Science and Engineering! Indian Institute of Technology Ropar! 20!

8.11.c When a relation is decomposed into 3NF, what primary and foreign key dependencies would you expect will hold on the decomposed schema? 16/03/15! Department of Computer Science and Engineering! Indian Institute of Technology Ropar! 21!

8.1 Suppose that we decompose the schema r(a, B, C,D,E) into r1(a, B, C) and r2(a, D, E). Show that this decomposition is a lossless decomposition if the following set F of functional dependencies holds: A BC CD E B D E A 16/03/15! Department of Computer Science and Engineering! Indian Institute of Technology Ropar! 22!

8.16 Let a prime attribute be one that appears in at least one candidate key. Let α and β be sets of attributes such that α β, holds, but β α does not hold. Let A be an attribute that is not in α and β, and for which β A holds. We say that A is transitively dependent on α. We can restate our definition of 3NF as follows: A relation schema R is in 3NF with respect to a set F of functional dependencies if there are no nonprime attributes A in R for which A is transitively dependent on a key for R. Show that this new definition is equivalent to textbook definition of 3NF. 16/03/15! Department of Computer Science and Engineering! Indian Institute of Technology Ropar! 23!

Multivalued Dependencies Suppose we record names of children, and phone numbers for instructors: inst_child(id, child_name) inst_phone(id, phone_number) If we were to combine these schemas to get inst_info(id, child_name, phone_number) Example data: (99999, David, 512-555-1234) (99999, David, 512-555-4321) (99999, William, 512-555-1234) (99999, William, 512-555-4321) This relation is in BCNF Why? 8.24

Example Let R be a relation schema with a set of attributes that are partitioned into 3 nonempty subsets. Y, Z, W We say that Y Z (Y multidetermines Z ) if and only if for all possible relations r (R ) then < y 1, z 1, w 1 > r and < y 1, z 2, w 2 > r < y 1, z 1, w 2 > r and < y 1, z 2, w 1 > r Note that since the behavior of Z and W are identical it follows that Y Z if Y W 8.25

Example (Cont.) In our example: ID child_name ID phone_number The above formal definition is supposed to formalize the notion that given a particular value of Y (ID) it has associated with it a set of values of Z (child_name) and a set of values of W (phone_number), and these two sets are in some sense independent of each other. Note: If Y Z then Y Z Indeed we have (in above notation) Z 1 = Z 2 The claim follows. 8.26

MVD (Cont.) Tabular representation of α β 8.27

Multivalued Dependencies (MVDs) Let R be a relation schema and let α R and β R. The multivalued dependency α β holds on R if in any legal relation r(r), for all pairs for tuples t 1 and t 2 in r such that t 1 [α] = t 2 [α], there exist tuples t 3 and t 4 in r such that: t 1 [α] = t 2 [α] = t 3 [α] = t 4 [α] t 3 [β] = t 1 [β] t 3 [R β] = t 2 [R β] t 4 [β] = t 2 [β] t 4 [R β] = t 1 [R β] 8.28

Use of Multivalued Dependencies We use multivalued dependencies in two ways: 1. To test relations to determine whether they are legal under a given set of functional and multivalued dependencies 2. To specify constraints on the set of legal relations. We shall thus concern ourselves only with relations that satisfy a given set of functional and multivalued dependencies. If a relation r fails to satisfy a given multivalued dependency, we can construct a relations rʹ that does satisfy the multivalued dependency by adding tuples to r. 8.29

Theory of MVDs From the definition of multivalued dependency, we can derive the following rule: If α β, then α β That is, every functional dependency is also a multivalued dependency The closure D + of D is the set of all functional and multivalued dependencies logically implied by D. We can compute D + from D, using the formal definitions of functional dependencies and multivalued dependencies. We can manage with such reasoning for very simple multivalued dependencies, which seem to be most common in practice For complex dependencies, it is better to reason about sets of dependencies using a system of inference rules (see Appendix C). 8.30

Fourth Normal Form A relation schema R is in 4NF with respect to a set D of functional and multivalued dependencies if for all multivalued dependencies in D + of the form α β, where α R and β R, at least one of the following hold: α β is trivial (i.e., β α or α β = R) α is a superkey for schema R If a relation is in 4NF it is in BCNF 8.31

Restriction of Multivalued Dependencies The restriction of D to R i is the set D i consisting of All functional dependencies in D + that include only attributes of R i All multivalued dependencies of the form α (β R i ) where α R i and α β is in D + 8.32

4NF Decomposition Algorithm result: = {R}; done := false; compute D + ; Let D i denote the restriction of D + to R i while (not done) if (there is a schema R i in result that is not in 4NF) then begin let α β be a nontrivial multivalued dependency that holds on R i such that α R i is not in D i, and α β=φ; result := (result - R i ) (R i - β) (α, β); end else done:= true; Note: each R i is in 4NF, and decomposition is lossless-join 8.33

Example R =(A, B, C, G, H, I) F ={ A B B HI CG H } R is not in 4NF since A B and A is not a superkey for R Decomposition a) R 1 = (A, B) (R 1 is in 4NF) b) R 2 = (A, C, G, H, I) (R 2 is not in 4NF, decompose into R 3 and R 4 ) c) R 3 = (C, G, H) (R 3 is in 4NF) d) R 4 = (A, C, G, I) (R 4 is not in 4NF, decompose into R 5 and R 6 ) A B and B HI è A HI, (MVD transitivity), and and hence A I (MVD restriction to R 4 ) e) R 5 = (A, I) (R 5 is in 4NF) f)r 6 = (A, C, G) (R 6 is in 4NF) 8.34