Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC 453 Database Systems Lecture

Similar presentations


Presentation on theme: "CSC 453 Database Systems Lecture"— Presentation transcript:

1 CSC 453 Database Systems Lecture
Tanu Malik College of CDM DePaul University

2 Today Normalization Functional Dependencies Review Closure
Minimal Cover Equivalence

3 Functional Dependencies

4 ssn name projectID rating hourly_wages hours_worked Alice 48 8 10 40 Bob 22 30 Charlie 35 5 7 Don 32 Ellie hourly_wages attribute is determined by the rating attribute. Integrity Constraint: for a given rating value, there is only one permissible hourly_wages value.

5 Create Table statement
CREATE TABLE EmployeeRec ( ssn number(10) primary key, name varchar2(100), projectid number(2), rating number(1), hourly_wages number(3), hours_worked float, unique (rating,hourly_wages) );

6 UPDATE EmployeeRec SET hourly_wages = 11 WHERE name = ‘Alice’

7 INSERT INTO EmployeeRec Values (NULL,NULL,NULL,6,7,NULL,NULL);

8 DELETE FROM EmployeeRec WHERE Rating = 5

9 Better way? Decomposition ssn name ProjectID rating hours_worked
Alice 48 8 40 Bob 22 30 Charlie 35 5 Don 32 Ellie rating hourly_wages 8 10 5 7

10 Anomalies Update Anomaly: The hourly_wages in the first tuple could be updated without making a similar change in the second tuple. Insertion Anomaly: Cannot insert a tuple for the hourly_wages unless have an employee with that wage. Deletion Anomaly: If we delete all tuples with a given rating value (e.g., we delete the tuples for Charlie and Don) we lose the association between that rating value and its hourly_wage value.

11 Functional Dependencies
Set of attributes Y is functionally dependent on set of attributes X if and only if the values of X uniquely determine the values of Y Also “X determines Y”, “X  Y” X is called the determinant A functional dependency must hold for all possible relation states to be valid

12 AddressID City State ZipCode
1 Chicago IL 60604 2 Portland ME 04104 3 60611 4 Boston MA 02468 Move lower?

13 It appears that City  State
AddressID City State ZipCode 1 Chicago IL 60604 2 Portland ME 04104 3 60611 4 Boston MA 02468

14 False Function Dependency
New data may violate this “FD” Need other ways to validate AddressID City State ZipCode 1 Chicago IL 60604 2 Portland ME 04104 3 60611 4 Boston MA 02468 5 OR 97232

15 Where do FDs Come From? Given by domain experts
Deduced by the database designer N:1 relationships Stock Name  Stock Return/Risk Level Software Package  Software Maker Price  Extended warranty cost Laws of Physics X, Y, Z  Location in 3D space

16 BC B D A B C D 1 2 XYZ 3 ABC MNO 4

17 Is a functional dependency a function?
A C BC A A B C 1 a x 2 b y 3 4 c Accessing ‘x’ requires a column name and rowed Search notion of a f.d

18 Student Example (StudentID, School, Location, Dorm, SchoolCost)
StudentID  Dorm School, Dorm  SchoolCost

19 Functional Dependency Rules
(StudentID, School, Location, Dorm, SchoolCost) StudentID, Location, Dorm  StudentID, Location, Dorm StudentID, Location, Dorm  Location, Dorm StudentID, Location, Dorm  Location StudentID, Location, Dorm  Dorm

20 Functional Dependency Rules
Reflexivity (⊆ is a subset operator) If Y ⊆ X, X  Y (StudentID, School, Location, Dorm, SchoolCost) StudentID, Location, Dorm  StudentID, Location, Dorm StudentID, Location, Dorm  Location, Dorm StudentID, Location, Dorm  Location StudentID, Location, Dorm  Dorm

21 Functional Dependency Rules
(StudentID, School, Location, Dorm, SchoolCost) StudentID  School (given) Then Dorm, StudentID  Dorm, School Location, StudentID  Location, School SomeAttrSet, StudentID  SomeAttrSet, School

