Vous êtes sur la page 1sur 10

Database Design

Chapter 11
Relational Database Design Algorithms and Further Dependencies
CS 6360.501 (Fall 2009) Instructor: Sunan Han

Outline
1. Properties of Relational Decompositions 2. Algorithms for Relational Database Schema
Algorithm 11.1: Testing for Non-additive Join Property Algorithm 11.2: Relational Synthesis into 3NF with Dependency Preservation Algorithm 11.3: Relational Decomposition into BCNF with non-additive join property Algorithm 11.4: Relational Synthesis into 3NF with Dependency Preservation and Non-Additive Join Property Algorithm 11.4a: Finding a Key K for R Given a set F of Functional Dependencies
Slide 11- 2

The University of Texas at Dallas

Relational Decompositions
Definition of Universal Relation Schema: A relation schema R = {A1, A2, , An} that includes all the attributes of the database. Universal relation assumption: Every attribute name is unique Think of this as a bottom-up design: we know all the attributes and functional dependencies in the database as a single relation. We need to find a way to decompose it into smaller relations that are in a desired normal form, as well as maintaining certain important properties
Slide 11- 3

Relational Decompositions
Decomposition:
The process of decomposing the universal relation schema R into a set of relation schemas D = {R1,R2, , Rm} that will become the relational database schema by using the functional dependencies.

Attribute preservation condition:


Each attribute in R will appear in at least one relation schema Ri in the decomposition so that no attributes are lost. So, R1 R2 , Rm = R
Slide 11- 4

Insufficiency of Normal Form


Another goal of decomposition is to have each individual relation Ri in the decomposition D be in BCNF or 3NF. However, this still does not sufficient to guarantee a good database design Additional properties of decomposition are needed to prevent from generating spurious tuples Example: the relation EMP_LOCS(Ename, Plocation) from Chapter 10 is in BCNF (therefore 3NF). It still generates spurious tuples even joining with a BCNF relation PROJECT(Pname, Pnumber, Plocation, Dnum)
Slide 11- 5

Insufficiency of Normal Form Example

A natural join will generate spurious tuples (e.g. Wong would work on project 30 in the join, which he was not supposed to)

Slide 11- 6

Definition of Projection
Given a set of dependencies F on R, the projection of F on Ri, denoted by Ri(F) where Ri is a subset of R, is the set of dependencies X Y in F+ such that the attributes in X Y are all contained in Ri (example on next slide) Hence, the projection of F on each relation schema Ri in the decomposition D = {R1,R2, , Rm} is the set of functional dependencies in F+, the closure of F, such that all their left- and right-hand-side attributes are in Ri. Informally, we want each dependency in R to be directly from one of the decomposed Ri or can be inferred from dependencies in some Ri. This is the dependency preservation condition
Slide 11- 7

Projection Example: R= EMP_DEPT


Relation R(Ssn, Ename, Bdate, Address, Dnumber, Dname, Dmgr_ssn)s F is as follows
F = {Ssn {Ename, Bdate, Address, Dnumber}, Dnumber {Dname, Dmgr_ssn} }

We will have F above plus the following in F+


Ssn Dnumber Ssn {Dname, Dmgr_ssn) Dnumber Dname Dnumber Dmgr_ssn Ssn every_combination_else (decomposition) (Transitivity) (decomposition) ()

For subset relation R1(Dnumber, Dname, Dmgr_ssn), the projection of F on R1 is the subset of FDs in F+ such that left and right hand side attributes of the FDs are in R1. So,
R1(F) = { Dnumber {Dname, Dmgr_ssn}, Dnumber Dname, Dnumber Dmgr_ssn }
Slide 10- 8

Dependency Preservation Property of Decomposition


A decomposition D = {R1, R2, ..., Rm} of R is dependency-preserving with respect to F if the union of the projections of F on each Ri in D is equivalent to F; that is ((R1(F)) . . . (Rm(F)))+ = F+ Claim 1:
It is always possible to find a dependency-preserving decomposition D with respect to F such that each relation Ri in D is in 3NF

