Download presentation
Presentation is loading. Please wait.
Published byAlbert Cunningham Modified over 8 years ago
1
1 Lecture 08: E/R Diagrams and Functional Dependencies Friday, January 21, 2005
2
2 Outline Finish E/R diagrams (Chapter 2) –And E/R diagrams to relations (3.2, 3.3) The relational data model: 3.1 Functional dependencies: 3.4
3
3 Constraints in E/R Diagrams Finding constraints is part of the modeling process. Commonly used constraints: Keys: social security number uniquely identifies a person. Single-value constraints: a person can have only one father. Referential integrity constraints: if you work for a company, it must exist in the database. Other constraints: peoples’ ages are between 0 and 150.
4
4 Keys in E/R Diagrams address namessn Person Product namecategory price No formal way to specify multiple keys in E/R diagrams Underline:
5
5 Single Value Constraints makes v. s.
6
6 Referential Integrity Constraints CompanyProduct makes CompanyProduct makes Each product made by at most one company. Some products made by no company Each product made by exactly one company.
7
7 Other Constraints CompanyProduct makes <100 What does this mean ?
8
8 Weak Entity Sets Entity sets are weak when their key comes from other classes to which they are related. UniversityTeam affiliation numbersportname Notice: we encountered this when converting multiway relationships to binary relationships (last lecture)
9
9 Handling Weak Entity Sets UniversityTeam affiliation numbersportname Convert to a relational schema (in class)
10
10 The Relational Data Model Data Modeling Data Modeling Relational Schema Relational Schema Physical storage Physical storage E/R diagrams Tables: column names: attributes rows: tuples Complex file organization and index structures. Have seen this in SQL Have seen this too Discuss next
11
11 Terminology Name Price Category Manufacturer gizmo $19.99 gadgets GizmoWorks Power gizmo $29.99 gadgets GizmoWorks SingleTouch $149.99 photography Canon MultiTouch $203.99 household Hitachi Tuples or rows or records Attribute names Table name or relation name Products:
12
12 Schemas Relational Schema: –Relation name plus attribute names –E.g. Product(Name, Price, Category, Manufacturer) –In practice we add the domain for each attribute Database Schema –Set of relational schemas –E.g. Product(Name, Price, Category, Manufacturer), Company(Name, Address, Phone),....... This is all mathematics, not to be confused with SQL tables !
13
13 Instances Relational schema = R(A 1,…,A k ): Instance = relation with k attributes (of “type” R) –values of corresponding domains Database schema = R 1 (…), R 2 (…), …, R n (…) Instance = n relations, of types R 1, R 2,..., R n
14
14 Example Name Price Category Manufacturer gizmo $19.99 gadgets GizmoWorks Power gizmo $29.99 gadgets GizmoWorks SingleTouch $149.99 photography Canon MultiTouch $203.99 household Hitachi Relational schema:Product(Name, Price, Category, Manufacturer) Instance:
15
15 First Normal Form (1NF) A database schema is in First Normal Form if all tables are flat NameGPACourses Alice3.8 Bob3.7 Carol3.9 Math DB OS DB OS Math OS Student NameGPA Alice3.8 Bob3.7 Carol3.9 Student Course Math DB OS StudentCourse AliceMath CarolMath AliceDB BobDB AliceOS CarolOS Takes Course May need to add keys
16
16 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
17
17 Functional Dependencies Warning: this is the most abstract, and “hardest” part of the course. This chapter in the book (3.4) is not very good COME TO CLASS, PAY ATTENTION, READ THE LECTURE NOTES !
18
18 Functional Dependencies Definition: If two tuples agree on the attributes then they must also agree on the attributes Formally: A 1, A 2, …, A n B 1, B 2, …, B m A 1, A 2, …, A n B 1, B 2, …, B m
19
19 Typical Examples of FDs Product: name price, manufacturer Person: ssn name, age zip city city, state zip
20
20 Examples EmpID Name, Phone, Position Position Phone but not Phone Position EmpIDNamePhonePosition E0045Smith1234Clerk E1847John9876Salesrep E1111Smith9876Salesrep E9999Mary1234Lawyer Formally, an FD holds, or does not hold on an instance:
21
21 In General To check A B, erase all other columns check if the remaining relation is many-one (called functional in mathematics) Note: this is the mathematical definition of a function. Book is wrong.
22
22 Example EmpIDNamePhonePosition E0045Smith1234Clerk E1847John9876Salesrep E1111Smith9876Salesrep E9999Mary1234Lawyer
23
23 Example FD’s are constraints: On some instances they hold On others they don’t namecategorycolordepartmentprice GizmoGadgetGreenToys49 TweakerGadgetGreenToys99 Does this instance satisfy all the FDs ? name color category department color, category price name color category department color, category price
24
24 Example namecategorycolordepartmentprice GizmoGadgetGreenToys49 TweakerGadgetBlackToys99 GizmoStationaryGreenOffice-supp.59 What about this one ? name color category department color, category price name color category department color, category price
25
25 An Interesting Observation If all these FDs are true: name color category department color, category price name color category department color, category price Then this FD also holds: name, category price Why ??
26
26 Inference Rules for FD’s Is equivalent to Splitting rule and Combing rule A1...AmB1...Bm A 1, A 2, …, A n B 1, B 2, …, B m A 1, A 2, …, A n B 1 A 1, A 2, …, A n B 2..... A 1, A 2, …, A n B m A 1, A 2, …, A n B 1 A 1, A 2, …, A n B 2..... A 1, A 2, …, A n B m
27
27 Inference Rules for FD’s (continued) Trivial Rule Why ? A1A1 …AmAm where i = 1, 2,..., n A 1, A 2, …, A n A i
28
28 Inference Rules for FD’s (continued) Transitive Closure Rule If and then Why ? A 1, A 2, …, A n B 1, B 2, …, B m B 1, B 2, …, B m C 1, C 2, …, C p A 1, A 2, …, A n C 1, C 2, …, C p
29
29 A1A1 …AmAm B1B1 …BmBm C1C1...CpCp
30
30 Example (continued) Start from the following FDs: Infer the following FDs: 1. name color 2. category department 3. color, category price 1. name color 2. category department 3. color, category price 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
31
31 Example (continued) 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 Trivial rule 7. name, category color, category Split/combine on 5, 6 8. name, category price Transitivity on 3, 7 1. name color 2. category department 3. color, category price 1. name color 2. category department 3. color, category price THIS IS TOO HARD ! Let’s see an easier way.
32
32 Closure of a set of Attributes Given a set of attributes A 1, …, A n The closure, {A 1, …, A n } +, is the set of attributes B s.t. A 1, …, A n B Given a set of attributes A 1, …, A n The closure, {A 1, …, A n } +, is the set of attributes B s.t. A 1, …, A n B name color category department color, category price name color category department color, category price Example: Closures: name + = {name, color} {name, category} + = {name, category, color, department, price} color + = {color}
33
33 Closure Algorithm Start with X={A1, …, An}. Repeat until X doesn’t change do: if B 1, …, B n C is a FD and B 1, …, B n are all in X then add C to X. Start with X={A1, …, An}. Repeat until X doesn’t change do: if B 1, …, B n C is a FD and B 1, …, B n are all in X then add C to X. {name, category} + = { } name color category department color, category price name color category department color, category price Example: name, category, color, department, price Hence: name, category color, department, price
34
34 Another Example Enrollment(student, major, course, room, time) student major major, course room course time What else can we infer ? [in class, or at home]
35
35 Example Compute {A,B} + X = {A, B, } Compute {A, F} + X = {A, F, } R(A,B,C,D,E,F) A, B C A, D E B D A, F B A, B C A, D E B D A, F B In class:
36
36 Using Closure to Infer ALL FDs A, B C A, D B B D A, B C A, D B B D Example: 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 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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.