22 Functional Dependency Rules
Augmentation If X  Y then WX  WY (StudentID, School, Location, Dorm, SchoolCost) StudentID  School (given) Dorm, StudentID  Dorm, School Location, StudentID  Location, School SomeAttrSet, StudentID  SomeAttrSet, School

23 Functional Dependency Rules
(StudentID, School, Location, Dorm, SchoolCost) StudentID  School (given) School  Location (given) StudentID  Location (derived)

24 Functional Dependency Rules
Transitivity If X  Y and Y  Z then X  Z (StudentID, School, Location, Dorm, SchoolCost) StudentID  School (given) School  Location (given) StudentID  Location (derived)

25 Inference Rules Reflexivity: If X includes Y, then X  Y
Augmentation: If X  Y, then XZ  YZ Transitivity: If X  Y and Y  Z, then X  Z

26 Functional Dependency Rules
Union If X  Y, X  Z then X  YZ (StudentID, School, Location, Dorm, SchoolCost) StudentID  School (given) StudentID  Location (derived earlier) StudentID  School, Location (union)

27 Inference Rules Reflexivity: If X includes Y, then X  Y
Augmentation: If X  Y, then XZ  YZ Transitivity: If X  Y and Y  Z, then X  Z [Union: If X  Y and X  Z, then X  YZ] [Decomposition: If X  YZ, then X  Y and X  Z ] [Pseudo-transitivity: If X  Y and WY  Z, then WX  Z]

28 Union: Derived Union If X  Y, X  Z then X  Y, Z X  Y (given above)
X  Z (given above) X  XZ (augment X  Z by X) XZ  YZ (augment X  Y by Z) X  YZ (transitivity rule)

29 Decomposition: Derived
If X  YZ then X  Y and X  Z X  YZ (given above) YZ  Z (reflexivity rule) X  Z (transitive rule) Same with X  Y derivation

30 Functional Dependency Rules
Decomposition If X  YZ then X  Y and X  Z (StudentID, School, Location, Dorm, SchoolCost) StudentID  School, Location (given) StudentID  School (decomposition) StudentID  Location (decomposition)

31 Pseudo-Transitivity: Derived
If X  Y and WY  Z then WX  Z X  Y (given above) WY  Z (given above) WX  WY (augment X  Y with W) WX  Z (transitivity on WX  WY and WY  Z)

32 Functional Dependency Rules
Pseudo-transitivity If X  Y and WY  Z then WX  Z (StudentID, School, Location, Dorm, SchoolCost) StudentID  Dorm (given) School, Dorm  SchoolCost (given) School, StudentID  SchoolCost (pseudo-trans.) 32

33 Inference Rules Reflexivity: If X includes Y, then X  Y
Augmentation: If X  Y, then XZ  YZ Transitivity: If X  Y and Y  Z, then X  Z [Union: If X  Y and X  Z, then X  YZ] [Decomposition: If X  YZ, then X  Y and X  Z ] [Pseudo-transitivity: If X  Y and WY  Z, then WX  Z]

34 Closures For a set of functional dependencies F, the closure of F (F+) is the set of all functional dependencies that can be derived from F F+ can be constructed from the closures under F of all possible sets of attributes X For F and a set of attributes X, the closure of X under F (X+) is the set of all attributes that can be determined from X

35 Finding a Closure of X under F
To find the closure of X under F: set X+ = X repeat set oldX+ = X+ for each Y  Z in F do if X+ includes Y, then set X+ = X+ U Z until oldX+ = X+

36 Attribute Closures: Example 1
Given: R = {A, B, C, D, E, H} F = {A → BC, B → CE, A → E, AC → H, D → B} What is the closure of CD (i.e., (CD)+)?

37 Attribute Closures: Example 1
Given: R = {A, B, C, D, E, H} F = {A → BC, CD → E, CD → H, B → E, D → B} What is the closure of AD (i.e., (AD)+)?

38 SuperKey=Key For each set X, find the closure of X under F
If X+ contains all attributes, then X is a superkey

