Database Design and Normalization



Similar documents
Chapter 7: Relational Database Design

COSC344 Database Theory and Applications. Lecture 9 Normalisation. COSC344 Lecture 9 1

Databases -Normalization III. (N Spadaccini 2010 and W Liu 2012) Databases - Normalization III 1 / 31

Relational Database Design

Design of Relational Database Schemas

CS143 Notes: Normalization Theory

Functional Dependencies and Finding a Minimal Cover

Database Design and Normal Forms

Chapter 15 Basics of Functional Dependencies and Normalization for Relational Databases

Relational Database Design Theory

Chapter 10. Functional Dependencies and Normalization for Relational Databases

Theory of Relational Database Design and Normalization

Relational Database Design: FD s & BCNF

Schema Refinement, Functional Dependencies, Normalization

Introduction to Databases, Fall 2005 IT University of Copenhagen. Lecture 5: Normalization II; Database design case studies. September 26, 2005

Schema Design and Normal Forms Sid Name Level Rating Wage Hours

Advanced Relational Database Design

Database Management System

Normalisation to 3NF. Database Systems Lecture 11 Natasha Alechina

Schema Refinement and Normalization

Lecture Notes on Database Normalization

Theory behind Normalization & DB Design. Satisfiability: Does an FD hold? Lecture 12

normalisation Goals: Suppose we have a db scheme: is it good? define precise notions of the qualities of a relational database scheme

Boyce-Codd Normal Form

Database Management Systems. Redundancy and Other Problems. Redundancy

Database Design and Normalization

Functional Dependency and Normalization for Relational Databases

Normalization in Database Design

Week 11: Normal Forms. Logical Database Design. Normal Forms and Normalization. Examples of Redundancy

Relational Normalization Theory (supplemental material)

Why Is This Important? Schema Refinement and Normal Forms. The Evils of Redundancy. Functional Dependencies (FDs) Example (Contd.)

Functional Dependencies and Normalization

Limitations of E-R Designs. Relational Normalization Theory. Redundancy and Other Problems. Redundancy. Anomalies. Example

Normalisation. Why normalise? To improve (simplify) database design in order to. Avoid update problems Avoid redundancy Simplify update operations

CS 377 Database Systems. Database Design Theory and Normalization. Li Xiong Department of Mathematics and Computer Science Emory University

Theory of Relational Database Design and Normalization

Chapter 10 Functional Dependencies and Normalization for Relational Databases

Relational Normalization: Contents. Relational Database Design: Rationale. Relational Database Design. Motivation

Chapter 8. Database Design II: Relational Normalization Theory

Chapter 7: Relational Database Design

Chapter 10. Functional Dependencies and Normalization for Relational Databases. Copyright 2007 Ramez Elmasri and Shamkant B.

Functional Dependencies

RELATIONAL DATABASE DESIGN

Normalization in OODB Design

Introduction to Database Systems. Normalization

Normalization of database model. Pazmany Peter Catholic University 2005 Zoltan Fodroczi

Objectives of Database Design Functional Dependencies 1st Normal Form Decomposition Boyce-Codd Normal Form 3rd Normal Form Multivalue Dependencies

Lecture 2 Normalization

Chapter 5: FUNCTIONAL DEPENDENCIES AND NORMALIZATION FOR RELATIONAL DATABASES

Introduction Decomposition Simple Synthesis Bernstein Synthesis and Beyond. 6. Normalization. Stéphane Bressan. January 28, 2015

An Algorithmic Approach to Database Normalization

Database Systems Concepts, Languages and Architectures

DATABASE NORMALIZATION

DATABASE SYSTEMS. Chapter 7 Normalisation

DATABASE DESIGN: NORMALIZATION NOTE & EXERCISES (Up to 3NF)

Introduction to Database Systems. Chapter 4 Normal Forms in the Relational Model. Chapter 4 Normal Forms

6.830 Lecture PS1 Due Next Time (Tuesday!) Lab 1 Out today start early! Relational Model Continued, and Schema Design and Normalization

Normalization. CIS 331: Introduction to Database Systems

How To Find Out What A Key Is In A Database Engine

Limitations of DB Design Processes

SQL DDL. DBS Database Systems Designing Relational Databases. Inclusion Constraints. Key Constraints

