Functional Dependencies and Relational Schema Design.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Lecture 6: Design Constraints and Functional Dependencies January 21st, 2004.
Functional Dependencies, Normalization Rose-Hulman Institute of Technology Curt Clifton.
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 227 Database Systems I Design Theory for Relational Databases.
1 Lecture 02: Conceptual Design Wednesday, October 6, 2010 Dan Suciu -- CSEP544 Fall 2010.
Lossless Decomposition Prof. Sin-Min Lee Department of Computer Science San Jose State University.
1 Lecture 10: Database Design XML Wednesday, October 20, 2004.
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:
1 Introduction to Database Systems CSE 444 Lectures 8 & 9 Database Design April 16 & 18, 2008.
Lecture #3 Functional Dependencies Normalization Relational Algebra Thursday, October 12, 2000.
Lossless Decomposition Prof. Sin-Min Lee Department of Computer Science San Jose State University.
1 Lecture 06 The Relational Data Model. 2 Outline Relational Data Model Functional Dependencies FDs in ER Logical Schema Design Reading Chapter 8.
M.P. Johnson, DBMS, Stern/NYU, Spring C : Database Management Systems Lecture #7 Matthew P. Johnson Stern School of Business, NYU Spring,
M.P. Johnson, DBMS, Stern/NYU, Spring C : Database Management Systems Lecture #4 M.P. Johnson Stern School of Business, NYU Spring, 2008.
Boyce-Codd NF & Lossless Decomposition Professor Sin-Min Lee.
M.P. Johnson, DBMS, Stern/NYU, Sp20041 C : Database Management Systems Lecture #6 Matthew P. Johnson Stern School of Business, NYU Spring, 2004.
Relation Decomposition A, A, … A 12n Given a relation R with attributes Create two relations R1 and R2 with attributes B, B, … B 12m C, C, … C 12l Such.
1 Lecture 4: Database Modeling (end) The Relational Data Model April 8, 2002.
©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.
E/R Diagrams and Functional Dependencies. Modeling Subclasses The world is inherently hierarchical. Some entities are special cases of others We need.
Lecture 08: E/R Diagrams and Functional Dependencies.
1 Schema Design & Refinement (aka Normalization).
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.
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.
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)
1 Lecture 7: Normal Forms, Relational Algebra Monday, 10/15/2001.
© D. Wong Ch. 3 (continued)  Database design problems  Functional Dependency  Keys of relations  Decompositions based on Functional Dependency.
Tallahassee, Florida, 2015 COP4710 Database Systems Relational Design Fall 2015.
1 Database Systems Lecture #4 Yan Pan School of Software, SYSU 2011.
1 Lecture 10: Database Design Wednesday, January 26, 2005.
Functional Dependencies and Relational Schema Design.
Lecture 3: Conceptual Database Design and Schema Design April 12 th, 2004.
Lecture 13: Relational Decomposition and Relational Algebra February 5 th, 2003.
1 Lecture 10: Database Design and Relational Algebra Monday, October 20, 2003.
1 Lecture 08: E/R Diagrams and Functional Dependencies Friday, January 21, 2005.
CS411 Database Systems Kazuhiro Minami 04: Relational Schema Design.
COMP 430 Intro. to Database Systems Normal Forms Slides use ideas from Chris Ré.
© 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.
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.
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.
Lecture 11: Functional Dependencies
COP4710 Database Systems Relational Design.
3.1 Functional Dependencies
Problems in Designing Schema
BCNF and Normalization
Lecture 2: Database Modeling (end) The Relational Data Model
Cse 344 May 16th – Normalization.
Functional Dependencies and Relational Schema Design
Introduction to Database Systems CSE 444 Lectures 8 & 9 Database Design October 12 & 15, 2007.
Lecture 09: Functional Dependencies, Database Design
Functional Dependencies
Lecture 8: Database Design
Lecture 07: E/R Diagrams and Functional Dependencies
Functional Dependencies
CSE544 Data Modeling, Conceptual Design
Functional Dependencies
Terminology Product Attribute names Name Price Category Manufacturer
Lecture 08: E/R Diagrams and Functional Dependencies
Lecture 6: Functional Dependencies
Lecture 11: Functional Dependencies
Lecture 09: Functional Dependencies
Presentation transcript:

Functional Dependencies and Relational Schema Design

Relational Schema Design Person buys Product name pricenamessn Conceptual Model: Relational Model: (plus FD’s) Normalization:

Functional Dependencies A form of constraint (hence, part of the schema) Finding them is part of the database design Also used in normalizing the relations

Outline Functional dependencies and keys (3.4,3.5) Normal forms: BCNF (3.6)

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: A, A, … A 12n B, B, … B 12m Main (and simplest) example: keys How many different FDs are there?

Examples EmpID Name, Phone, Position Position Phone but Phone Position EmpIDNamePhonePosition E0045Smith1234Clerk E1847John9876Salesrep E1111Smith9876Salesrep E9999Mary1234lawyer

In General To check A B, erase all other columns check if the remaining relation is many-one (called functional in mathematics)

Example

More Examples Product: name price, manufacturer Person: ssn name, age Company: name stock price, president Key of a relation is a set of attributes that: - functionally determines all the attributes of the relation - none of its subsets determines all the attributes. Superkey: a set of attributes that contains a key.

Finding the Keys of a Relation Given a relation constructed from an E/R diagram, what is its key? Rules: 1. If the relation comes from an entity set, the key of the relation is the set of attributes which is the key of the entity set. address namessn Person Person(address, name, ssn)

