Closure The closure of {B 1 …B k } under the set of FDs S, denoted by {B 1 …B k } +, is defined as follows: {B 1 …B k } + = {B | any relation satisfies.

Slides:



Advertisements
Similar presentations
Schema Refinement: Canonical/minimal Covers
Advertisements

Logical Database Design (3 of 3) John Ortiz. Lecture 7Logical Database Design (2)2 Normalization  If a relation is not in BCNF or 3NF, we refine it by.
CS 440 Database Management Systems Practice problems for normalization.
Spring 2011 Instructor: Hassan Khosravi
Normalization CMSC 461 Michael Wilson. Anomalies  Poor relational database design can lead to the occurrence of anomalies  Anomalies that we tend to.
1 Design Theory. 2 Minimal Sets of Dependancies A set of dependencies is minimal if: 1.Every right side is a single attribute 2.For no X  A in F and.
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.
Functional Dependencies - Example
Lossless Decomposition (2) Prof. Sin-Min Lee Department of Computer Science San Jose State University.
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 227 Database Systems I Design Theory for Relational Databases.
Midterm Review II. Redundancy. –Information may be repeated unnecessarily in several tuples. –E.g. length and filmType. Update anomalies. –We may change.
Functional Dependencies
Functional Dependencies. Babies At a birth, there is one baby (twins would be represented by two births), one mother, any number of nurses, and a doctor.
603 Database Systems Senior Lecturer: Laurie Webster II, M.S.S.E.,M.S.E.E., M.S.BME, Ph.D., P.E. Lecture 8 A First Course in Database Systems.
Decomposition By Timothy Chen CS157A. Goal to Decomposition Eliminate redundancy by decomposing a relation into several relations in a higher normal form.
Classroom Exercise: Normalization
Functional Dependencies Definition: If two tuples agree on the attributes A, A, … A 12n then they must also agree on the attributes B, B, … B 12m Formally:
CMSC424: Database Design Instructor: Amol Deshpande
The Relational Data Model. Why Relational Model? Most of the current DBMS are based on the relational data model: –simplicity –mathematically based: expressions.
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.
Multivalued Dependencies. Intuition Redundancy: addresses, title repeated several times –because a star might have several addresses and stars in several.
M.P. Johnson, DBMS, Stern/NYU, Spring C : Database Management Systems Lecture #7 Matthew P. Johnson Stern School of Business, NYU Spring,
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.
Fall 2001Arthur Keller – CS 1804–1 Schedule Today Oct. 4 (TH) Functional Dependencies and Normalization. u Read Sections Project Part 1 due. Oct.
Functional Dependencies and Relational Schema Design.
Chapter 8: Relational Database Design First Normal Form First Normal Form Functional Dependencies Functional Dependencies Decomposition Decomposition Boyce-Codd.
©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.
Multi-valued Dependencies and Fourth Normal Form
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.
Normal Forms1. 2 The Problems of Redundancy Redundancy is at the root of several problems associated with relational schemas: Wastes storage Causes problems.
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 09: Functional Dependencies. Outline Functional dependencies (3.4) Rules about FDs (3.5) Design of a Relational schema (3.6)
Design Theory for Relational Databases 2015, Fall Pusan National University Ki-Joune Li.
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.
THIRD NORMAL FORM (3NF) A relation R is in BCNF if whenever a FD XA holds in R, one of the following statements is true: XA is a trivial FD, or X is.
Revisit FDs & BCNF Normalization 1 Instructor: Mohamed Eltabakh
Functional Dependencies. FarkasCSCE 5202 Reading and Exercises Database Systems- The Complete Book: Chapter 3.1, 3.2, 3.3., 3.4 Following lecture slides.
IST 210 Normalization 2 Todd Bacastow IST 210. Normalization Methods Inspection Closure Functional dependencies are key.
Functional Dependencies. Outline Functional dependencies (3.4) Rules about FDs (3.5) Design of a Relational schema (3.6)
© D. Wong Ch. 3 (continued)  Database design problems  Functional Dependency  Keys of relations  Decompositions based on Functional Dependency.
Rensselaer Polytechnic Institute CSCI-4380 – Database Systems David Goldschmidt, Ph.D.
Design Theory for RDB Normal Forms. Lu Chaojun, SJTU 2 Redundant because these info may be figured out by using FD s1  … What’s Bad Design? Redundancy.
3 Spring Chapter Normalization of Database Tables.
CS 405G: Introduction to Database Systems Instructor: Jinze Liu Fall 2009.
Functional Dependencies CIS 4301 Lecture Notes Lecture 8 - 2/7/2006.
Rensselaer Polytechnic Institute CSCI-4380 – Database Systems David Goldschmidt, Ph.D.
Functional dependencies CMSC 461 Michael Wilson. Designing tables  Now we have all the tools to build our databases  How should we actually go about.
Multivalued Dependencies and 4th NF CIS 4301 Lecture Notes Lecture /21/2006.
Rensselaer Polytechnic Institute CSCI-4380 – Database Systems David Goldschmidt, Ph.D.
CPSC 603 Database Systems Lecturer: Laurie Webster II, M.S.S.E., M.S.E.E., M.S.BME, Ph.D., P.E. Lecture 5 Introduction to a First Course in Database Systems.
© 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.
1 Lecture 9: Database Design Wednesday, January 25, 2006.
Normalization and FUNctional Dependencies. Redundancy: root of several problems with relational schemas: –redundant storage, insert/delete/update anomalies.
Databases : Design of Relational Database Schemas 2007, Fall Pusan National University Ki-Joune Li.
More on Decompositions and Third Normal Form CIS 4301 Lecture Notes Lecture /16/2006.
Formal definition of a key A key is a set of attributes A 1,..., A n such that for any other attribute B: A 1,..., A n  B A minimal key is a set of attributes.
Schedule Today: Next After that Normal Forms. Section 3.6.
3.1 Functional Dependencies
Design Theory for Relational Databases
Functional Dependencies and Normalization
Lecture 6: Functional Dependencies
Chapter 3: Design theory for relational Databases
CS4222 Principles of Database System
Design Theory for Relational Databases
Presentation transcript:

Closure The closure of {B 1 …B k } under the set of FDs S, denoted by {B 1 …B k } +, is defined as follows: {B 1 …B k } + = {B | any relation satisfies S will also satisfies B 1 …B k  B}

Computing the closure Given: the set S and {A 1,…,A n } Compute: {A 1,…,A n } + - denote this set by X Step 1: X = {A 1,…,A n } Step 2: find a FD B 1 …B k  B in S such that {B 1,…,B k }  X and B  X, then X=X  {B} Step 3: repeat step 2 until nothing more can be added to X, then go to step 4 Step 4: return X

Example S = {AB  C, BC  AD, D  E, CF  B} Compute {A,B} + Step 1: X = {A,B} Step 2: X = X  {C}={A,B,C} because AB  C Step 3 back to step 2: X = X  {D} because BC  AD Step 3 back to step 2: X = X  {E} because D  E Step 3 back to step 2: nothing more Step 3 go to step 4: return {A,B,C,D,E}

Correctness of closure algorithm It computes true functional dependencies –proof: show that if B belongs to {A 1,…,A n } + then A 1 …A n  B holds. By induction over the number of steps (n) used in adding an attribute B into the set X n=0 then B belongs to {A 1,…,A n } and so A 1 …A n  B is a trivial functional dependency n  n+1: if B is added to X in the step n+1, then A 1 …A n  B j for all j by inductive hypothesis; this, together with B 1 …B k  B, implies that A 1 …A n  B It computes all functional dependencies –proof: show that if B does not belong to {A 1,…,A n } + then A 1 …A n  B does not hold. By constructing an instance I of the relation R such that the FD does not hold. A in the closure Others 111 … … … 111

Simple questions What is {A 1,…,A n } + if {A 1,…,A n } is a key of the relation? Can {A 1,…,A n } + ={A 1,…,A n }? Does {B 1,…,B m }  {A 1,…,A n } imply {B 1,…,B m } +  {A 1,…,A n } + ?

Transitive Rules GivenA 1 …A n  B 1 …B m B 1 …B m  C 1 …C k then A 1 …A n  C 1 …C k

Closing sets of FDs Given a set of FDs we can derive some other FDs using the rules about FDs (e.g. combining, splitting, and transitive) For a relation R, a set of FD is called a basis for R if all other FDs of R can be derived form it. A basis is minimal if none of its proper subsets is a basis.

Projecting FDs B’s R S Given: –R with a set of FDs F –S (a new relation) is obtained by removing the attributes {B 1,…,B m } from R Questions: What are the FDs of S? Answer: if A 1 …A n  C 1 …C k is a FD of R and none of the Bs appears on the left or right side ({B 1,…,B m }  {A 1,…,A n,C 1,…,C k }=  ) is a FD of S

Projecting - Example Given R(A,B,C,D) with the FDs A  B, B  C, and C  D. Remove the attribute B from R, we obtain a new relation S(A,C,D). What are the FDs of S? –A  C? –A  D? –C  D? We can compute this by: Compute all the closure of every subset of {A,C,D} by using the FDs of R that do not contain B.

Homework Consider a relation with schema R(A,B,C,D) and FD’s AB  C, C  D, and D  A. –What are all the nontrivial FD’s that follow from the given FD’s? List only the FDs with one attribute on the right? (5pt) –What are the keys of R? (5pt) –What are the superkeys but not keys? (5pt) Show that the following rule holds: (5pt) if A 1 …A n  B 1 …B m and C 1 …C k  D 1 …D t hold then A 1 …A n C 1 …C k  B 1 …B m D 1 …D t also holds.

For those whole like fun: Does the following hold: –if A  B then B  A –if AB  C and A  C then B  C A set of attributes is closed if X + =X. What are the FDs of a relation R(A,B,C,D) if –all sets of four attributes are closed –the only closed sets are {} and {A,B,C,D} –the closed sets are {}, {A,B}, {A,B,C,D} (note: the cases are considered separate) Stars: try the exercises with stars.

Design of Relational Database Schema titleyearlengthstudioNamestarNamefilmType Star Wars FoxMark Hamillcolor Star Wars FoxHarrison Fordcolor Star Wars FoxCarrie Fishercolor Mighty Ducks DisneyEmilio Estevezcolor Wayne’s World199295ParamountDana Carveycolor Wayne’s World199295ParamountMike Meyerscolor Some observations: value of studioName is the same in several tuples value of filmType is also repeated What wrong with it? redundancy  store the same value unnecessary several time update anormalies  an update might require several changes deletion anormalies  losing information if delete a value CAN WE AVOID THESE ANORMALIES?

Possible ways to avoid anormalies (Intuition) The bad way: start again (Oh, no!) The natural way: try to decompose the given relation into two or more relations that –contain the same information –avoid the anormalies

Example titleyearlengthstudioNamestarNamefilmType Star Wars FoxMark Hamillcolor Star Wars FoxHarrison Fordcolor Star Wars FoxCarrie Fishercolor Mighty Ducks DisneyEmilio Estevezcolor Wayne’s World199295ParamountDana Carveycolor Wayne’s World199295ParamountMike Meyerscolor titleyearlengthstudioNamefilmType Star Wars Foxcolor Mighty Ducks Disneycolor Wayne’s World Paramountcolor titleyearstarName Star Wars1977Mark Hamill Star Wars1977Harrison Ford Star Wars1977Carrie Fisher Mighty Ducks1991Emilio Estevez Wayne’s World1992Dana Carvey Wayne’s World1992Mike Meyers MovieStudioStar(title, year, length, studioName, starName, filmType) is decomposed into 2 relations MovieStudio(title, year, length, studioName, filmType) and StarsIn(title, year, starName)

Decomposition Given a relation R with schema {A 1,…,A n }. A decomposition of R into two relations S and T with schemas {B 1,…,B m } and {C 1,…,C k }, respectively, such that 1.{A 1,…,A n } = {B 1,…,B m }  {C 1,…,C k } 2.The tuples in S are the projections onto {B 1,…,B m } of all the tuples in R. 3.The tuples in T are the projections onto {C 1,…,C k } of all the tuples in R.

Example – Projections titleyearlengthstudioNamestarNamefilmType Star Wars FoxMark Hamillcolor Star Wars FoxHarrison Fordcolor Star Wars FoxCarrie Fishercolor Mighty Ducks DisneyEmilio Estevezcolor Wayne’s World199295ParamountDana Carveycolor Wayne’s World199295ParamountMike Meyerscolor titleyearlengthstudioNamefilmType Star Wars Foxcolor Mighty Ducks Disneycolor Wayne’s World Paramountcolor titleyearstarName Star Wars1977Mark Hamill Star Wars1977Harrison Ford Star Wars1977Carrie Fisher Mighty Ducks1991Emilio Estevez Wayne’s World1992Dana Carvey Wayne’s World1992Mike Meyers MovieStudioStar(title, year, length, studioName, starName, filmType) is decomposed into 2 relations MovieStudio(title, year, length, studioName, filmType) and StarsIn(title, year, starName) How do we come up with this decomposition?

Boyce-Codd Normal Form (BCNF) BCNF: a relation R is in BCNF iff: whenever there is a nontrivial FD A 1 …A n  B for R, it is the case that {A 1,…,A n } is a superkey for R. Why this definition? Answer: if a relation is in BCNF then there is no anormaly. Example: MovieStudioStar(title, year, length, studioName, starName, filmType): not in BCNF MovieStudio(title, year, length, studioName, filmType): in BCNF StarsIn(title, year, starName): in BCNF

Decomposition into BCNF Suppose that we decompose a relation R into two relations S and T which are in BCNF. The requirements for S and T: –S and T is a decomposition of R –it is possible to reconstruct R from S and T Will every decomposition of R satisfy these two conditions? What are the FDs of the new relations?

Algorithm Given a relation R with the attributes {A 1,…,A n }. Step 1: For every nontrivial FD B 1 …B m  B if {B 1,…,B m } is a superkey then returns R (no decomposition is needed) Step 2: Takes a nontrivial FD B 1 …B m  B such that {B 1,…,B m } is not a superkey, then decomposes R into two relations S and T with the following schema: –S’s schema: {B 1,…,B m } + –T’s schema: {B 1,…,B m }  ({A 1,…,A n }\{B 1,…,B m } + ) Repeat Step 1&2 for S and T until no decomposition is needed for every new relation; return the set of new relations as the result

Example The ‘new’ movie relation with the following attributes: {title,year,studioName,president,presAddress} (we call this set ALL) with the FDs: {title year  studioName, studioName  president, president  presAddress} Only one key: {title,year} studioName  president violated BCNF Step 2: takes studioName  president, decomposes into –S with the schema {studioName} + ={studioName,president,presAddress} –T with the schema {studioName,title,year}={studioName}  (ALL\ {studioName} + ) Check: {studioName,title,year} is in BCNF (the first two FDs) {studioName,president,presAddress} is not in BCNF Continue with the decomposition of S using president  presAddress and we get the following two relation schemas: {president,presAddress} and {president,studioName} both are in BCNF. The final result: {studioName,title,year}, {president,presAddress},{president,studioName}

Recovering information from a decomposition Suppose that R with the schema {A 1,…,A n } is decomposed into two relations S and T according to the algorithm whose attributes are {B 1,…,B m } + and {B 1,…,B m }  ({A 1,…,A n }\{B 1,…,B m } + ) The tuples of R can be obtained by joining all possible pairs of S and T where {B 1,…,B m } have the same values.

Recovering … t (R) t’ (S) t’’ (T) the B’s the rest of the closure others Projection Join {B1,…,Bm} {A1,…,An}\{B1,…,Bm} + {B1,…,Bm} + \ {B1,…,Bm}

Example – Decomposition and Recovering titleyearlengthstudioNamestarNamefilmType Star Wars FoxMark Hamillcolor Star Wars FoxHarrison Fordcolor Star Wars FoxCarrie Fishercolor Mighty Ducks DisneyEmilio Estevezcolor Wayne’s World199295ParamountDana Carveycolor Wayne’s World199295ParamountMike Meyerscolor titleyearlengthstudioNamefilmType Star Wars Foxcolor Mighty Ducks Disneycolor Wayne’s World Paramountcolor titleyearstarName Star Wars1977Mark Hamill Star Wars1977Harrison Ford Star Wars1977Carrie Fisher Mighty Ducks1991Emilio Estevez Wayne’s World1992Dana Carvey Wayne’s World1992Mike Meyers MovieStudioStar(title, year, length, studioName, starName, filmType) is not in BCNF is decomposed into 2 relations that are in BCNF: MovieStudio(title, year, length, studioName, filmType) and StarsIn(title, year, starName)

Some remarks The algorithm will stop and output a set of BCNF relations. Not every decomposition according to the algorithm is good The FD’s for the new relations are determined by ‘projecting’. If a decomposition is based on FDs (according to the algorithm) then the recovering process will give us exactly the original relation. If a decomposition is not based on FDs then we might not be able to recover the original relation from the new ones: –Example: R(A,B,C) with A  B and we decompose it into S(A,B) and T(B,C): ABC AB BC ABC

Third Normal Form (3NF) So far: if a relation is not in BCNF then anormalies arise. Given a relation Bookings with the attributes: –title: name of the movie –theater: name of the theater where the movie is being shown –city: the city where the theater is located (a tuple (m,t,c): represents the fact that movie m is shown at theater t in city c)

Bookings(title,theater,city) The FDs of the relations: –theater  city –title city  theater theater  city violates the BCNF condition, why? decomposition yields: {theater,city} and {theater,title} Consider the relations: theatercity GuildMenlo ParkMenlo theatertitle GuildNet ParkNet theatertitlecity GuildNetMenlo ParkNetMenlo recovering Violate the FD title city  theater Possible relations according to the FDs of each schema

3NF A relaxation of the BCNF condition: a relation R is in 3NF if: whenever there is a nontrivial FD A 1 …A n  B, either {A 1,…,A n } is a superkey or B is a member of some key. Bookings(title,theater,city) is in 3NF

Checking BCNF and 3NF Given R(A,B,C,D) with FDs AB  C, C  D, D  A. Question: Indicate the BCNF violations and 3NF violations. Steps in answering the question: –Step 1: compute all nontrivial FDs (right side: one att) –Step 2: find all keys –Step 3: find all the violations Step 1: AB  C, C  D, D  A, AB  D, C  A, DB  C, AC  D Step 2: Keys – {A,B}, {C,B}, and {D,B} Step 3: –BCNF violation: C  D, D  A, C  A, AC  D and their trivial extensions (e.g. CD  D, DA  A,…) –3NF violation: none