Santa Clara UniversityHolliday – COEN 1783–1 Today’s Topic Today: u Relational Model, Functional Dependencies. u Read Sections 3.1-3.5. Next time u Normal.

Slides:



Advertisements
Similar presentations
Relational Model Table = relation. Column headers = attributes. Row = tuple Beers Relation schema = name(attributes) + other structure info., e.g., keys,
Advertisements

Database Management Systems Chapter 3 The Relational Data Model (I) Instructor: Li Ma Department of Computer Science Texas Southern University, Houston.
Temple University – CIS Dept. CIS616– Principles of Data Management V. Megalooikonomou Functional Dependencies (based on notes by Silberchatz,Korth, and.
Functional Dependencies - Example
Databases : Relational Model 2007, Fall Pusan National University Ki-Joune Li These slides are made from the materials that Prof. Jeffrey D. Ullman distributes.
Topics to be discusses Functional Dependency Key
Chapter 4 Notes. Entity-Relationship Model E/R Diagrams Weak Entity Sets Converting E/R Diagrams to Relations.
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 227 Database Systems I Design Theory for Relational Databases.
1 Relational Model and Translating ER into Relational.
1 The Relational Data Model Functional Dependencies.
Chapter 7: Relational Database Design. ©Silberschatz, Korth and Sudarshan7.2Database System Concepts Chapter 7: Relational Database Design First Normal.
1 Announcement Recitation time  Before midterm: 6-7pm, by Earl Wagner  After midterm: 5-6pm, by Yi Qiao Newsgroup safe to subscribe  Will not cause.
Slides adapted from A. Silberschatz et al. Database System Concepts, 5th Ed. Relational Database Design - part 2 - Database Management Systems I Alex Coman,
1 Functional Dependencies Meaning of FD’s Keys and Superkeys Inferring FD’s.
1 Functional Dependencies Meaning of FD’s Keys and Superkeys Inferring FD’s Source: slides by Jeffrey Ullman.
1 CMSC424, Spring 2005 CMSC424: Database Design Lecture 9.
Winter 2002Arthur Keller – CS 1804–1 Schedule Today: Jan. 15 (T) u Normal Forms, Multivalued Dependencies. u Read Sections Assignment 1 due. Jan.
1 The Relational Data Model Tables Schemas Conversion from E/R to Relations.
Fall 2001Arthur Keller – CS 1803–1 Schedule Today Oct. 2 (T) Relational Model. u Read Sections Assignment 1 due. Personal Letter of Introduction.
1 The Relational Data Model Tables Schemas Conversion from E/R to Relations Source: slides by Jeffrey Ullman.
Winter 2002Arthur Keller – CS 1803–1 Schedule Today: Jan. 10 (TH) u Relational Model, Functional Dependencies. u Read Sections Jan. 15 (T) u Normal.
Fall 2001Arthur Keller – CS 1804–1 Schedule Today Oct. 4 (TH) Functional Dependencies and Normalization. u Read Sections Project Part 1 due. Oct.
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.
1 The Relational Data Model Tables Schemas Conversion from E/R to Relations.
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.
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.
Logical Database Design (1 of 3) John Ortiz Lecture 6Logical Database Design (1)2 Introduction  The logical design is a process of refining DB schema.
Entity-Relationship Model
Functional Dependencies. FarkasCSCE 5202 Reading and Exercises Database Systems- The Complete Book: Chapter 3.1, 3.2, 3.3., 3.4 Following lecture slides.
1 IT 244 Database Management System Topic 7 The Relational Data Model Functional Dependencies Ref : -A First Course in Database System (Jeffrey D Ullman,
IST 210 Normalization 2 Todd Bacastow IST 210. Normalization Methods Inspection Closure Functional dependencies are key.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide
© D. Wong Ch. 3 (continued)  Database design problems  Functional Dependency  Keys of relations  Decompositions based on Functional Dependency.
Databases 1 Fifth lecture. Entity-Relationship Model Diagrams Class hierarchies Weak entity sets From E/R diagrams to Relations 2.
Instructor: Jinze Liu Fall Phases of Database Design u Conceptual design begins with the collection of requirements and results needed from the.
1 Multivalued Dependencies Fourth Normal Form Reasoning About FD’s + MVD’s.
Functional Dependencies CIS 4301 Lecture Notes Lecture 8 - 2/7/2006.
Rensselaer Polytechnic Institute CSCI-4380 – Database Systems David Goldschmidt, Ph.D.
1 The Relational Data Model Tables Schemas Conversion from E/R to Relations Functional Dependencies.
CS 405G: Introduction to Database Systems Relations.
1 Lecture 08: E/R Diagrams and Functional Dependencies Friday, January 21, 2005.
CS542 1 Schema Refinement Chapter 19 (part 1) Functional Dependencies.
Functional Dependencies Zaki Malik September 25, 2008.
CS411 Database Systems Kazuhiro Minami 04: Relational Schema Design.
Databases 1 Sixth lecture. 2 Functional Dependencies X -> A is an assertion about a relation R that whenever two tuples of R agree on all the attributes.
© 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.
PMIT-6102 Advanced Database Systems By- Jesmin Akhter Assistant Professor, IIT, Jahangirnagar University.
1 The Relational Data Model David J. Stucki. Relational Model Concepts 2 Fundamental concept: the relation  The Relational Model represents an entire.
1 The Relational Data Model Tables Schemas Conversion from E/R to Relations.
Design Theory for Relational Databases Functional Dependencies Decompositions Normal Forms: BCNF, Third Normal Form Introduction to Multivalued Dependencies.
IT 5433 LM3 Relational Data Model. Learning Objectives: List the 5 properties of relations List the properties of a candidate key, primary key and foreign.
CSC 411/511: DBMS Design Dr. Nan Wang 1 Schema Refinement and Normal Forms Chapter 19.
Introduction to Database Systems, CS420
Entity-Relationship Model
Design Theory for Relational Databases
Schedule Today: Next After that Normal Forms. Section 3.6.
Cushing, Keller, UlmanJudy Cushing
CPSC-310 Database Systems
CPSC-310 Database Systems
Lecture 09: Functional Dependencies, Database Design
Functional Dependencies
Chapter 19 (part 1) Functional Dependencies
The Relational Data Model
Functional Dependencies
CS 405G Introduction to Database Systems
CS 505: Intermediate Topics to Database Systems
Lecture 08: E/R Diagrams and Functional Dependencies
CS 405G: Introduction to Database Systems
Presentation transcript:

Santa Clara UniversityHolliday – COEN 1783–1 Today’s Topic Today: u Relational Model, Functional Dependencies. u Read Sections Next time u Normal Forms. u Read Sections 3.6. Note: we are skipping section 3.7

Santa Clara UniversityHolliday – COEN 1783–2 Relational Model Table = relation. Column headers = attributes. Row = tuple Depositor Relation schema = name(attributes) + other structure info., e.g., keys, other constraints. Ex: Depositor(name, acc#) u Order of attributes is arbitrary, but in practice we need to assume the order given in the relation schema. Relation instance is current set of rows for a relation schema. Database schema = collection of relation schemas. Customername account# Tom 101 Bill 524 …

Santa Clara UniversityHolliday – COEN 1783–3 A1 A2 A3... An a1 a2 a3 an b1 b2 a3 cn a1 c3 b3 bn. x1 v2 d3 wn Relational Data Model Set theoretic Domain — set of values like a data type Cartesian product (or product) D1  D2 ...  Dn n-tuples (V1,V2,...,Vn) s.t., V1  D1, V2  D2,...,Vn  Dn Relation-subset of cartesian product of one or more domains FINITE only; empty set allowed Tuples = members of a relation inst. Arity = number of domains Components = values in a tuple Domains — corresp. with attributes Cardinality = number of tuples Relation as table Rows = tuples Columns = components Names of columns = attributes Set of attribute names = schema REL (A1,A2,...,An) Arity CardinalityCardinality Attributes Component Tuple

Santa Clara UniversityHolliday – COEN 1783–4 Relation Instance NameAddressTelephone Bob123 Main St Ted128 First St Pat128 First St Harry456 Main St Sally456 Main St Nancy52 Third St Alice12 State St

Santa Clara UniversityHolliday – COEN 1783–5 About Relational Model Order of tuples not important Order of attributes not important (in theory) Collection of relation schemas = Relational database schema Corresponding relation instances = Relational database Schema is part of “metadata” and doesn’t have actual data

Santa Clara UniversityHolliday – COEN 1783–6 Why Relations? Very simple model. Often a good match for the way we think about our data. Abstract model that underlies SQL, the most important language in DBMS’s today. u But SQL uses “bags” while the abstract relational model is set-oriented. (remember set rules about order and duplication)

Santa Clara UniversityHolliday – COEN 1783–7 Relational Design Simplest approach (not always best): convert each E.S. to a relation and each relationship set to a relation. Entity Set  Relation E.S. attributes become relational attributes. Becomes: Beers(name, manf) Beers namemanf

Santa Clara UniversityHolliday – COEN 1783–8 Keys in Relations An attribute or set of attributes K is a key for a relation R if we expect that in no instance of R will two different tuples agree on all the attributes of K. Indicate a key by underlining the key attributes. Example: If name is a key for Beers: Beers(name, manf)

Santa Clara UniversityHolliday – COEN 1783–9 E/R Relationships  Relations Relation has an attribute for the key attributes of each E.S. that participates in the relationship. Add any attributes that belong to the relationship itself. Renaming attributes OK. u Essential if multiple roles for an E.S.

Santa Clara UniversityHolliday – COEN 1783–10 Drinkers For one-one relation Married, we can choose either husband or wife as key. Likes(drinkerName, beerName) Favorite(drinkerName, beerName) Married(husband, wife) Buddies(name1, name2) Beers Likes namemanfnameaddr Buddies Married Favorite 1 2 husbandwife

Santa Clara UniversityHolliday – COEN 1783–11 Combining Relations Sometimes it makes sense to combine relations. Common case: Relation for an E.S. E plus the relation for some many-one relationship from E to another E.S. Example Combine Drinker(name, addr) with Favorite(drinkerN, beerN) to get Drinker1(name, addr, favBeer). Danger in pushing this idea too far: redundancy. e.g., combining Drinker with Likes causes the drinker's address to be repeated, viz.: nameaddrbeer Sally123 MapleBud Sally123 MapleMiller Notice the difference: Favorite is many-one; Likes is many-many.

Santa Clara UniversityHolliday – COEN 1783–12 Weak Entity Sets, Relationships  Relations Relation for a weak E.S. must include its full key (i.e., attributes of related entity sets) as well as its own attributes. A supporting (double-diamond) relationship yields a relation that is actually redundant and should be deleted from the database schema.

Santa Clara UniversityHolliday – COEN 1783–13 Example Hosts(hostName) Logins(loginName, hostName) At(loginName, hostName, hostName2) In At, hostName and hostName2 must be the same host, so delete one of the attributes. Then, Logins and At become the same relation; delete one of the tables. In this case, Hosts ’ schema is a subset of Logins ’ schema. Delete Hosts name

Santa Clara UniversityHolliday – COEN 1783–14 Subclasses  Relations Three approaches: 1. Object-oriented: each entity is in one class. Create a relation for each class, with all the attributes for that class. u Don’t forget inherited attributes. 2. E/R style: an entity is in a network of classes related by isa. Create one relation for each E.S. u An entity is represented in the relation for each subclass to which it belongs. u Relation has only the attributes attached to that E.S. + key. 3. Use nulls. Create one relation for the root class or root E.S., with all attributes found anywhere in its network of subclasses.  Put NULL in attributes not relevant to a given entity.

Santa Clara UniversityHolliday – COEN 1783–15 Example: Subclasses namemanf Beers Ales color isa

Santa Clara UniversityHolliday – COEN 1783–16 OO-Style E/R Style BeersAles BeersAles Beers Using NULLS

Santa Clara UniversityHolliday – COEN 1783–17 Subclasses: Special Case If the subclasses are disjoint and the isa relationship is complete (every entity is in one of the subclasses), make tables for the subclasses only. name Acc# Accounts Savings a/c rate isa Checking a/c

Santa Clara UniversityHolliday – COEN 1783–18 Why Study Functional Dependencies? Functional dependencies help us to quantify the concept of a "good" database schema. A functional dependency is a generalization of the notion of a key. A functional dependency is a result of the semantics or meaning of the attributes. A functional dependency is a property of the relation schema, not of a particular relation instance.

Santa Clara UniversityHolliday – COEN 1783–19 Functional Dependencies In the schema R=(X, A, B, C) X  A (pronounced X determines A) is an assertion about a relation R that whenever two tuples agree on all the attributes of X, then they must also agree on attribute A.

Santa Clara UniversityHolliday – COEN 1783–20 Example Lending(name, addr, loan#, amount, branchName, branchCity) Reasonable FD's to assert: 1. name  addr 2. branchName  branchCity 3. loan#  name

Santa Clara UniversityHolliday – COEN 1783–21 More on Functional Dependencies Sometimes, several attributes jointly determine another attribute, although neither does by itself. Cust (FirstName, LastName, SSN, addr, etc) SSN  addr FirstName LastName  addr

Santa Clara UniversityHolliday – COEN 1783–22 Manipulating FD’s Combine FD's with common left side by concatenating their right sides. Cust (FirstName, LastName, SSN, addr, etc) SSN  addr SSN  LastName SSN  LastName addr Order within an attribute set does not matter LastName addr is the same as addr LastName

Santa Clara UniversityHolliday – COEN 1783–23 Armstrong’s Axioms Reflexivity: If  is a set of attributes and  is a subset of , then     SSN addr  addr Augmentation: If    and  is a set of attributes, then       SSN  addr means that SSN etc  addr etc Transitivity: If    and   , then     Loan#  Bname and Bname  Bcity means that Loan#  Bcity

Santa Clara UniversityHolliday – COEN 1783–24 Functional Dependency Rules A  A, AB  A if A  B then AC  BC if A  B and B  C then A  C if A  B and A  C then A  BC if A  BC then A  B and A  C if A  B then AC  B

Santa Clara UniversityHolliday – COEN 1783–25 Questions Given the schema: Cust (FirstName, LastName, SSN, addr, etc) If SSN  LastName and LastName  addr can we conclude SSN  addr? If FirstName, LastName  addr can we concludeFirstName  addr If LastName  addr can we conclude FirstName, LastName  addr

Santa Clara UniversityHolliday – COEN 1783–26 Keys of Relations K is a (candidate) key for relation R if: 1. K  all attributes of R. (Uniqueness) 2. For no proper subset of K is (1) true. (Minimality) If K at least satisfies (1), then K is a superkey. Conventions Pick one key; underline key attributes in the relation schema. X, etc., represent sets of attributes; A etc., represent single attributes. No set notation in FD’s, e.g., ABC instead of {A, B, C}.

Santa Clara UniversityHolliday – COEN 1783–27 Example Drinkers(name, addr, beersLiked, manf, favoriteBeer) { name, beersLiked } FD’s all attributes (because key).  Shows { name, beersLiked } is a superkey. name  beersLiked is false, so name not a superkey. beersLiked  name also false, so beersLiked not a superkey. Thus, { name, beersLiked } is a (candidate) key. No other keys in this example.  Neither name nor beersLiked is on the right of any observed FD, so they must be part of any superkey.

Santa Clara UniversityHolliday – COEN 1783–28 Example 2 Keys are { Lastname, Firstname } and { StudentID } Lastname Firstname Student ID Major Key (2 attributes) Superkey Note: There are alternate keys

Santa Clara UniversityHolliday – COEN 1783–29 Who Determines Keys/FD’s? We could assert a key K.  Then the only FD’s asserted are that K  A for every attribute A. u No surprise: K is then the only key for those FD’s, according to the formal definition of “key.” Or, we could assert some FD’s and deduce one or more keys by the formal definition. u E/R diagram implies FD’s by key declarations and many-one relationship declarations. Rule of thumb: FD’s either come from keyness, many-1 relationship, or from physics.  E.g., “no two courses can meet in the same room at the same time” yields room time  course. Not always obvious: does LastName determine StudentID?

Santa Clara UniversityHolliday – COEN 1783–30 FD’s and Many-One Relationships Consider R(A1,…, An) and X is a key then X  Y for any attributes Y in A1,…, An even if they overlap with X. Why? Suppose R is used to represent a many  one relationship: E1 entity set  E2 entity set and X key for E1, and Y key for E2, Then, X  Y holds, And Y  X does not hold unless the relationship is one-one. What about many-many relationships? DeptEmp WorksIn CourseStudent Takes

Santa Clara UniversityHolliday – COEN 1783–31 Inferring FD’s And this is important because … When we talk about improving relational designs, we often need to ask “does this FD hold in this relation?” Given FD’s X1  A1, X2  A2,…, Xn  An, does FD Y  B necessarily hold in the same relation? Start by assuming two tuples agree in Y. Use given FD’s to infer other attributes on which they must agree. If B is among them, then yes, else no.

Santa Clara UniversityHolliday – COEN 1783–32 Closure of an Attribute Set Define Y + = closure of Y = set of attributes functionally determined by Y: Basis: Y + :=Y. Induction: If X  Y +, and X  A is a given FD, then add A to Y +. End when Y + cannot be changed.

Santa Clara UniversityHolliday – COEN 1783–33 Example Given the FD’s: A  B, BC  D. A + = AB. C + =C. (AC) + = ABCD.

Santa Clara UniversityHolliday – COEN 1783–34 Given Versus Implied FD’s Typically, we state a few FD’s that are known to hold for a relation R. Other FD’s may follow logically from the given FD’s; these are implied FD’s. Sometimes a set of FD’s can be stated in more than one way.

Santa Clara UniversityHolliday – COEN 1783–35 Schema Decomposition One way of improving DB design is decomposition. Lending(name, addr, loan#, amount, branchName, branchCity) Less redundancy and error prone if decomposed to Customer(name, addr), Loan( name, loan#, amount, branchName), Branch(branchName, branchCity)

Santa Clara UniversityHolliday – COEN 1783–36 Finding All Implied FD’s Motivation: Suppose we have a relation ABCD with some FD’s F. If we decide to decompose ABCD into ABC and AD, what are the FD’s for ABC, AD? Example: F = AB  C, C  D, D  A. It looks like just AB  C holds in ABC, but in fact C  A follows from F and applies to relation ABC. Problem is exponential in worst case.

Santa Clara UniversityHolliday – COEN 1783–37 Algorithm For each set of attributes X compute X +. u But skip X = , X = all attributes. u Add X  A for each A in X + –X. Drop XY  A if X  A holds. u Consequence: If X + is all attributes, then there is no point in computing closure of supersets of X. Finally, project the FD’s by selecting only those FD’s that involve only the attributes of the projection. u Notice that after we project the discovered FD’s onto some relation, the eliminated FD’s can be inferred in the projected relation.

Santa Clara UniversityHolliday – COEN 1783–38 Example F = AB  C, C  D, D  A. What FD’s follow? A + = A; B + =B (nothing). C + =ACD (add C  A). D + =AD (nothing new). (AB) + =ABCD (add AB  D; skip all supersets of AB). (BC) + =ABCD (nothing new; skip all supersets of BC). (BD) + =ABCD (add BD  C; skip all supersets of BD). (AC) + =ACD; (AD) + =AD; (CD) + =ACD (nothing new). (ACD) + =ACD (nothing new). All other sets contain AB, BC, or BD, so skip. Thus, the only interesting FD’s that follow from F are: C  A, AB  D, BD  C.

Santa Clara UniversityHolliday – COEN 1783–39 Example 2 Set of FD’s in ABCGHI: A  B A  C CG  H CG  I B  H Compute (CG) +, (BG) +, (AG) +

Santa Clara UniversityHolliday – COEN 1783–40 Example 3 In ABC with FD’s A  B, B  C, project onto AC. 1.A + = ABC; yields A  B, A  C. 2.B + = BC; yields B  C. 3.AB + = ABC; yields AB  C; drop in favor of A  C. 4.AC + = ABC yields AC  B; drop in favor of A  B. 5.C + = C and BC + = BC; adds nothing. Resulting FD’s: A  B, A  C, B  C. Projection onto AC: A  C.