Finding the Keys Person buys Product name pricenamessn buys(name, ssn, date) date Rules: 2. If the relation comes from a many-many relationship, the key of the relation is the set of all attribute keys in the relations corresponding to the entity sets

Finding the Keys But: if there is an arrow from the relationship to E, then we don’t need the key of E as part of the relation key. Purchase Product Person Store Payment Method name card-no ssn sname Purchase(name, sname, ssn, card-no)

Finding the Keys More rules: Many-one, one-many, one-one relationships Multi-way relationships Weak entity sets (Try to find them yourself, check book)

Rules for FD’s A, A, … A 12n B, B, … B 12m A, A, … A 12n 1 Is equivalent to B A, A, … A 12n 2 B 12n m B … Splitting rule and Combing rule

Rules in FD’s (continued) A, A, … A 12n i A Trivial Rule Why ?

Rules in FD’s (continued) A, A, … A 12n Transitive Closure Rule B, B, … B 12m A, A, … A 12n 1 B, B …, B 2m1 C, C …, C 2p1 2p If and then Why ?

Closure of a set of Attributes Given a set of attributes {A1, …, An} and a set of dependencies S. Problem: find all attributes B such that: any relation which satisfies S also satisfies: A1, …, An B The closure of {A1, …, An}, denoted {A1, …, An}, is the set of all such attributes B +

Closure Algorithm Start with X={A1, …, An}. Repeat until X doesn’t change do: if is in S, and C is not in X then add C to X. B, B, … B 12n C 12 n are all in X, and

Example A B C A D E B D A F B Closure of {A,B}: X = {A, B, } Closure of {A, F}: X = {A, F, }

Why Is the Algorithm Correct ? Show the following by induction: –For every B in X: A1, …, An B Initially X = {A1, …, An} -- holds Induction step: B1, …, Bm in X –Implies A1, …, An B1, …, Bm –We also have B1, …, Bm C –By transitivity we have A1, …, An C This shows that the algorithm is sound; need to show it is complete

Relational Schema Design (or Logical Design) Main idea: Start with some relational schema Find out its FD’s Use them to design a better relational schema

Relational Schema Design Person buys Product name pricenamessn Conceptual Model: Relational Model: (plus FD’s) Normalization:

Relational Schema Design Goal: eliminate anomalies Redundancy anomalies Deletion anomalies Update anomalies

Relational Schema Design Name SSN Phone Number Fred (201) Fred (206) Joe (908) Joe (212) Anomalies: Redundancy = repeat data update anomalies = need to update in many places deletion anomalies = need to delete many tuples Recall set attributes (persons with several phones): Note: SSN no longer a key here

Relation Decomposition SSN Name Fred Joe SSN Phone Number (201) (206) (908) (212) Break the relation into two:

Decompositions in General A, A, … A 12n Let R be a relation with attributes Create two relations R1 and R2 with attributes B, B, … B 12m C, C, … C 12l Such that: B, B, … B 12m C, C, … C 12l  A, A, … A 12n And -- R1 is the projection of R on -- R2 is the projection of R on B, B, … B 12m C, C, … C 12l

Incorrect Decomposition NameCategory GizmoGadget OneClickCamera DoubleClickCamera PriceCategory 19.99Gadget 24.99Camera 29.99Camera NamePriceCategory Gizmo19.99Gadget OneClick24.99Camera OneClick29.99Camera DoubleClick24.99Camera DoubleClick29.99Camera When we put it back: Cannot recover information NamePriceCategory Gizmo19.99Gadget OneClick24.99Camera DoubleClick29.99Camera Decompose on : Name, Category and Price, Category

Normal Forms First Normal Form = all attributes are atomic Second Normal Form (2NF) = old and obsolete Third Normal Form (3NF) = this lecture Boyce Codd Normal Form (BCNF) = this lecture Others...

Boyce-Codd Normal Form A simple condition for removing anomalies from relations: A relation R is in BCNF if and only if: Whenever there is a nontrivial dependency for R, it is the case that { } a super-key for R. A, A, … A 12n B 12n In English (though a bit vague): Whenever a set of attributes of R is determining another attribute, should determine all the attributes of R.

Example Name SSN Phone Number Fred (201) Fred (206) Joe (908) Joe (212) What are the dependencies? SSN Name What are the keys? Is it in BCNF?

Decompose it into BCNF SSN Name Fred Joe SSN Phone Number (201) (206) (908) (212) SSN Name

What About This? Name Price Category Gizmo $19.99 gadgets OneClick $24.99 camera Name Price, Category

BCNF Decomposition Find a dependency that violates the BCNF condition: A, A, … A 12n B, B, … B 12m A’s Others B’s R1R2 Heuristics: choose B, B, … B “as large as possible” 12m Decompose: Find a 2-attribute relation that is not in BCNF. Continue until there are no BCNF violations left.

Example Decomposition Name SSN Age EyeColor PhoneNumber Functional dependencies: SSN Name, Age, Eye Color What if we also had an attribute Draft-worthy, and the FD: Age Draft-worthy Person: BNCF: Person1(SSN, Name, Age, EyeColor), Person2(SSN, PhoneNumber)

Other Example R(A,B,C,D) A B, B C Key: Violations of BCNF: Pick : split into R1( ) R2( )

Correct Decompositions A decomposition is lossless if we can recover: R(A,B,C) R1(A,B) R2(A,C) R’(A,B,C) = R(A,B,C) R’ is in general larger than R. Must ensure R’ = R