Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 07: E/R Diagrams and Functional Dependencies

Similar presentations


Presentation on theme: "Lecture 07: E/R Diagrams and Functional Dependencies"— Presentation transcript:

1 Lecture 07: E/R Diagrams and Functional Dependencies
Wednesday, October 12, 2005

2 Outline Finish E/R diagrams (Chapter 2) The relational data model: 3.1
And E/R diagrams to relations (3.2, 3.3) The relational data model: 3.1 Functional dependencies: 3.4

3 Relational Schema Design
Person buys Product name price ssn Conceptual Model: Relational Model: plus FD’s Normalization: Eliminates anomalies

4 Data Anomalies When a database is poorly designed we get anomalies:
Redundancy: data is repeated Updated anomalies: need to change in several places Delete anomalies: may lose data when we don’t want

5 Relational Schema Design
Recall set attributes (persons with several phones): Name SSN PhoneNumber City Fred Seattle Joe Westfield One person may have multiple phones, but lives in only one city Anomalies: Redundancy = repeat data Update anomalies = Fred moves to “Bellevue” Deletion anomalies = Joe deletes his phone number: what is his city ?

6 Relation Decomposition
Break the relation into two: Name SSN PhoneNumber City Fred Seattle Joe Westfield Name SSN City Fred Seattle Joe Westfield SSN PhoneNumber Anomalies have gone: No more repeated data Easy to move Fred to “Bellevue” (how ?) Easy to delete all Joe’s phone number (how ?)

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

8 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

9 Functional Dependencies
Definition: If two tuples agree on the attributes A1, A2, …, An then they must also agree on the attributes B1, B2, …, Bm Formally: A1, A2, …, An  B1, B2, …, Bm

10 When Does an FD Hold Definition: A1, ..., Am  B1, ..., Bn holds in R if: t, t’  R, (t.A1=t’.A1  ...  t.Am=t’.Am  t.B1=t’.B1  ...  t.Bn=t’.Bn ) R A1 ... Am B1 Bm t if t, t’ agree here then t, t’ agree here t’

11 Examples EmpID Name Phone Position E0045 Smith 1234 Clerk E3542 Mike
An FD holds, or does not hold on an instance: EmpID Name Phone Position E0045 Smith 1234 Clerk E3542 Mike 9876 Salesrep E1111 E9999 Mary Lawyer EmpID  Name, Phone, Position Position  Phone but not Phone  Position

12 Example EmpID Name Phone Position E0045 Smith 1234 Clerk E3542 Mike
Salesrep E1111 E9999 Mary Lawyer Position  Phone

13 Example EmpID Name Phone Position E0045 Smith 1234  Clerk E3542 Mike
Clerk E3542 Mike 9876 Salesrep E1111 E9999 Mary Lawyer but not Phone  Position

14 Typical Examples of FDs
Product: name  price, manufacturer Person: ssn  name, age zip  city city, state  zip

15 Example FD’s are constraints: On some instances they hold name  color
On others they don’t name  color category  department color, category  price name category color department price Gizmo Gadget Green Toys 49 Tweaker 99 No: color,category  price doesn’t hold Does this instance satisfy all the FDs ?

16 Example name  color category  department color, category  price
Gizmo Gadget Green Toys 49 Tweaker Black 99 Stationary Office-supp. 59 yes What about this one ?

17 An Interesting Observation
name  color category  department color, category  price If all these FDs are true: Then this FD also holds: name, category  price Why ??

18 Goal: Find ALL Functional Dependencies
Anomalies occur when certain “bad” FDs hold We know some of the FDs Need to find all FDs, then look for the bad ones

19 Inference Rules for FD’s
A1, A2, …, An  B1, B2, …, Bm Splitting rule and Combing rule Is equivalent to A1 ... Am B1 Bm A1, A2, …, An  B1 A1, A2, …, An  B2 A1, A2, …, An  Bm

20 Inference Rules for FD’s (continued)
Trivial Rule A1, A2, …, An  Ai where i = 1, 2, ..., n A1 Am Why ?

21 Inference Rules for FD’s (continued)
Transitive Closure Rule If A1, A2, …, An  B1, B2, …, Bm and B1, B2, …, Bm  C1, C2, …, Cp then A1, A2, …, An  C1, C2, …, Cp Why ?

22 A1 Am B1 Bm C1 ... Cp

23 Example (continued) Which Rule did we apply ? Inferred FD
1. name  color 2. category  department 3. color, category  price Start from the following FDs: Infer the following FDs: Inferred FD Which Rule did we apply ? 4. name, category  name 5. name, category  color 6. name, category  category 7. name, category  color, category 8. name, category  price

24 Example (continued) 1. name  color 2. category  department
3. color, category  price Answers: Inferred FD Which Rule did we apply ? 4. name, category  name Trivial rule 5. name, category  color Transitivity on 4, 1 6. name, category  category 7. name, category  color, category Split/combine on 5, 6 8. name, category  price Transitivity on 3, 7 THIS IS TOO HARD ! Let’s see an easier way.

25 Inference Rules for FDs
The three simple rules are all we need to derive all possible FDs Called “Armstrong Rules” However, they are clumsy to use in practice Better: use “closure” of a set of attributes (next)

26 Closure of a set of Attributes
Given a set of attributes A1, …, An The closure, {A1, …, An}+ , is the set of attributes B s.t. A1, …, An  B Example: name  color category  department color, category  price Closures: name+ = {name, color} {name, category}+ = {name, category, color, department, price} color+ = {color}

27 Closure Algorithm Start with X={A1, …, An}.
Repeat until X doesn’t change do: if B1, …, Bn  C is a FD and B1, …, Bn are all in X then add C to X. Example: name  color category  department color, category  price {name, category}+ = { } name, category, color, department, price Hence: name, category  color, department, price

28 Example In class: A, B  C R(A,B,C,D,E,F) A, D  E B  D A, F  B
Compute {A,B}+ X = {A, B, } Compute {A, F}+ X = {A, F, }

29 Why Do We Need Closure With closure we can find all FD’s easily
To check if X ® A Compute X+ Check if A Î X+

30 Using Closure to Infer ALL FDs
Example: A, B  C A, D  B B  D Step 1: Compute X+, for every X: A+ = A, B+ = BD, C+ = C, D+ = D AB+ = ABCD, AC+ = AC, AD+ = ABCD ABC+ = ABD+ = ACD+ = ABCD (no need to compute– why ?) BCD+ = BCD, ABCD+ = ABCD Step 2: Enumerate all FD’s X  Y, s.t. Y  X+ and XY = : AB  CD, ADBC, ABC  D, ABD  C, ACD  B

31 Another Example Enrollment(student, major, course, room, time)
course  time What else can we infer ? [in class, or at home]

32 Back to Conceptual Design
Now we know how to find more FDs, it’s easy Search for “bad” FDs If there are such, then decompose the table into two tables, repeat for the subtables. When done, the database schema is normalized Unfortunately, there are several normal forms…

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


Download ppt "Lecture 07: E/R Diagrams and Functional Dependencies"

Similar presentations


Ads by Google