39 Candidate Key If X is a superkey, but no subset Y of X is a superkey, then X is a candidate key Why candidate key? Because they are minimal.

40 SuperKey Vs Candidate Key
Example F = {A → BCD, AB → CD, ABC → D,BD → AB,C → AD} F.Ds SuperKey Candidate Key A → BCD AB → CD ABC → D BD → AB C → AD

41 SuperKey Vs Candidate Key
Example F = {A → BCD, AB → CD, ABC → D,BD → AB,C → AD} F.Ds SuperKey Candidate Key Primary Key A → BCD AB → CD ABC → D BD → AB C → AD

42 Primary Key Choose a candidate key to be the primary key

43 Types of Keys Super Key: Candidate Key Primary Key
For each set X, find the closure of X under F If X+ contains all attributes, then X is a superkey Candidate Key If X is a superkey, but no subset Y of X is a superkey, then X is a candidate key Primary Key Choose a candidate key to be the primary key

44 Find Candidate Keys R(ABCDEFGH) F = {AB →C, A → DE, B → F, F → GH}
F = {AB →C, BD → EF, AD → G, A → H} R(ABCDE) F = {BC → ACE, D →B} F = {AB → CD, D →A, BC →DE}

45 More examples R(WXYZ) Z → W Y → XZ WX → Y

46 Equivalent Set of F.Ds Given a relation R and two set of functional dependencies F and G on R, the equivalence question is if F ⊆ G, G ⊆ F, F = G, or F ̸= G.

47 Example F={A→B,AB→C,D→AC,D→E} G={A→BC,D→AE}

48 Algorithm Must bring F and G on the same starting point. How?
Compute the closure set of left hand attributes in F, but by using the f.ds in G. Compute the closure set of left hand attributes in G but using the f.ds in F. Compare F’s search power as stated by its f.d set is equivalent to the search power of G.

49 Minimal Cover F is a minimal cover if F no dependency can be removed from F In other words, F is an irreducible funcitonal dependency set

50 Finding Minimal Cover Find a minimal cover by eliminating the redundant/extraneous set of attributes in the set F . Given a f.d α → β, 3 kinds of redundancies: the redundancy is on the r.h.s, the redundancy is on l.h.s, or the entire f.d is redundant.

51 Eliminate redundancy on R.H.S
Use decomposition rule to decompose all f.d’s in F. α → βγ to α → β, and α → γ We have only one attribute on r.h.s, either the entire f.d will be redundant or the f.d will not be redundant.

52 Eliminate entire f.d Eliminate a f.d if you see it exactly repeated again or can be inferred transitively. Given a α → β in F, Find α+ first using F, compute α+ but this time using F − {α → β}. If α+ does not change then surely the f.d is redundant.

53 Eliminate redundancy on l.h.s
Given a f.d such αγ → β, determine if α → γ holds in F′ = F −{αγ → β}. If γ can be searched by just α in F’ without the given f.d, then clearly γ is redundant in the given f.d to search for β.

54 Finding Minimal Cover Minimal/Canonical Cover Algorithm (CCA)
Given FD set, F, CCA finds minimal FD set equivalent to F minimal: can’t find another equivalent FD set with fewer FD’s

55 Minimal Cover Algorithm
ALGORITHM minimal-cover (X: FD Set) BEGIN REPEAT UNTIL STABLE 1. Remove all trivial FDs. 2. Where possible, apply DECOMPOSITION rule (A’s Axioms) 3. Remove all extraneous attributes: a. Test if B extraneous in A → BC (B extraneous if (A → B)  (F – {A → BC} U {A → C})+) b. Test if B extraneous in AB → C (B extraneous if (A → C)  F+) END

56 Extraneous Attributes
1. Extraneous in RHS? e.g.: Can we replace A → BC with A → C? (i.e.: Is B extraneous in A → BC?) 2. Extraneous in LHS? e.g.: Can we replace AB → C with A → C? (i.e.: Is B extraneous in AB → C?) Simple (but expensive) test: 1. Replace A → BC (or AB → C) with A → C in F Define F2 = F – {A → BC}  {A → C} OR F2 = F – {AB → C}  {A → C} 2. Test: Is F2+ = F+? If yes, then B was extraneous