Dependency Preservation Example


In the following example, FD2 is lost in the decomposition

If a decomposition is not dependency-preserving, then some dependencies are lost in the decomposition
Slide 11- 9 Slide 11- 10

Dependency Preservation Example


In this example, all FDs are preserved

Dependency Preservation Example


In this example (in 3NF), FD1 will get lost regardless how TEACH is decomposed
Student Course FD1 FD2 Instructor

Slide 11- 11

Slide 11- 12

Non-additive (Lossless) Join Property of a Decomposition


Definition: Non-additive join property: a decomposition D = {R1, R2, ..., Rm} of R has the non-additive (lossless) join property with respect to the set of dependencies F on R if, for every relation state r of R that satisfies F, the following holds, where * is the natural join of all the relations in D: * ( R1(r), ..., Rm(r)) = r (Consider as the PROJECT operation in relational algebra) Note: The word loss in lossless refers to loss of information, not to loss of tuples. In fact, for loss of information a better term is addition of spurious information

Algorithm 11.1: Testing for Non-additive Join Property


Input: A universal relation R, a decomposition D = {R1, R2, ..., Rm} of R, and a set F of functional dependencies.
1. Create an initial matrix S with one row i for each relation Ri in D, and one column j for each attribute Aj in R. 2. Set S(i,j):=bij for all matrix entries. (* each bij is a distinct symbol associated with indices (i,j) *). 3. For each row i representing relation schema Ri {for each column j representing attribute Aj {if (relation Ri includes attribute Aj) then set S(i,j):= aj;};}; (* each aj is a distinct symbol associated with index (j) *)

(Refer to an example on the whiteboard) CONTINUED on NEXT SLIDE

Slide 11- 13

Slide 11- 14

Algorithm 11.1: Testing for Non-additive Join Property


4. Repeat the following loop until a complete loop execution results in no changes to S {for each functional dependency X Y in F {for all rows which have the same symbols in the columns corresponding to attributes in X {make the symbols in each column that correspond to an attribute in Y be the same in all these rows as follows: If any of the rows has an a symbol for the column, set the other rows to that same a symbol in the column. If no a symbol exists for the attribute in any of the rows, choose one of the b symbols that appear in one of the rows for the attribute and set the other rows to that same b symbol in the column ;}; }; }; 5. If a row is made up entirely of a symbols, then the decomposition has the lossless join property; otherwise it does not.
Slide 11- 15

Testing for Non-additive Join Property


The idea behind Algorithm 11.1 is that for any natural join of the decomposed relations, make sure that one side of equality in the join condition can uniquely determine the other side In X Y, X uniquely determines Y. In step 4, when columns corresponding to Y already contains aj, meaning there exist attributes in X in the same relation that uniquely determines Aj, so its save to populate aj to other rows having same attributes in X
Slide 11- 16

Example: Testing for Non-additive Join Property

Example: Testing for Non-additive Join Property

Slide 11- 17

Slide 11- 18

Testing Binary Decompositions for Non-additive Join Property


Binary Decomposition: Decomposition of a relation R into two relations. PROPERTY NJB (non-additive join test for binary decompositions): A decomposition D = {R1, R2} of R has the non-additive join property with respect to a set of functional dependencies F on R if and only if either
The FD ((R1 R2) The FD ((R1 R2) (R1- R2)) is in F+, or (R2 - R1)) is in F+.

Binary Decomposition Example


ED1 ED2 = {Dnumber} ED2 ED1 = {Dname, Dmgr_ssn} And ED1 ED2 ED2 ED1, or Dnumber {Dname, Dmgr_ssn} (FD1) So, the binary decomposition is non-additive

FD1

Slide 11- 19

Slide 10- 20

Successive Non-additive Join Decomposition


Claim 2 (Preservation of non-additivity in successive decompositions):
If a decomposition D = {R1, R2, ..., Rm} of R has the non-additive join property with respect to a set of functional dependencies F on R, and if a decomposition Di = {Q1, Q2, ..., Qk} of Ri has the non-additive join property with respect to the projection of F on Ri, then the decomposition D2 = {R1, R2, ..., Ri-1, Q1, Q2, ..., Qk, Ri+1, ..., Rm} of R has the non-additive join property with respect to F
Slide 11- 21

