Database Design and Normalization Chapter 10 (Week 11) EE562 Slides and Modified Slides from Database Management Systems, R. Ramakrishnan 1
Computing Closure F + Example: List all FDs with: - a single attribute on left - two attributes on left - three attributes on left A B C D a1 b1 c1 d1 a1 b1 c2 d2 a2 b1 c1 d3 a2 b1 c3 d4 EE562 Slides and Modified Slides from Database Management Systems, R. Ramakrishnan 2
Computing Closure F + Example: Let F = { A B, B C, C D, D E, E F, F G, G H} How many Fds in F + and how to find them? EE562 Slides and Modified Slides from Database Management Systems, R. Ramakrishnan 3
Reasoning About FDs (Contd.) Computing the closure of a set of FDs can be expensive. (Size of closure is exponential in # attrs!) Typically, we just want to check if a given FD X Y is in the closure of a set of FDs F. An efficient check: Compute attribute closure of X (denoted X + ) wrt F: Set of all attributes A such that X A is in F + There is a linear time algorithm to compute this. Check if Y is in X + Does F = {A B, B C, C D E } imply A E? i.e, is A E in the closure F +? Equivalently, is E in A+? EE562 Slides and Modified Slides from Database Management Systems, R. Ramakrishnan 4
Computing Closure (see algorithm in section 10.5) Algorithm rule: If X YZ and Z W then X YZW Example: A B, A GC GD F A ABGC Example: A B, A GC, GD FGB E, BC D Compute A+ A ACDFBGE EE562 Slides and Modified Slides from Database Management Systems, R. Ramakrishnan 5
Minimal Cover for a Set of FDs Cover G is a set of FDs covered by another set of FDs F of G can be derived from F, that is, F+ G Minimal cover G for a set of FDs F: Closure of F = closure of G. Right hand side of each FD in G is a single attribute. If we modify G by deleting an FD or by deleting attributes from an FD in G, the closure changes. Intuitively, every FD in G is needed, and ``as small as possible in order to get the same closure as F. e.g., A B, ABCD E, EF GH, ACDF EG has the following minimal cover: A B, ACD E, EF G and EF H EE562 Slides and Modified Slides from Database Management Systems, R. Ramakrishnan 6
Computing Minimal Cover Step 1 From set F create an equivalent set H of FDs with only single attributes on the R.H.S. Step 2 From H, successively remove individual FDs that are inessential in H. In other words, after removal, the new set remains equivalent to H. Step 3 Now successively replace individual FDs with FDs that have a smaller number of attributes on the LHS, in a manner that does not change H+. (You may need to repeat Step 2) Step 4 From the remaining set of FDs, gather all FDs with equal left-hand sides and use the union rule to create an equivalent FDs where all left-hand sides are unique. (You may not get singleton on RHS) EE562 Slides and Modified Slides from Database Management Systems, R. Ramakrishnan 7
Computing Minimal Cover Exmaple: ABD AC, C BE, AD BF, B E Step 1 H = {ABD A, ABD C, C B, C E, AD B,AD F, B E} Step 2 - ABD A is not essential. - How about ABD C? It cannot be derived from other FDs using set closure rule. - How about C B? Can it be implied by other FDs? Compute C+={CE}. Since, C* does not contain B, C B is essential. - C E is inessential - Compute (AD)+=(ADF), so AD B is essential. How about AD F? No FD has F on left hand side. So this is essential. - Also, B E is essential. H = {ABD C, C B, C E, AD B, AD F, B E} EE562 Slides and Modified Slides from Database Management Systems, R. Ramakrishnan 8
Computing Minimal Cover Step 3 Can we drop A in ABD C? The new set J={BD C, C B, C E, AD B, AD F, B E} Is J=H? Yes iff (BD)+ under H = BD+ under J. BDE is not equal BDC. So we cannot drop A. Proceed on this line and repeat Step 2. H={AD C, C B, AD F, B E} Step 4 H={AD CF, C B, B E} EE562 Slides and Modified Slides from Database Management Systems, R. Ramakrishnan 9
Computing Minimal Cover Example: F={AB C, C A, BC D, ACD B, D G, BE C, CG BD, CE AG } Minimal covers: H={AB C, C A, BC D, CD B, D E, D G, BE C, CG D, CE G } H={AB C, C A, BC D, D E, D G, BE C, CG B, CE G } EE562 Slides and Modified Slides from Database Management Systems, R. Ramakrishnan 10
Normal Forms Returning to the issue of schema refinement, the first question to ask is whether any refinement is needed! If a relation is in a certain normal form (BCNF, 3NF etc.), it is known that certain kinds of problems are avoided/minimized. This can be used to help us decide whether decomposing the relation will help. Role of FDs in detecting redundancy: Consider a relation R with 3 attributes, ABC. No FDs hold: There is no redundancy here. Given A B: Several tuples could have the same A value, and if so, they ll all have the same B value! EE562 Slides and Modified Slides from Database Management Systems, R. Ramakrishnan 11
Normalization (Chapter 11) Universe of relations(normalized and unnormalized) 1NF relations 2NF relations 3NF relations BCNF relations 4NF relations PJ/NF (5NF) relation EE562 Slides and Modified Slides from Database Management Systems, R. Ramakrishnan 12
Moving Towards 3N Form Objective 3NF R is in 3NF iff its non-key attributes (if any) are mutually independent i.e. no FD s among them. Full dependence on the P. Key EE562 Slides and Modified Slides from Database Management Systems, R. Ramakrishnan 13
1NF FIRST S# Status City P# Qty S1 20 London P1 300 S1 20 London P2 200 S1 20 London P3 400 S1 20 London P4 200 S1 20 London P5 100 S1 20 London P6 100 S2 10 Paris P1 300 S2 10 Paris P2 400 S3 10 Paris P2 200 S4 20 London P2 200 S4 20 London P4 300 S4 20 London P5 400 Sample tabulation of FIRST EE562 Slides and Modified Slides from Database Management Systems, R. Ramakrishnan 14
1NF A relation R is in 1NF iff all underlying simple domains contain atomic values QTY S# P# Status City EE562 Slides and Modified Slides from Database Management Systems, R. Ramakrishnan 15
Problems with 1NF Example Insert: We cannot insert a supplier. Why? Delete: Problem? Update: Problem? EE562 Slides and Modified Slides from Database Management Systems, R. Ramakrishnan 16
2N Form Convert 1NF 2NF (using Projection) R is in 1NF. Must has a composite P.K. Project into two relations R1 and R2 Def: R is in 2NF if it is 1NF and every non-key attribute is fully dependent on the P.K. EE562 Slides and Modified Slides from Database Management Systems, R. Ramakrishnan 17
Procedure 1NF 2NF If R(A,B,C,D) P.Key (A,B) with A D Then generate R2 and R2 (lossless) R1 (A,D) R2 (A,B,C) P.Key = A P.Key = (A,B) F.Key = (A) reference R1 EE562 Slides and Modified Slides from Database Management Systems, R. Ramakrishnan 18