Functional Dependencies
Total Page:16
File Type:pdf, Size:1020Kb
Basics of FDs Manipulating FDs Closures and Keys Minimal Bases Functional Dependencies T. M. Murali October 19, 21 2009 T. M. Murali October 19, 21 2009 CS 4604: Functional Dependencies Students(Id, Name) Advisors(Id, Name) Advises(StudentId, AdvisorId) Favourite(StudentId, AdvisorId) I Suppose we perversely decide to convert Students, Advises, and Favourite into one relation. Students(Id, Name, AdvisorId, AdvisorName, FavouriteAdvisorId) Basics of FDs Manipulating FDs Closures and Keys Minimal Bases Example Name Id Name Id Students Advises Advisors Favourite I Convert to relations: T. M. Murali October 19, 21 2009 CS 4604: Functional Dependencies I Suppose we perversely decide to convert Students, Advises, and Favourite into one relation. Students(Id, Name, AdvisorId, AdvisorName, FavouriteAdvisorId) Basics of FDs Manipulating FDs Closures and Keys Minimal Bases Example Name Id Name Id Students Advises Advisors Favourite I Convert to relations: Students(Id, Name) Advisors(Id, Name) Advises(StudentId, AdvisorId) Favourite(StudentId, AdvisorId) T. M. Murali October 19, 21 2009 CS 4604: Functional Dependencies Basics of FDs Manipulating FDs Closures and Keys Minimal Bases Example Name Id Name Id Students Advises Advisors Favourite I Convert to relations: Students(Id, Name) Advisors(Id, Name) Advises(StudentId, AdvisorId) Favourite(StudentId, AdvisorId) I Suppose we perversely decide to convert Students, Advises, and Favourite into one relation. Students(Id, Name, AdvisorId, AdvisorName, FavouriteAdvisorId) T. M. Murali October 19, 21 2009 CS 4604: Functional Dependencies Name and FavouriteAdvisorId. Id ! Name Id ! FavouriteAdvisorId AdvisorId ! AdvisorName I Can we say Id ! AdvisorId? NO! Id is not a key for the Students relation. I What is the key for the Students? fId, AdvisorIdg. I Why is this relation \bad?" Parts of the key determine other attributes. Basics of FDs Manipulating FDs Closures and Keys Minimal Bases Example of a Bad Relation Students(Id, Name, AdvisorId, AdvisorName, FavouriteAdvisorId) I If you know a student's Id, can you determine the values of any other attributes? T. M. Murali October 19, 21 2009 CS 4604: Functional Dependencies Id ! Name Id ! FavouriteAdvisorId AdvisorId ! AdvisorName I Can we say Id ! AdvisorId? NO! Id is not a key for the Students relation. I What is the key for the Students? fId, AdvisorIdg. I Why is this relation \bad?" Parts of the key determine other attributes. Basics of FDs Manipulating FDs Closures and Keys Minimal Bases Example of a Bad Relation Students(Id, Name, AdvisorId, AdvisorName, FavouriteAdvisorId) I If you know a student's Id, can you determine the values of any other attributes? Name and FavouriteAdvisorId. T. M. Murali October 19, 21 2009 CS 4604: Functional Dependencies AdvisorId ! AdvisorName I Can we say Id ! AdvisorId? NO! Id is not a key for the Students relation. I What is the key for the Students? fId, AdvisorIdg. I Why is this relation \bad?" Parts of the key determine other attributes. Basics of FDs Manipulating FDs Closures and Keys Minimal Bases Example of a Bad Relation Students(Id, Name, AdvisorId, AdvisorName, FavouriteAdvisorId) I If you know a student's Id, can you determine the values of any other attributes? Name and FavouriteAdvisorId. Id ! Name Id ! FavouriteAdvisorId T. M. Murali October 19, 21 2009 CS 4604: Functional Dependencies AdvisorName I Can we say Id ! AdvisorId? NO! Id is not a key for the Students relation. I What is the key for the Students? fId, AdvisorIdg. I Why is this relation \bad?" Parts of the key determine other attributes. Basics of FDs Manipulating FDs Closures and Keys Minimal Bases Example of a Bad Relation Students(Id, Name, AdvisorId, AdvisorName, FavouriteAdvisorId) I If you know a student's Id, can you determine the values of any other attributes? Name and FavouriteAdvisorId. Id ! Name Id ! FavouriteAdvisorId AdvisorId ! T. M. Murali October 19, 21 2009 CS 4604: Functional Dependencies I Can we say Id ! AdvisorId? NO! Id is not a key for the Students relation. I What is the key for the Students? fId, AdvisorIdg. I Why is this relation \bad?" Parts of the key determine other attributes. Basics of FDs Manipulating FDs Closures and Keys Minimal Bases Example of a Bad Relation Students(Id, Name, AdvisorId, AdvisorName, FavouriteAdvisorId) I If you know a student's Id, can you determine the values of any other attributes? Name and FavouriteAdvisorId. Id ! Name Id ! FavouriteAdvisorId AdvisorId ! AdvisorName T. M. Murali October 19, 21 2009 CS 4604: Functional Dependencies NO! Id is not a key for the Students relation. I What is the key for the Students? fId, AdvisorIdg. I Why is this relation \bad?" Parts of the key determine other attributes. Basics of FDs Manipulating FDs Closures and Keys Minimal Bases Example of a Bad Relation Students(Id, Name, AdvisorId, AdvisorName, FavouriteAdvisorId) I If you know a student's Id, can you determine the values of any other attributes? Name and FavouriteAdvisorId. Id ! Name Id ! FavouriteAdvisorId AdvisorId ! AdvisorName I Can we say Id ! AdvisorId? T. M. Murali October 19, 21 2009 CS 4604: Functional Dependencies I What is the key for the Students? fId, AdvisorIdg. I Why is this relation \bad?" Parts of the key determine other attributes. Basics of FDs Manipulating FDs Closures and Keys Minimal Bases Example of a Bad Relation Students(Id, Name, AdvisorId, AdvisorName, FavouriteAdvisorId) I If you know a student's Id, can you determine the values of any other attributes? Name and FavouriteAdvisorId. Id ! Name Id ! FavouriteAdvisorId AdvisorId ! AdvisorName I Can we say Id ! AdvisorId? NO! Id is not a key for the Students relation. T. M. Murali October 19, 21 2009 CS 4604: Functional Dependencies fId, AdvisorIdg. I Why is this relation \bad?" Parts of the key determine other attributes. Basics of FDs Manipulating FDs Closures and Keys Minimal Bases Example of a Bad Relation Students(Id, Name, AdvisorId, AdvisorName, FavouriteAdvisorId) I If you know a student's Id, can you determine the values of any other attributes? Name and FavouriteAdvisorId. Id ! Name Id ! FavouriteAdvisorId AdvisorId ! AdvisorName I Can we say Id ! AdvisorId? NO! Id is not a key for the Students relation. I What is the key for the Students? T. M. Murali October 19, 21 2009 CS 4604: Functional Dependencies I Why is this relation \bad?" Parts of the key determine other attributes. Basics of FDs Manipulating FDs Closures and Keys Minimal Bases Example of a Bad Relation Students(Id, Name, AdvisorId, AdvisorName, FavouriteAdvisorId) I If you know a student's Id, can you determine the values of any other attributes? Name and FavouriteAdvisorId. Id ! Name Id ! FavouriteAdvisorId AdvisorId ! AdvisorName I Can we say Id ! AdvisorId? NO! Id is not a key for the Students relation. I What is the key for the Students? fId, AdvisorIdg. T. M. Murali October 19, 21 2009 CS 4604: Functional Dependencies Parts of the key determine other attributes. Basics of FDs Manipulating FDs Closures and Keys Minimal Bases Example of a Bad Relation Students(Id, Name, AdvisorId, AdvisorName, FavouriteAdvisorId) I If you know a student's Id, can you determine the values of any other attributes? Name and FavouriteAdvisorId. Id ! Name Id ! FavouriteAdvisorId AdvisorId ! AdvisorName I Can we say Id ! AdvisorId? NO! Id is not a key for the Students relation. I What is the key for the Students? fId, AdvisorIdg. I Why is this relation \bad?" T. M. Murali October 19, 21 2009 CS 4604: Functional Dependencies Basics of FDs Manipulating FDs Closures and Keys Minimal Bases Example of a Bad Relation Students(Id, Name, AdvisorId, AdvisorName, FavouriteAdvisorId) I If you know a student's Id, can you determine the values of any other attributes? Name and FavouriteAdvisorId. Id ! Name Id ! FavouriteAdvisorId AdvisorId ! AdvisorName I Can we say Id ! AdvisorId? NO! Id is not a key for the Students relation. I What is the key for the Students? fId, AdvisorIdg. I Why is this relation \bad?" Parts of the key determine other attributes. T. M. Murali October 19, 21 2009 CS 4604: Functional Dependencies Basics of FDs Manipulating FDs Closures and Keys Minimal Bases Motivation for Functional Dependencies I Reason about constraints on attributes in a relation. I Procedurally determine the keys of a relation. I Detect when a relation has redundant information. I Improve database designs systematically using normalisation. T. M. Murali October 19, 21 2009 CS 4604: Functional Dependencies I The FD AdvisorId ! AdvisorName holds in R if in every instance of R, for every pair of tuples t and u if tAdvisorId = uAdvisorId, then tAdvisorName = uAdvisorName. Basics of FDs Manipulating FDs Closures and Keys Minimal Bases Definition of Functional Dependency I If t is a tuple in a relation R and A is an attribute of R, then tA is the value of attribute A in tuple t. T. M. Murali October 19, 21 2009 CS 4604: Functional Dependencies if tAdvisorId = uAdvisorId, then tAdvisorName = uAdvisorName. Basics of FDs Manipulating FDs Closures and Keys Minimal Bases Definition of Functional Dependency I If t is a tuple in a relation R and A is an attribute of R, then tA is the value of attribute A in tuple t. I The FD AdvisorId ! AdvisorName holds in R if in every instance of R, for every pair of tuples t and u T. M. Murali October 19, 21 2009 CS 4604: Functional Dependencies Basics of FDs Manipulating FDs Closures and Keys Minimal Bases Definition of Functional Dependency I If t is a tuple in a relation R and A is an attribute of R, then tA is the value of attribute A in tuple t. I The FD AdvisorId ! AdvisorName holds in R if in every instance of R, for every pair of tuples t and u if tAdvisorId = uAdvisorId, then tAdvisorName = uAdvisorName. T. M. Murali October 19, 21 2009 CS 4604: Functional Dependencies I FD says that for every pair of tuples t and u in any instance of R, if tA1 = uA1 and tA2 = uA2 and ::: tAn = uAn , then tB = uB .