A Web-Based Environment for Learning Normalization of Relational Database Schemata

Jordan University of Science & Technology Computer Science Department CS 728: Advanced Database Systems Midterm Exam First 2009/2010

Determination of the normalization level of database schemas through equivalence classes of attributes

BCA. Database Management System

Quiz 3: Database Systems I Instructor: Hassan Khosravi Spring 2012 CMPT 354

Chapter 5: Logical Database Design and the Relational Model Part 2: Normalization. Introduction to Normalization. Normal Forms.

Normalisation in the Presence of Lists

Database Constraints and Design

Design Theory for Relational Databases: Functional Dependencies and Normalization

C# Cname Ccity.. P1# Date1 Qnt1 P2# Date2 P9# Date9 1 Codd London Martin Paris Deen London

Normalization. Normalization. Normalization. Data Redundancy

A. TRUE-FALSE: GROUP 2 PRACTICE EXAMPLES FOR THE REVIEW QUIZ:

MCQs~Databases~Relational Model and Normalization

Normalization. Functional Dependence. Normalization. Normalization. GIS Applications. Spring 2011

Introduction to normalization. Introduction to normalization

CSCI-GA Database Systems Lecture 7: Schema Refinement and Normalization

Module 5: Normalization of database tables

Chapter 6. Database Tables & Normalization. The Need for Normalization. Database Tables & Normalization

Teaching Database Modeling and Design: Areas of Confusion and Helpful Hints

Normalization of Database

DATABASE MANAGEMENT SYSTEMS. Question Bank:

Normal forms and normalization

Normalization for Relational DBs

Normalisation and Data Storage Devices

LiTH, Tekniska högskolan vid Linköpings universitet 1(7) IDA, Institutionen för datavetenskap Juha Takkinen

Normalisation 6 TABLE OF CONTENTS LEARNING OUTCOMES

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer

Announcements. SQL is hot! Facebook. Goal. Database Design Process. IT420: Database Management and Organization. Normalization (Chapter 3)

TYPICAL QUESTIONS & ANSWERS

Normalisation 1. Chapter 4.1 V4.0. Napier University

Unit 3.1. Normalisation 1 - V Normalisation 1. Dr Gordon Russell, Napier University

Fundamentals of Database System

Question 1. Relational Data Model [17 marks] Question 2. SQL and Relational Algebra [31 marks]

Part 6. Normalization

Introduction to Databases

Transcription:

Database Design and Normalization CPS352: Database Systems Simon Miner Gordon College Last Revised: 9/27/12

Agenda Check-in Functional Dependencies (continued) Design Project E-R Diagram Presentations Database Normalization Homework 3

Check-in

Functional Dependencies

Database Design Goal Decide whether a particular relation R is in good form. Middle ground between the universal relation and relations which suffer from lossy join 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 each relation is in good form the decomposition is a lossless-join decomposition Our theory is based on: functional dependencies database normal forms multivalued dependencies

Functional Dependency (FD) When the value of a certain set of attributes uniquely determines the value for another set of attributes Generalization of the notion of a key A way to find good relations A B (read: A determines B) Formal definition For some relation scheme R and attribute sets A (A R) and B (B R) A B if for any legal relation on R If there are two tuples t 1 and t 2 such that t 1 (A) = t 2 (A) It must be the case that t 2 (A) = t 2 (B)

Finding Functional Dependencies From keys of an entity Primary and candidate keys From relationships between entities One to one, one to many/many to one, and many to many relationships Implied functional dependencies

Implied Functional Dependencies Initial set of FDs logically implies other FDs If A B and B C, then B C Closure If F is the set of functional dependencies we develop from the logic of the underlying reality Then F+ (the transitive closure of F) is the set consisting of all the dependencies of F, plus all the dependencies they imply

Rules for Computing F+ We can find F +, the closure of F, by repeatedly applying Armstrong s Axioms: if, then (reflexivity) Trivial dependency if, then (augmentation) if, and, then (transitivity) Additional rules (inferred from Armstrong s Axioms) If and, then (union) If, then and (decomposition) If and, then (pseudotransitivity)

