Download presentation
Presentation is loading. Please wait.
Published byСергей Самарин Modified over 5 years ago
1
Lecture 08: E/R Diagrams and Functional Dependencies
Friday, October 15, 2004
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
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
Keys in E/R Diagrams name category Underline: price Product
No formal way to specify multiple keys in E/R diagrams Person name ssn address
5
Single Value Constraints
makes v. s. makes
6
Referential Integrity Constraints
makes Product Company Each product made by at most one company. Some products made by no company makes Product Company Each product made by exactly one company.
7
Other Constraints makes <100 Product Company What does this mean ?
8
Weak Entity Sets Entity sets are weak when their key comes from other
classes to which they are related. affiliation Team University sport number name Notice: we encountered this when converting multiway relationships to binary relationships (last lecture)
9
Handling Weak Entity Sets
affiliation Team University sport number name Convert to a relational schema (in class)
10
The Relational Data Model
Schema Physical storage Data Modeling Have seen this in SQL Complex file organization and index structures. E/R diagrams Tables: column names: attributes rows: tuples Have seen this too Discuss next
11
Terminology Table name or relation name Attribute names Products:
Name Price Category Manufacturer gizmo $ gadgets GizmoWorks Power gizmo $ gadgets GizmoWorks SingleTouch $ photography Canon MultiTouch $ household Hitachi Tuples or rows or records
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
Instances Relational schema = R(A1,…,Ak): Instance = relation with k attributes (of “type” R) values of corresponding domains Database schema = R1(…), R2(…), …, Rn(…) Instance = n relations, of types R1, R2, ..., Rn
14
Example Relational schema:Product(Name, Price, Category, Manufacturer)
Instance: Name Price Category Manufacturer gizmo $ gadgets GizmoWorks Power gizmo $ gadgets GizmoWorks SingleTouch $ photography Canon MultiTouch $ household Hitachi
15
First Normal Form (1NF) A database schema is in First Normal Form if all tables are flat Student Student Name GPA Alice 3.8 Bob 3.7 Carol 3.9 Name GPA Courses Alice 3.8 Bob 3.7 Carol 3.9 Math DB OS Takes Course Student Course Alice Math Carol DB Bob OS Course Math DB OS DB OS May need to add keys Math OS
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
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
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
19
Typical Examples of FDs
Product: name price, manufacturer Person: ssn name, age zip city city, state zip
20
Examples EmpID Name Phone Position E0045 Smith 1234 Clerk E1847 John
Formally, an FD holds, or does not hold on an instance: EmpID Name Phone Position E0045 Smith 1234 Clerk E1847 John 9876 Salesrep E1111 E9999 Mary Lawyer EmpID Name, Phone, Position Position Phone but not Phone Position
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
Example EmpID Name Phone Position E0045 Smith 1234 Clerk E1847 John
9876 Salesrep E1111 Smith 9876 Salesrep E9999 Mary 1234 Lawyer
23
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 ?
24
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 ?
25
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 ??
26
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
27
Inference Rules for FD’s (continued)
Trivial Rule A1, A2, …, An Ai where i = 1, 2, ..., n A1 … Am Why ?
28
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 ?
29
A1 … Am B1 Bm C1 ... Cp
30
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
31
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.
32
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}
33
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
34
Another Example Enrollment(student, major, course, room, time)
course time What else can we infer ? [in class, or at home]
35
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, }
36
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 XY = : AB CD, ADBC, ABC D, ABD C, ACD B
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.