Algorithm 11.2: Relational Synthesis into 3NF with Dependency Preservation


Input: A universal relation R and a set of functional dependencies F on the attributes of R 1. Find a minimal cover G for F (use Algorithm 10.2); 2. For each left-hand-side X of a functional dependency that appears in G, create a relation schema in D with attributes {X {A1} {A2} ... {Ak}}, where X A1, X A2, ..., X Ak are the only dependencies in G with X as left-hand-side (X is the key of this relation) ; 3. Place any remaining attributes (that have not been placed in any relation) in a single relation schema to ensure the attribute preservation property. Claim 3: Every relation schema created by Algorithm 11.2 is in 3NF.

Slide 11- 22

Example of Algorithm 11.2


Consider relation U and F = {FD1, FD2, FD3} as follows U(Emp_ssn,Pno,Esal,Ephone,Dno,Pname,Plocation) FD1: Emp_ssn Esal, Ephone, Dno FD2: Pno Pname, Plocation FD3: {Emp_ssn, Pno} Esal, Ephone, Dno, Pname, Plocation {Emp_ssn, Pno} is a key of U When looking for redundant attributes in left-hand-side of FDs in applying minimum cover algorithm 10.2, we find redundant Emp_ssn, or Pno. So, the minimum cover is {Emp_ssn Esal, Ephone, Dno; Pno Pname, Plocation} By algorithm 11.2, we decompose U into R1 and R2 in 3NF: R1(Emp_ssn, Esal, Ephone, Dno) and R2(Pno, Pname, Plocation
Slide 11- 23

Algorithm 11.3: Relational Decomposition into BCNF with non-additive join property
Input: A universal relation R and a set of functional dependencies F on the attributes of R.
1. Set D := {R}; 2. While there is a relation schema Q in D that is not in BCNF do { choose a relation schema Q in D that is not in BCNF; find a functional dependency X Y in Q that violates BCNF; replace Q in D by two relation schemas (Q - Y) and (X Y); }; Assumption: No null values are allowed for the join attributes.
Slide 11- 24

Algorithm 11.4: Relational Synthesis into 3NF with Dependency Preservation and NonAdditive Join Property
Input: A universal relation R and a set of functional dependencies F on the attributes of R.
1. Find a minimal cover G for F (Use Algorithm 10.2). 2. For each left-hand-side X of a functional dependency that appears in G, create a relation schema in D with attributes {X {A1} {A2} ... {Ak}}, where X A1, X A2, ..., X >Ak are the only dependencies in G with X as left-hand-side (X is the key of this relation). 3. If none of the relation schemas in D contains a key of R, then create one more relation schema in D that contains attributes that form a key of R. (Use Algorithm 11.4a to find the key of R) 4. Among decomposed relation schemas, if Ri is a subset of Rj, remove Ri
Slide 11- 25

Algorithm 11.4a Finding a Key K for R Given a set F of Functional Dependencies


Input: A universal relation R and a set of functional dependencies F on the attributes of R
1. Set K := R; 2. For each attribute A in K {

Compute (K - A)+ with respect to F; If (K - A)+ contains all the attributes in R, (All attributes in R can be FDed by K-A) then set K := K - {A}; }
Slide 11- 26

Algorithm 11.4: Example


In a simplified form for example LOTS1A F: {P LCA, LC AP, A C} 1. Apply minimum cover algorithm (MCA): in step 1 F: {P L, P C, P A, LC A, LC P, A C} In step 3 of MCA, P A is redundant (P LC, LC A) MC G of F: {P L, P C, LC A, LC P, A C} 2. Decompose to three relations based on G R1(P, L, C) R2(L, C, P, A) R3(A, C) 3. R1 or R2 contains a key (P) of R 4. R1 and R3 are actually subset of R2, so R2 is the final decomposition (No change from LOTS1A, which was already in 3NF!)
Slide 11- 27

Problems with Null Values


One problem of having null values for a foreign key attribute may cause loss of tuples in a JOIN operation (Example in next two slides) A related problem is that, in decomposing a relation with null values, if the nulls are not appropriately reserved, an inner join operation will result in loss of tuples. Dangling tuples refer to the tuples in danger in the join

Slide 11- 28

Null Values in Attribute Dnum

Join Operation with Relation of Nulls

Missing tuples in a NATURAL JOIN

LEFT OUTER JOIN must be used


Slide 11- 29 Slide 11- 30

Example for Dangling Tuples

Example for Dangling Tuples

Slide 11- 31

Slide 11- 32

Discussion of Normalization Algorithms


Problems: The database designer must first specify all the relevant functional dependencies among the database attributes. These algorithms are not deterministic in general. It is not always possible to find a decomposition into relation schemas that preserves dependencies and allows each relation schema in the decomposition to be in BCNF (instead of 3NF as in Algorithm 11.4).

Summary of Algorithms

Slide 11- 33

Slide 11- 34

Chapter Summary
Designing a Set of Relations from decompositions Properties of Relational Decompositions Algorithms for Relational Database Schema

Assignment #9
Page 409, 11.27 (on 10.29 only), 11.29 (a), 11.30 (LJ1 = NJB on slide 19) Due date 11/9/09 (Monday)

Slide 11- 35

Slide 10- 36

Project Phase 3
1.

Project Phase 3
6.

2.

3. 4.

5.

Simplify the original model by removing the computer requirement (and therefore the administrator/administer relations) and by removing the accounting group/department. Normalize all of your tables to third normal form, if necessary. If a change is made, explain why. Draw a functional dependency diagram for each table schema Create the database and tables on MS SQL Server. Primary keys and foreign keys must be defined appropriately. Populate your tables with an initial state. Make sure all constraints (key, entity integrity, referential integrity and domain) are maintained and the initial state forms a real world database so that reasonable queries will return sensible results

7.

Use the Create View statement to create the following views: a. Employee: This view returns Employee_ID, First Name, Last Name, Rank, Supervisor ID, Address and Title of the employee. b. Senior employee: This view returns the Employee_ID, First Name, Last Name, and Salary of the employees whose Rank is above 6 including 6. c. Marketing information: This view returns the Employee_ID, Name of employee who is in the marketing group and the marketing sites and products the employee is in charge of. Make the following queries into your database. a. List the number of employees in the IT group. b. List the all the employees with the same title Sales Manager
Slide 11- 38

Slide 11- 37

Project Phase 3
7.

Project Phase 3
Submission
1. 2.

Make the following queries into your database.


c.

d.

e.

f.

g. h.

i.

j.

List the Last Name, Employee_ID, Rank, and Title of the female employees whose Rank is above 6 including 6. For a certain employee, find all his/her supervisees Last Name, Employee_ID, and Rank For a certain city (e.g. Richardson), find Employee_ID of all employees who live in that city. For a certain employee, find all the trips he/she made to a certain destination (like Atlanta). Find all the marketing sites which have at least 5 employees. List Name and Employee_ID of the employees who sold products at a certain marketing site. List all the VPs who are in charge of the Marketing Department and whose number of stocks is more than 10,000. Find all the product names sold by a particular marketing employee
Slide 11- 39

3. 4. 5.

6. 7.

The new simplified EER diagram. Relational model schema after simplification and normalization. Make sure all relations are in 3NF. The relation schemas should include primary keys as well as foreign keys and include arrowed lines from foreign keys to primary keys, for all relations. Dependency diagram for each table All requested SQL definition and view statements in 4, 5 and 6 All queries in 7. You are also required to demonstrate the 10 queries to my TA. Make appointment with Ling for 10 minutes per student, starting right after the due date The weight of Phase 3 is 50% of total project Due date 11/30/09 (Monday, right after the Thanksgiving break)
Slide 11- 40

Vous aimerez peut-être aussi