Applying the Axioms R = (A, B, C, G, H, I) F = { A B A C CG H CG I B H} some members of F + A H by transitivity from A B and B H AG I by augmenting A C with G, to get AG CG and then transitivity with CG I CG HI by augmenting CG I to infer CG CGI, and augmenting of CG H to infer CGI HI, and then transitivity or by the union rule

Algorithm to Compute F+ To compute the closure of a set of functional dependencies F: F + = F repeat for each functional dependency f in F + apply reflexivity and augmentation rules on f add the resulting functional dependencies to F + for each pair of functional dependencies f 1 and f 2 in F + if f 1 and f 2 can be combined using transitivity then add the resulting functional dependency to F + until F + does not change any further

Algorithm to Compute the Closure of Attribute Sets Given a set of attributes, define the closure of under F (denoted by + ) as the set of attributes that are functionally determined by under F Algorithm to compute +, the closure of under F result := ; while (changes to result) do for each in F do begin if result then result := result end

Example of Attribute Set R = (A, B, C, G, H, I) F = {A B A C CG H CG I B H} Closure (AG) + 1. result = AG 2. result = ABCG (A C and A B) 3. result = ABCGH (CG H and CG AGBC) 4. result = ABCGHI (CG I and CG AGBCH) Is AG a candidate key? 1. Is AG a super key? 1. Does AG R? == Is (AG) + R 2. Is any subset of AG a superkey? 1. Does A R? == Is (A) + R 2. Does G R? == Is (G) + R

Canonical Cover Sets of functional dependencies may have redundant dependencies that can be inferred from the others For example: A C is redundant in: {A B, B C, A C} Parts of a functional dependency may be redundant E.g.: on RHS: {A B, B C, A CD} can be simplified to {A B, B C, A D} E.g.: on LHS: {A B, B C, AC D} can be simplified to {A B, B C, A D} Intuitively, a canonical cover of F is a minimal set of functional dependencies equivalent to F, having no redundant dependencies or redundant parts of dependencies

Definition of Canonical Cover A canonical cover for F is a set of dependencies F c such that F logically implies all dependencies in F c, and F c logically implies all dependencies in F, and No functional dependency in F c contains an extraneous attribute, and Each left side of functional dependency in F c is unique. To compute a canonical cover for F: repeat Use the union rule to replace any dependencies in F 1 1 and 1 2 with 1 1 2 Find a functional dependency with an extraneous attribute either in or in /* Note: test for extraneous attributes done using F c, not F*/ If an extraneous attribute is found, delete it from until F does not change Note: Union rule may become applicable after some extraneous attributes have been deleted, so it has to be re-applied

Finding a Canonical Cover Another algorithm Write F as a set of dependencies where each has a single attribute on the right hand side Eliminate trivial dependencies In which and Eliminate redundant dependencies (implied by other dependencies) Combine dependencies with the same left hand side For any given set of FDs, the canonical cover is not necessarily unique

Uses of Functional Dependencies Testing for lossless-join decomposition Testing for dependency preserving decompositions Defining keys

Testing for Lossless-Join Decomposition The closure of a set of FDs can be used to test if a decomposition is lossless-join For the case of R = (R 1, R 2 ), we require that for all possible relations r on schema R r = R1 (r ) R2 (r ) A decomposition of R into R 1 and R 2 is lossless join if at least one of the following dependencies is in F + : R 1 R 2 R 1 R 1 R 2 R 2 Does the intersection of the decomposition satisfy at least one FD?

Testing for Dependency Preserving Decompositions The closure of a set of FDs allows us to test a new tuple being inserted into a table to see if it satisfies all relevant FDs without having to do a join This is desirable because joins are expensive Let F i be the set of dependencies F + that include only attributes in R i. A decomposition is dependency preserving, if (F 1 F 2 F n ) + = F + If it is not, then checking updates for violation of functional dependencies may require computing joins, which is expensive. The closure of a dependency preserving decomposition equals the closure of the original set Can all FDs be tested (either directly or by implication) without doing a join?

Keys and Functional Dependencies Given a relation scheme R with attribute set K R K is a superkey if K R K is a candidate key if there is no subset L of K such that L R A superkey with one attribute is always a candidate key Primary key is the candidate key K chosen by the designer Every relation must have a superkey (possibly the entire set of attributes) Key attribute an attribute that is or is part of a candidate key

