Download presentation
Presentation is loading. Please wait.
1
H.Lu/HKUST L02: Logical Database Design Introduction Functional Dependencies Normal Forms Normalization by decomposition
2
L02: Database Design -- 2 H.Lu/HKUST Life Cycle of Database Applications Database initial study Database design Implement. & loading Operation Maint. & evolution Analyze the company situation Define problems & constraints Define objectives; scope and boundaries Conceptual design; DBMS selection Logical & physical design Introducing changes; make enhancements Produce the required information flow Install the DBMS; create the DB; load & convert the data Testing & evaluation Test, fine-tune, evaluate the DB and its application programs
3
L02: Database Design -- 3 H.Lu/HKUST Database Design Procedures Data & requirement analysis Conceptual modeling Data model verification Logical design Physical design Determine end user views, outputs, and transaction- processing requirements Make the semantics clear Define storage structures and access paths for optimum performance Translate the conceptual schema into definitions of tables, views, and so on Identifying main processes, insert, update, and delete rules, validate reports, queries, views, integrity, sharing, and security DBMS software selection
4
L02: Database Design -- 4 H.Lu/HKUST Overview of Relational Database Design Requirement Analysis Conceptual design: make the semantics clear What are the entities and relationships in the enterprise? What information about these entities and relationships should we store in the database? What are the integrity constraints or business rules that hold? A database `schema’ in the ER Model can be represented pictorially (ER diagrams). Can map an ER diagram into a relational schema. Logical design: (Normalization) Check relational schema for redundancies and related anomalies. Physical Database Design and Tuning: Consider typical workloads and further refine the database design.
5
L02: Database Design -- 5 H.Lu/HKUST Logical Design of Relational Databases Logical design of relational database requires that we find a “ good ” collection of relation schemas. A bad design may lead to Repetition of information. Inability to represent certain information. Design goals: Avoid redundant data Ensure that relationships among attributes are represented Facilitate the checking of updates for violation of database integrity constraints
6
L02: Database Design -- 6 H.Lu/HKUST The Evils of Redundancy Redundancy is at the root of several problems associated with relational schemas: redundant storage, insert/delete/update anomalies Input of our design phase: An initial schema. Consider relation obtained from Hourly_Emps: Hourly_Emps (eid, name, office, rating, hrly_wages, hrs_worked) Notation: We will denote this relation schema by listing the attributes: ENORWH This is really the set of attributes {E,N,O,R,W,H}. Sometimes, we will refer to all attributes of a relation by using the relation name. (e.g., Hourly_Emps for ENORWH)
7
L02: Database Design -- 7 H.Lu/HKUST Anomalies IC: the hourly_wage is determined by rating. Update anomaly : Can we change W in just the 1st tuple of ENORWH? Insertion anomaly : What if we want to insert an employee and don’t know the hourly wage for his rating? Deletion anomaly : If we delete all employees with rating 5, we lose the information about the wage for rating 5!
8
L02: Database Design -- 8 H.Lu/HKUST Decomposition – The Basic Approach Main refinement technique: decomposition (replacing ENORWH with ENORH and RW) Decomposition should be used judiciously: Is there reason to decompose a relation? What problems (if any) does the decomposition cause? Integrity constraints, in particular functional dependencies, can be used to identify schemas with such problems and to suggest refinements. Hourly_Emps 2 Wages
9
L02: Database Design -- 9 H.Lu/HKUST Database Design Theory Decide whether a particular relation R is in “good” form. In the case that a relation R is not in “good” form, decompose it into a set of relations {R1, R2,..., Rn} such that each relation is in good form the decomposition is a lossless-join decomposition Our theory is based on: functional dependencies multivalued dependencies (not going to cover in this course)
10
H.Lu/HKUST L02: Logical Database Design Introduction Functional Dependencies Normal Forms Normalization by decomposition
11
L02: Database Design -- 11 H.Lu/HKUST Functional Dependencies A functional dependency X Y holds over relation R if, for every allowable instance r of R: t1 r, t2 r, X (t1) = X (t2) implies Y (t1) = Y (t2) i.e., given two tuples in r, if the X values agree, then the Y values must also agree. (X and Y are sets of attributes.) A functional dependency is a generalization of the notion of a key. Constraints on the set of legal relations. Require that the value for a certain set of attributes determines uniquely the value for another set of attributes
12
L02: Database Design -- 12 H.Lu/HKUST Functional Dependencies (Cont.) Example: Consider r(A,B) with the instance of r. On this instance, A B does NOT hold, but B A does hold. An FD is a statement about all allowable relations. Must be identified based on semantics of application. Given some allowable instance r1 of R, we can check if it violates some FD f, but we cannot tell if f holds over R! A functional dependency is trivial if it is satisfied by all instances of a relation E.g. Sid, Sname Sname Sname Sname In general, is trivial if 14 1 5 37
13
L02: Database Design -- 13 H.Lu/HKUST Use of Functional Dependencies We use functional dependencies to: test relations to see if they are legal under a given set of functional dependencies. If a relation r is legal under a set F of functional dependencies, we say that r satisfies F. specify constraints on the set of legal relations We say that F holds on R if all legal relations on R satisfy the set of functional dependencies F. Note: A specific instance of a relation schema may satisfy a functional dependency even if the functional dependency does not hold on all legal instances. For example, a specific instance of Sailors may, by chance, satisfy Sname Sid.
14
L02: Database Design -- 14 H.Lu/HKUST Armstrong ’ s Axioms Given some FDs, we can usually infer additional FDs: ssn did, did lot implies ssn lot An FD f is implied by a set of FDs F if f holds whenever all FDs in F hold. F + = closure of F is the set of all FDs that are implied by F. Armstrong ’ s Axioms (X, Y, Z are sets of attributes): Reflexivity: If X Y, then X Y Augmentation: If X Y, then XZ YZ for any Z Transitivity: If X Y and Y Z, then X Z These are sound and complete inference rules for FDs!
15
L02: Database Design -- 15 H.Lu/HKUST Reasoning About FDs Couple of additional rules (that follow from AA): 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 ZY T, then X Z T Example: Contracts(cid, sid, jid, did, pid, qty, value), and: C is the key: C CSJDPQV Project purchases each part using single contract: JP C Dept purchases at most one part from a supplier: SD P JP C, C CSJDPQV imply JP CSJDPQV SD P implies SDJ JP SDJ JP, JP CSJDPQV imply SDJ CSJDPQV
16
L02: Database Design -- 16 H.Lu/HKUST Closure of Attribute Sets 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 + ?
17
L02: Database Design -- 17 H.Lu/HKUST Algorithm to Compute Closure Algorithm to compute +, the closure of under F result:= ; while (changes to result) do for each in F do begin if result then result :=result ; end R = (A, B, C, G, H, I) F = (A B A C CG H CG I B H } (AG + ) 1. Result= AG 2. Result= ABCG (A C; A B and A AGB) 3. Result= ABCGH (CG H and CG AGBC) 4. Result=ABCGHI (CG I and CG AGBCH) Is AG a candidate key?
18
L02: Database Design -- 18 H.Lu/HKUST Uses of Attribute Closure There are several uses of the attribute closure algorithm: Testing for superkey: To test if is a superkey, we compute +, and check if + contains all attributes of R. Testing functional dependencies To check if a functional dependency holds (or, in other words, is in F+), just check if +. That is, we compute + by using attribute closure, and then check if it contains . Is a simple and cheap test, and very useful Computing closure of F For each R, we find the closure +, and for each S +, we output a functional dependency S.
19
L02: Database Design -- 19 H.Lu/HKUST Closure of a Set of Functional Dependencies Given a set F set of functional dependencies, there are certain other functional dependencies that are logically implied by F. E.g. If A B and B C, then we can infer that A C The set of all functional dependencies logically implied by F is the closure of F. We denote the closure of F by F+.
20
L02: Database Design -- 20 H.Lu/HKUST Procedure for Computing F+ F+ = F repeat for each FD f in F+ apply reflexivity and augmentation rules on f add the resulting functional dependencies to F+ for each pair of FD f1 and f2 in F+ if f1 and f2 can be combined using transitivity then add the resulting FD to F+ until F+ does not change any further
21
L02: Database Design -- 21 H.Lu/HKUST Example R = (A, B, C, G, H, I) F = { A B, A C, CG H, CG I, B H} some members of F+ A H by transitivity from A B and B H AG I by augmenting A C with G, to get AG CG and then transitivity with CG I CG HI from CG H and CG I : “union rule” can be inferred from –definition of functional dependencies, or –Augmentation of CG I to infer CG CGI, augmentation of CG H to infer CGI HI, and then transitivity
22
L02: Database Design -- 22 H.Lu/HKUST Minimal Cover Sets of functional dependencies may have redundant dependencies that can be inferred from the others Eg: A C is redundant in: {A B, B C, A C} Parts of a functional dependency may be redundant E.g. on RHS: {A B, B C, A CD} can be simplified to {A B, B C, A D} E.g. on LHS: {A B, B C, AC D} can be simplified to {A B, B C, A D} Intuitively, a minimal cover of F is a “minimal” set of functional dependencies equivalent to F, with no redundant dependencies or having redundant parts of dependencies
23
L02: Database Design -- 23 H.Lu/HKUST Minimal Cover for a Set of FDs 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. Example: F: {A B, ABCD E, EF GH, ACDF EG} has minimal cover G: {A B, ACD E, EF G, EF H}
24
L02: Database Design -- 24 H.Lu/HKUST Extraneous Attributes Consider a set F of functional dependencies and the functional dependency in F. Attribute A is extraneous in if A and F logically implies (F – { }) {( – A) }. Attribute A is extraneous in if A and the set of functional dependencies (F – { }) { ( – A)} logically implies F. Note: implication in the opposite direction is trivial in each of the cases above, since a “stronger” functional dependency always implies a weaker one Example: Given F = {A C, AB C } B is extraneous in AB C because A C logically implies AB C. Example: Given F = {A C, AB CD} C is extraneous in AB CD since A C can be inferred even after deleting C
25
L02: Database Design -- 25 H.Lu/HKUST Testing if an Attribute is Extraneous Consider a set F of functional dependencies and the functional dependency in F. To test if attribute A is extraneous in compute (a – {A})+ using the dependencies in F if (a – {A})+ contains A, A is extraneous To test if attribute A is extraneous in compute + using only the dependencies in F’ = (F – { }) { ( – A)}, check that + contains A; if it does, A is extraneous
26
L02: Database Design -- 26 H.Lu/HKUST Computing a Minimal Cover 1. Rewrite FDs in F into G so that all FDs in G have a single attribute on the right side 2. For each FD in G, check each attribute in the left side to see if it can be deleted while preserving equivalence to F +. 3. Check each remaining FD in G to see if it can be deleted while preserving equivalence to F +.
27
L02: Database Design -- 27 H.Lu/HKUST Minimal Cover – An Example F: {A B, ABCD E, EF G, EF H, ACDF EG} Rewrite F into G G: {A B, ABCD E, EF G, EF H, ACDF E, ACDF G} Since A B, B in ABCD E can be deleted G: {A B, ACD E, EF G, EF H, ACDF E, ACDF G} Since A B, ACD E, EF G, ACDF G is redundant Since ACD E, ACDF E is redundant No more extraneous attributes at the left side and no more redundant FDS Minimal cover: {A B, ACD E, EF G, EF H}
28
L02: Database Design -- 28 H.Lu/HKUST Minimal Cover – Another Example F = {A BC, B C, A B, AB C} G = {A B, A C, B C, A B, AB C} G = {A B, A C, B C, AB C} AB C is redundant, G = {A B, A C, B C} A C is logically implied by A B and B C, A C is redundant The minimal cover is: {A B, B C}
29
L02: Database Design -- 29 H.Lu/HKUST Summary Functional dependency (FD) theory forms the base of database design theory. We will see how they are used in database design. There are other forms dependencies, such as multivalued dependency (MVD), inclusion dependency, etc. In practical applications, FD seems sufficient to derive good schema
30
H.Lu/HKUST L02: Logical Database Design Introduction Functional Dependencies Normal Forms Normalization by decomposition
31
L02: Database Design -- 31 H.Lu/HKUST 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 all have the same B value!
32
L02: Database Design -- 32 H.Lu/HKUST First Normal Form Domain is atomic if its elements are considered to be indivisible units Examples of non-atomic domains: Set of names, composite attributes Identification numbers like CS101 that can be broken up into parts A relational schema R is in first normal form if the domains of all attributes of R are atomic Non-atomic values complicate storage and encourage redundant (repeated) storage of data We assume all relations are in first normal form
33
L02: Database Design -- 33 H.Lu/HKUST Second Normal Form R: Sales ( dept, item, price) FD: dept, item price; item price R is in 1NF dept item price KEY Anomalies? Yes. Caused by non-full functional dependency A 2NF relation is a 1NF relation whose nonprime attributes are fully and functionally dependent on the keys Sales (dept, item) Iteminfo (item, price)
34
L02: Database Design -- 34 H.Lu/HKUST Third Normal Form Iteminfo (item, price,discount) item price price discount discount item price KEY Anomalies? Relation R with FDs F is in 3NF if, for all X A in F + A X, or X contains a key for R, or A is part of some key for R. Iteminfo (item, price) Discnt(price,discount) Yes. Caused by transitive dependency
35
L02: Database Design -- 35 H.Lu/HKUST What Does 3NF Achieve? If 3NF violated by X A, one of the following holds: X is a subset of some key K We store (X, A) pairs redundantly. X is not a proper subset of any key. There is a chain of FDs K X A, which means that we cannot associate an X value with a K value unless we also associate an A value with an X value. But: even if a relation is in 3NF, these problems could arise. e.g., Reserves SBDC, SBD is a key, if C S, SDBC is in 3NF, but for each reservation of sailor S, same (S, C) pair is stored.
36
L02: Database Design -- 36 H.Lu/HKUST Third Normal Form – Another Example SalaryInfo(salary-bracket, status, bonus) Salary-bracket, status bonus; bonus status Salary- bracket status price KEY
37
L02: Database Design -- 37 H.Lu/HKUST Boyce-Codd Normal Form (BCNF) Relationn R with FDs F is in BCNF if, for all X A in F + A X (called a trivial FD), or X contains a key for R. In other words, R is in BCNF if the only non-trivial FDs that hold over R are key constraints. No dependency in R that can be predicted using FDs alone. If we are shown two tuples that agree upon the X value, we cannot infer the A value in one tuple different from the A value in the other. If example relation is in BCNF, the 2 tuples must be identical (since X is a key).
38
L02: Database Design -- 38 H.Lu/HKUST BCNF – Examples R = (A, B, C) F = { A B, B C } Is R in BCNF? Property F = { P CAL, CL AP, A C } Property_id Country_name Area Lot#
39
H.Lu/HKUST L02: Logical Database Design Introduction Functional Dependencies Normal Forms Normalization by decomposition
40
L02: Database Design -- 40 H.Lu/HKUST Decomposition of a Relation Scheme Suppose that relation R contains attributes A1... An. A decomposition of R consists of replacing R by two or more relations such that: Each new relation scheme contains a subset of the attributes of R (and no attributes that do not appear in R), and Every attribute of R appears as an attribute of one of the new relations. Intuitively, decomposing R means we will store instances of the relation schemes produced by the decomposition, instead of instances of R. E.g., Can decompose ENORWH into ENORH and RW.
41
L02: Database Design -- 41 H.Lu/HKUST Example Decomposition Decompositions should be used only when needed. SNLRWH has FDs E ENORWH and R W Second FD causes violation of 3NF; W values repeatedly associated with R values. Easiest way to fix this is to create a relation RW to store these associations, and to remove W from the main schema: i.e., we decompose ENORWH into ENORH and RW The information to be stored consists of ENORWH tuples. If we just store the projections of these tuples onto ENORH and RW, are there any potential problems that we should be aware of?
42
L02: Database Design -- 42 H.Lu/HKUST Problems with Decompositions There are three potential problems to consider: Some queries become more expensive. e.g., How much did sailor Joe earn? (salary = W*H) Given instances of the decomposed relations, we may not be able to reconstruct the corresponding instance of the original relation! Fortunately, not in the ENORWH example. Checking some dependencies may require joining the instances of the decomposed relations. Fortunately, not in the ENORWH example. Tradeoff: Must consider these issues vs. redundancy.
43
L02: Database Design -- 43 H.Lu/HKUST Lossless Join Decompositions Decomposition of R into X and Y is lossless-join w.r.t. a set of FDs F if, for every instance r that satisfies F: x (r) y (r) = r It is always true that r x (r) y (r) In general, the other direction does not hold! If it does, the decomposition is lossless-join. Definition extended to decomposition into 3 or more relations in a straightforward way. It is essential that all decompositions used to deal with redundancy be lossless! (Avoids Problem (2).)
44
L02: Database Design -- 44 H.Lu/HKUST More on Lossless Join The decomposition of R into X and Y is lossless-join wrt F if and only if the closure of F contains: X Y X, or X Y Y In particular, the decomposition of R into UV and R - V is lossless- join if U V holds over R.
45
L02: Database Design -- 45 H.Lu/HKUST Losslessness Test – Case 1 Case 1: R is decomposed into R1 and R2. The decomposition is lossless if at least one of the following FDs is contained in F +. R1 R2 R1 – R2 or R1 R2 R2 – R1 Example: R (A, B, C) FD: A B Decomposition #1: R1 (A, B), R2 (A, C) Decomposition #2: R1 (A, B), R2 (B, C)
46
L02: Database Design -- 46 H.Lu/HKUST Losslessness Test – Case 2 1. Given a set of relations and FD 2. Construct a table: 3. columns – attributes, rows – relations 4. (i, j) = ‘a’ if Aj is an attribute of Ri 5. For each FD XY, if column X contains an ‘a’, mark column Y as ‘a’. 6. Repeat the above until no change 7. If there is at least one row with all ‘a’s, the decomposition is lossless, not otherwise.
47
L02: Database Design -- 47 H.Lu/HKUST Example (Losslessness Test, Case 2) A C B C C D R (A, B, C, D) R1 (A, B), R2 (B, D), R3 (A, B,C), R4 (B, C, D) FD: A C, B C, CD B, C D
48
L02: Database Design -- 48 H.Lu/HKUST Dependency Preserving Decomposition Consider CSJDPQV, C is key, JP C and SD P. BCNF decomposition: CSJDQV and SDP Problem: Checking JP C requires a join! Dependency preserving decomposition (Intuitive): If R is decomposed into X, Y and Z, and we enforce the FDs that hold on X, on Y and on Z, then all FDs that were given to hold on R must also hold. (Avoids Problem (3).) Projection of set of FDs F: If R is decomposed into X,... projection of F onto X (denoted F X ) is the set of FDs U V in F + (closure of F ) such that U, V are in X.
49
L02: Database Design -- 49 H.Lu/HKUST Dependency Preserving Decompositions (Contd.) Decomposition of R into X and Y is dependency preserving if (F X union F Y ) + = F + i.e., if we consider only dependencies in the closure F + that can be checked in X without considering Y, and in Y without considering X, these imply all dependencies in F +. Important to consider F +, not F, in this definition: ABC, A B, B C, C A, decomposed into AB and BC. Is this dependency preserving? Is C A preserved????? Dependency preserving does not imply lossless join: ABC, A B, decomposed into AB and BC. And vice-versa! (Example?)
50
L02: Database Design -- 50 H.Lu/HKUST Dependency Preserving -- Example R (A, B, C) FD: AB C, C B R1 (A, C), R2 (B, C) Dependency preserving? lossless? R1 { (a1, c1), (a1, c2)} R2 { (b1, c1), (b1, c2) } R1 R2 ?
51
L02: Database Design -- 51 H.Lu/HKUST Decomposition into BCNF Consider relation R with FDs F. If X Y violates BCNF, decompose R into R - Y and XY. Repeated application of this idea will give us a collection of relations that are in BCNF; lossless join decomposition, and guaranteed to terminate. e.g., CSJDPQV, key C, JP C, SD P, J S To deal with SD P, decompose into SDP, CSJDQV. To deal with J S, decompose CSJDQV into JS and CJDQV In general, several dependencies may cause violation of BCNF. The order in which we ``deal with? them could lead to very different sets of relations!
52
L02: Database Design -- 52 H.Lu/HKUST BCNF and Dependency Preservation In general, there may not be a dependency preserving decomposition into BCNF. e.g., CSZ, CS Z, Z C Can ’ t decompose while preserving 1st FD; not in BCNF. Similarly, decomposition of CSJDQV into SDP, JS and CJDQV is not dependency preserving (w.r.t. the FDs JP C, SD P and J S). However, it is a lossless join decomposition. In this case, adding JPC to the collection of relations gives us a dependency preserving decomposition. JPC tuples stored only for checking FD! (Redundancy!)
53
L02: Database Design -- 53 H.Lu/HKUST Decomposition into 3NF Obviously, the algorithm for lossless join decomposition into BCNF can be used to obtain a lossless join decomposition into 3NF (typically, can stop earlier). To ensure dependency preservation, one idea: If X Y is not preserved, add relation XY. Problem is that XY may violate 3NF! e.g., consider the addition of CJP to `preserve? JP C. What if we also have J C ? Refinement: Instead of the given set of FDs F, use a minimal cover for F.
54
L02: Database Design -- 54 H.Lu/HKUST Decomposition to 3NF 1begin 2S = 3 F c = minimal cover of F; 4for each X Y F c do 5 if not exists R i S and XY R i then 6S = S {R j (XY)} 7endif 8endfor 9if not exists R i S and R i contains a key of R then 10S = S {R j } 11endif 12remove from S the redundant relations ; 13return(S); 14end. A relation for each FD A relation for each key
55
L02: Database Design -- 55 H.Lu/HKUST Summary of Normalization If a relation is in BCNF, it is free of redundancies that can be detected using FDs. Thus, trying to ensure that all relations are in BCNF is a good heuristic. If a relation is not in BCNF, we can try to decompose it into a collection of BCNF relations. Must consider whether all FDs are preserved. If a lossless-join, dependency preserving decomposition into BCNF is not possible (or unsuitable, given typical queries), should consider decomposition into 3NF. Decompositions should be carried out and/or re-examined while keeping performance requirements in mind.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.