57 Extraneous Attributes
A. RHS: Is B extraneous in A → BC? Step 1: F2 = F – {A → BC}  {A → C} Step 2: F+ = F2+? To simplify step 2, observe that F2+  F+ (i.e.: no new FD’s in F2+) Why? Have effectively removed A → B from F When is F+ = F2+? A: When (A → B)  F2+ Idea: If F2+ includes: A → B and A → C, then it includes A → BC

58 Extraneous Attributes
A. RHS: Given F = {A → BC, B → C}, is C extraneous in A → BC? Why or why not? A: Yes, because (A → C)  {A → B, B → C}+ Proof: 1. A → B Given 2. B → C Given 3. A → C transitivity, (1) and (2) Use Armstrong’s axioms in proof

59 Extraneous Attributes
B. LHS: Is B extraneous in AB → C? Step 1: F2 = F – {AB → C} U {A → C} Step 2: F+ = F2+? To Simplify step 2, observe that F2+  F+ (i.e.: there may be new FD’s in F2+) Why? A → C “implies” AB → C. Thus, all FD’s in F+ also in F2+. But AB → C does not “imply” A → C. Thus, all FD’s in F2+, not necessarily in F+. When is F+ = F2+? A: When (A → C)  F+ Idea: If (A → C) F+ , then it will include all FD’s of F2+

60 Minimal Cover Algorithm
Example: Determine the minimal cover of F = {A → BC, B → CE, A → E} Iteration 1: a. F = {A → B A → C, A → E, B → E, B → C} Must check for up to 5 extraneous attributes Is A → B? extraneous No Is A → C? extraneous Yes: (A → C)  {A → BE, B → CE}+ 1. A → BE Given 2. A → B Decomposition (1) 3. B → CE Given 4. B → C Decomposition (3) 5. A  C Trans (2,4) Is B → E? extraneous …

61 Minimal Cover Algorithm
Example (cont.): F = {A → BC, B → CE, A → E} Iteration 1: Iteration 1: a. F = {A → B A → C, A → E, B → E, B → C} Extraneous atts: B extraneous in A → BCE? No C extraneous in A → BCE? Yes… F = {A → B, A → E, B → E, B → C} Is A → E? extraneous Yes: (A → E)  {A → B, B → E, B → C}+ 1. A → B Given 2. B → CE Given 3. B → E Decomposition (2) 4. A → E Trans (1,3) F = {A → B, B → E, B → C} E extraneous in B → E? No C extraneous in B → C? No

62 Minimal Cover Algorithm
Example (cont.): F = {A → BC, B → CE, A → E} Iteration 1: a. F = {A → BCE, B → CE} b. Extraneous atts: B extraneous in A → BCE? No C extraneous in A → BCE? Yes… E extraneous in A → BE? Yes… E extraneous in B → CE? No C extraneous in B → CE? No Iteration 2: a. F = {A → B, B → CE} b. Extraneous atts: E extraneous in B → CE? No C extraneous in B → CE? No DONE!

63 Minimal Cover of a set of FDs
F = {A → BC, B → CE, A → E, AC → H, D → B} F = {A → BC, B → C, A → B, AB → C, AC → D} A-> B, B -> CE, AC-> H, D->B

64 Minimal Cover Algorithm
Given: Determine canonical cover of F: F = {A → BC, B → CE, A → E, AC → H, D → B} Fc = {A → BH, B → CE, D → B} Another Example: F = {A → BC, B → C, A → B, AB → C, AC → D} cc algorithm Fc = {A → BD, B → C}

65 Minimal Cover Exercise
Relation R (A, B, C, D) Set F ABCD BC AB ABC BD Compute canonical cover


Download ppt "CSC 453 Database Systems Lecture"

Similar presentations


Ads by Google