Design Project Presentations Part 2: E-R Diagrams

Database Normalization

Database Design Goals (Updated) Goals Avoid redundancies and the resulting from insert, update, and delete anomalies by decomposing schemes as needed Ensure that all decompositions are lossless-join Ensure that all decompositions are dependency preserving Sometimes you cannot have all three Allow for redundancy to preserve dependencies Or give up dependency preservation to eliminate redundancy Never give up lossless-join as doing so would remove the ability to connect tuples in different relations Database normal forms help eliminate redundancy and anomalies Specify a set of decomposition rules to convert a database that is not in a given normal form into one that is

First Normal Form (1NF) A relation scheme R is in 1NF if the domains of all attributes in R are atomic Single and non-composite Guarantees that each non-key attribute in R is functionally dependent on the primary key

Second Normal Form (2NF) A 1NF relationship scheme R is in 2NF if each non-key attribute is fully functionally dependent on each candidate key Functionally dependent on the whole key, not just part of it This restriction does not apply to key attributes Avoids redundancy of information which is dependent on part of the primary key Any non-2nf scheme can be decomposed into 2NF schemes by factoring out The non-key attributes dependent on a portion of a candidate key The portion of the candidate key these attributes depend on Any 1NF scheme without a composite primary is in 2NF

Third Normal Form (3NF) A 2NF relation scheme R is in 3NF if no non-key attribute of R is transitively dependent on a candidate key through some other non-key attribute(s) This restriction does not apply to key attributes Transitive dependencies on a candidate key lead to insert, update, and delete anomalies Any non-3nf scheme can be decomposed into 3NF schemes by factoring out The transitively dependent attributes The transitional attributes which connect these to the candidate key Any non-3nf relation can be decomposed into 3NF in a lossless-join and dependency preserving manner

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 )

Boyce-Codd Normal Form (BCNF) 3NF did not take multiple candidate keys into account BCNF developed to address this A normalized relation is in BCNF if every FD satisfied by R is of the form A B, where A is a superkey BCNF is a stronger 3NF Every BCNF schema is also in 3NF Not every 3NF schema is in BCNF Some 3NF schemas cannot be decomposed into BCNF in a lossless-join and dependency preserving manner BCNF does not build on other normal forms

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.

Multivalued Dependencies (MVDs) A set of attributes A multi-determines a set of attributes B if In any relation including attributes A and B For any given value of A there is a (non-empty) set of values for B Such that we expect to see all of those B values (and no others) associated with the given A Along with remaining attribute values The number of B values associated with a given A value may vary between A values.

Formal Definition of Multivalued Dependency 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 ]

MVDs and E-R Diagrams MVDs correspond to multi-valued attributes A B A C

Properties of MVDs MVDs require the addition of certain tuples Example: copies of a book with multiple authors Opposite to FDs which prohibit certain tuples If A B, then A B FDs are a special case of MVDs An MVD is trivial if either of the following is true Its right-hand side is a subset of its left-hand side (just like FDs) The union of its left- and right-hand sides is the entire scheme The closure D+ of D is the set of all FDs and MVDs implied by D D+ can be computed from the formal definitions of FD and MVD Additional rules of inference (see Appendix C of Database Systems Concepts)

Fourth Normal Form (4NF) A relation schema R is in 4NF for all MVDs 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 (in which case it is an FD) If a relation is in 4NF it is in BCNF 4NF avoids redundancies introduced by MVDs

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

Database Design Guidelines Use the highest normal form possible 4NF unless it is not dependency preserving BCNF unless (in rare cases) it is not dependency preserving 3NF otherwise never need to compromise beyond this Lower normal forms may be useful for efficiency purposes Use good keys Every attribute should depend on the key, the whole key, and nothing but the key (BCNF) Avoid composite keys (automatic 2NF) Generate a unique single-attribute key if needed Factor out transitive dependencies ( sub-relations ) into their own schemes (3NF Isolate MVDs to their own schema (4NF)

Approaches to Database Design Start with a universal relation and decompose it The approach taken in this lecture Start with an E-R diagram Modify it while you normalize it Normalize it when converting it to a relational schema

Homework 3