39
8.1 atabase System Concepts - 6 th Edition Chapter 8: Relational Database Chapter 8: Relational Database Design Design Features of Good Relational Design Lossless join decomposition Functional Dependency Theory Atomic Domains and First Normal Form Normal Forms: BCNF and 3NF Decomposition Algorithms Using Functional Dependencies Database-Design Issues

Chapter 8: Relational Database Design

  • Upload
    brooke

  • View
    56

  • Download
    0

Embed Size (px)

DESCRIPTION

Chapter 8: Relational Database Design. Features of Good Relational Design Lossless join decomposition Functional Dependency Theory Atomic Domains and First Normal Form Normal Forms: BCNF and 3NF Decomposition Algorithms Using Functional Dependencies Database-Design Issues. Larger Schemas?. - PowerPoint PPT Presentation

Citation preview

Page 1: Chapter 8:  Relational Database Design

8.1Database System Concepts - 6th Edition

Chapter 8: Relational Database DesignChapter 8: Relational Database Design

Features of Good Relational Design

Lossless join decomposition

Functional Dependency Theory

Atomic Domains and First Normal Form

Normal Forms: BCNF and 3NF

Decomposition Algorithms Using Functional Dependencies

Database-Design Issues

Page 2: Chapter 8:  Relational Database Design

8.2Database System Concepts - 6th Edition

Larger Schemas?Larger Schemas?

Two schemas:

instructor (ID, name, dept_name, salary)

department (dept_name, building, budget)

Suppose we combine instructor and department into inst_dept as follows:

Result is possible repetition of information (the amount of budget)

刪掉就無 EE資訊

重複

Page 3: Chapter 8:  Relational Database Design

8.3Database System Concepts - 6th Edition

Disadvantage of Large SchemasDisadvantage of Large Schemas

Update anomalies:

Modifying the budget in one tuple but not all tuples leads to inconsistency.

Cannot insert a new department until the first instructor is hired

ID is part of PK.

Deleting the last instructor will lose the department information

刪掉 Kim 就無 EE 資訊

Page 4: Chapter 8:  Relational Database Design

8.4Database System Concepts - 6th Edition

What About Smaller Schemas?What About Smaller Schemas?

Suppose we had started with inst_dept. How would we know to split up (decompose) it into instructor and department?

Intuition: put “strongly-related” attributes in the same relation

Write a rule: “every department (identified by its department name) must have only one building and one budget value”.

Denote as a functional dependency:

dept_name building, budget

In inst_dept, because dept_name is not a candidate key, the building and budget of a department may have to be repeated.

This indicates the need to decompose inst_dept

Not all decompositions are good. Suppose we decompose employee(ID, name, street, city, salary) into

employee1 (ID, name)

employee2 (name, street, city, salary)

The next slide shows how we lose information -- we cannot reconstruct the original employee relation -- and so, this is a lossy decomposition.

Page 5: Chapter 8:  Relational Database Design

8.5Database System Concepts - 6th Edition

A Lossy DecompositionA Lossy Decomposition

Page 6: Chapter 8:  Relational Database Design

8.6Database System Concepts - 6th Edition

Example of Lossless-Join DecompositionExample of Lossless-Join Decomposition

Lossless join decomposition

Decomposition of R = (A, B, C)R1 = (A, B) R2 = (B, C)

A B

12

A

B

12

r B,C(r)

A (r) B (r)A B

12

C

AB

B

12

C

AB

C

AB

A,B(r)

Page 7: Chapter 8:  Relational Database Design

8.7Database System Concepts - 6th Edition

Goal of NormalizationGoal of Normalization

Decide whether a particular relation R is in “good” form.

1NF, 2NF, 3NF, BCNF, etc 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

The normalization theory is based on functional dependencies

Constraints requiring that the value for a certain set of attributes determines uniquely the value for another set of attributes.

A functional dependency is a generalization of the notion of a key.

Page 8: Chapter 8:  Relational Database Design

8.8Database System Concepts - 6th Edition

First Normal FormFirst Normal Form

Domain is atomic if its elements are considered to be indivisible units

Examples of non-atomic domains:

Set of names, composite attributes

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

Example: Set of accounts stored with each customer, and set of owners stored with each account

We assume all relations are in first normal form. (It is not required in Object Based Databases (see page 1.18))

Non-1NF should be normalized as discussed in Chapter 7.

ID Name Phone_number

1 John {6601, 6602}

ID Name

1 John

ID Phone_number

1 6601

1 6602

Page 9: Chapter 8:  Relational Database Design

8.9Database System Concepts - 6th Edition

ID Phone_number

2222222222

456-7890123-4567

ID Phone_number

22222 {456-7890,123-4567}

Page 10: Chapter 8:  Relational Database Design

8.10Database System Concepts - 6th Edition

Functional Dependencies Functional Dependencies

Let R be a relation schema

R and R The functional dependency

holds on R if and only if for any legal relations r(R), whenever any two tuples t1 and t2 of r agree on the attributes , they also agree on the attributes .

Example: Consider R(A,B, C, D ) with the following instance of r.

On this instance, A C is satisfied, but C A is not satisfied.

Page 11: Chapter 8:  Relational Database Design

8.11Database System Concepts - 6th Edition

Functional Dependencies (Cont.)Functional Dependencies (Cont.)

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 classroom may, by chance, satisfy room_number capacity.

We usually use functional dependencies to specify constraints on all legal relations

We say that F holds on R if all legal relations on R satisfy the set of functional dependencies F.

Page 12: Chapter 8:  Relational Database Design

8.12Database System Concepts - 6th Edition

PracticePractice

inst_dept (ID, name, salary, dept_name, building, budget ).

For each constraint, write the corresponding functional dependency

Every instructor (ID) has only one name and one salary.

Ans:

Every department (dept_name) is located in only one building and has only one budget.

Ans:

Every instructor belongs to only one department.

Ans:

Explain why each of the following functional dependency doesn’t make sense.

dept_name ID

Ans:

building dept_name

Ans:

Page 13: Chapter 8:  Relational Database Design

8.13Database System Concepts - 6th Edition

Functional Dependencies (Cont.)Functional Dependencies (Cont.)

Functional dependencies allow us to express constraints that cannot be expressed using superkeys.

K is a superkey for relation schema R if and only if K R Example: employee = (ID, name, street, city, salary),

{ID} is a superkey iff ID-> ID, name, street, city, salary

{ID, name} is a superkey

iff ID, name -> ID, name, street, city, salary K is a candidate key for R if and only if

K R, and for no K, R

In other words, K is a minimal set of attributes

Example: {ID} is a candidate key, but {ID, name} is NOT.

Page 14: Chapter 8:  Relational Database Design

8.14Database System Concepts - 6th Edition

Functional Dependencies (Cont.)Functional Dependencies (Cont.)

A functional dependency is trivial if it is satisfied by all instances of a relation

Example: (see examples)

dept_name, building dept_name

In general, is trivial if

Given a set F of functional dependencies, there are certain other functional dependencies that are logically implied by F.

For example: If A B and B C, then we can infer that A C

(see examples)

The set of all functional dependencies logically implied by F is the closure of F.

We denote the closure of F by F+.

F+ is a superset of F.

Page 15: Chapter 8:  Relational Database Design

8.15Database System Concepts - 6th Edition

Closure of Attribute SetsClosure of Attribute Sets

Given a set of attributes define the closure of under F (denoted by +) as the set of attributes that are functionally determined by under F

Algorithm to compute +, the closure of under F

result := ;while (changes to result) do

for each in F dobegin

if result then result := result end

PS: result, result , =>

Page 16: Chapter 8:  Relational Database Design

8.16Database System Concepts - 6th Edition

Example of Attribute Set ClosureExample of Attribute Set Closure R = (A, B, C, G, H, I) F = {A B

A C CG HCG IB H}

(AG)+

1. result = AG

2. result = ABCG (A C and A B)

3. result = ABCGH (CG H and CG AGBC)

4. result = ABCGHI (CG I and CG AGBCH) Is AG a candidate key? (see definition)

1. Is AG a super key?

1. Does AG R? == Is (AG)+ R

2. Is any subset of AG a superkey?

1. Does A R? == Is (A)+ R

2. Does G R? == Is (G)+ R

Page 17: Chapter 8:  Relational Database Design

8.17Database System Concepts - 6th Edition

Lossless-join DecompositionLossless-join Decomposition

For the case of R = (R1, R2), we require that for all possible relations r on schema R

r = R1 (r ) R2 (r )

A decomposition of R into R1 and R2 is lossless join if at least one of the following dependencies is in F + : (see examples)

R1 R2 R1

R1 R2 R2

In other words, if R1 R2 forms a superkey of either R1 or R2, the decomposition of R is a lossless decomposition.

Page 18: Chapter 8:  Relational Database Design

8.18Database System Concepts - 6th Edition

Boyce-Codd Normal FormBoyce-Codd Normal Form

is trivial (i.e., )

is a superkey for R

A relation schema R is in BCNF with respect to a set F of functional dependencies if for all functional dependencies in F+ of the form

where R and R, at least one of the following holds:

注意 : 在後續的討論中 , 為了簡化起見 , 我們直接使用 F 而不是 F+.

Example schema : instr_dept (ID, name, salary, dept_name, building, budget )

It is not in BCNF because dept_name building, budgetholds on instr_dept, but dept_name is not a superkey

Page 19: Chapter 8:  Relational Database Design

8.19Database System Concepts - 6th Edition

Testing for BCNFTesting for BCNF

To check if a non-trivial dependency causes a violation of BCNF

1. compute + (the attribute closure of ), and

2. verify that it includes all attributes of R, that is, it is a superkey of R.

For a non-BCNF Schema R and a non-trivial dependency causeing a violation of BCNF, we decompose R into:

• (U )

• ( R - )

• 注意 : 課本用 R - ( - ) ,是當 和有交集時 , 需要減掉。 In our example, “dept_name building, budget” causes the violation

= dept_name = building, budget

and inst_dept (ID, name, salary, dept_name, building, budget )

is replaced by (U ) = (dept_name, building, budget ) <- department relation ( R - ) = (ID, name, salary, dept_name ) <- instructor relation

Page 20: Chapter 8:  Relational Database Design

8.20Database System Concepts - 6th Edition

BCNF Decomposition AlgorithmBCNF Decomposition Algorithm

Given F and R;

result := {R };done := false;while (not done) do

if (there is a schema Ri in result that is not in BCNF)then begin

let be a nontrivial functional dependency that holds on Ri and cause Ri not in BCNF; result := (result – Ri ) (Ri – ) (, );

endelse done := true;

Note: each Ri is in BCNF, and decomposition is lossless-join.

Page 21: Chapter 8:  Relational Database Design

8.21Database System Concepts - 6th Edition

Example of BCNF DecompositionExample of BCNF Decomposition

class (course_id, title, dept_name, credits, sec_id, semester, year, building, room_number, capacity, time_slot_id)

Functional dependencies: course_id→ title, dept_name, credits building, room_number→capacity course_id, sec_id, semester, year→building, room_number,

time_slot_id BCNF Decomposition:

course_id→ title, dept_name, credits holds but course_id is not a superkey.

We replace class by: course(course_id, title, dept_name, credits) class-1 (course_id, sec_id, semester, year, building,

room_number, capacity, time_slot_id)

Page 22: Chapter 8:  Relational Database Design

8.22Database System Concepts - 6th Edition

Example of BCNF Decomposition (Cont.)Example of BCNF Decomposition (Cont.)

course is in BCNF

How do we know this?

building, room_number→capacity holds on class-1

but {building, room_number} is not a superkey for class-1.

We replace class-1 by:

classroom (building, room_number, capacity)

section (course_id, sec_id, semester, year, building, room_number, time_slot_id)

classroom and section are in BCNF.

Final decomposition: course, classroom, section.

Page 23: Chapter 8:  Relational Database Design

8.23Database System Concepts - 6th Edition

PracticePractice Decompose

inst_dept(ID, name, dept_name, salary, building, budget, SID)

based on the following set of functional dependency:

{dept_name building budget,

ID name salary,

SID dept_name ID}

Page 24: Chapter 8:  Relational Database Design

8.24Database System Concepts - 6th Edition

BCNF and Dependency PreservationBCNF and Dependency Preservation

Sometimes, to achieve BCNF, we will force attributes in one functional dependency represented in different schemas.

(This is called the violation of dependency preservation).

Example:

Given schema: dept_advisor(s_ID, i_ID, dept_name),

and functional dependencies:

{i_ID->dept_name ; s_ID, dept_name -> i_ID}

PS: {s_ID, dept_name } is a primary key.

一個學生在一個系只有一個指導老師 ( 學生可以多系 ), 老師只能一系 .

After the BCNF decomposition, we get

(s_ID, i_ID) and (i_ID, dept_name)

Note that s_ID, dept_name, i_ID are represented in two schemas.

Because it is not always possible to achieve both BCNF and dependency preservation, we consider a weaker normal form, known as third normal form.

Page 25: Chapter 8:  Relational Database Design

8.25Database System Concepts - 6th Edition

Third Normal FormThird Normal Form

A relation schema R is in third normal form (3NF) if for all:

in F+

at least one of the following holds:

is trivial (i.e., )

is a superkey for R

Each attribute A in is contained in a candidate key for R.

此處的意思是 , A 被 所決定 , 也被某個 CK 所決定 .

PS: 課本是 - .

(NOTE: each attribute may be in a different candidate key)

If a relation is in BCNF, it is in 3NF (since in BCNF one of the first two conditions above must hold).

Third Normal Form (3NF)

Allows some redundancy (see the next two pages)

There is always a lossless-join, dependency-preserving decomposition into 3NF.

Page 26: Chapter 8:  Relational Database Design

8.26Database System Concepts - 6th Edition

3NF Example3NF Example

Relation dept_advisor:

dept_advisor: (s_ID, i_ID, dept_name)F = {s_ID, dept_name i_ID ; i_ID dept_name}

Two candidate keys: {s_ID, dept_name} and {i_ID, s_ID}

R is in 3NF

s_ID, dept_name i_ID

– s_ID, dept_name is a superkey

i_ID dept_name

– dept_name is contained in a candidate key

Page 27: Chapter 8:  Relational Database Design

8.27Database System Concepts - 6th Edition

Redundancy in 3NFRedundancy in 3NF

Example

dept_advisor (s_ID, i_ID, dept_name)

F = {s_ID, dept_name i_ID ; i_ID dept_name}

Example of problems due to redundancy in 3NF

repetition of information (e.g., the relationship “l1, CS”)

need to use null values (e.g., to represent the relationship “l2, EE” where there is no corresponding value for s_ID).

s_IDs1

s2

s3

s1null

i_IDl1

l1

I1

l3l2

dept_nameCS

CS

CSMAEE

s_ID

s1

s2

s3

s1

i_ID

l1l1I1

l3

i_ID

l1l3l2

dept_name

CSMAEE

3 N F

B C N F

Page 28: Chapter 8:  Relational Database Design

8.28Database System Concepts - 6th Edition

Testing for 3NFTesting for 3NF

Use attribute closure to check for each dependency , if is a superkey.

If is not a superkey, we have to verify if each attribute in is contained in a candidate key of R

this test is rather more expensive, since it involve finding candidate keys

Note: We can get all the candidate keys based on the previous definition and attribute closure.

The 3NF decomposition algorithm is more complex than the BCNF decomposition algorithm.

Page 29: Chapter 8:  Relational Database Design

8.29Database System Concepts - 6th Edition

3NF Decomposition Algorithm3NF Decomposition Algorithm

Given F and R;

Result := {R };done := false;

while (not done) doif (there is a schema Ri in result that is not in 3NF) then begin let be a nontrivial functional dependency that

holds on Ri such that is not a superkey of Ri , and some attribute A in is not contained in any candidate key for R;

result := (result – Ri ) (Ri – ) (, ); endelse done := true;

Note: This algorithm does not always achieve dependency preserving. There exists an algorithm which ensures:

each relation schema Ri is in 3NF

decomposition is dependency preserving and lossless-join

Page 30: Chapter 8:  Relational Database Design

8.30Database System Concepts - 6th Edition

Example of 3NF DecompositionExample of 3NF Decomposition class (course_id, title, dept_name, credits, sec_id, semester, year,

building, room_number, capacity, time_slot_id) Functional dependencies:

course_id→ title, dept_name, credits building, room_number→capacity course_id, sec_id, semester, year→building, room_number,

time_slot_id A candidate key {course_id, sec_id, semester, year}. 3NF decomposition

course_id→ title, dept_name, credits holds but course_id is not a superkey. title (or dept_name, credits) is not contained in the candidate key

We replace class by: course(course_id, title, dept_name, credits) class-1 (course_id, sec_id, semester, year, building,

room_number, capacity, time_slot_id)

The following step is similar, and the result is the same as that of BCNF decomposition

Page 31: Chapter 8:  Relational Database Design

8.31Database System Concepts - 6th Edition

※ ※ Dependency-preserving lossless-join Dependency-preserving lossless-join decomposition into 3NFdecomposition into 3NF

Let Fc be a canonical cover for F;i := 0;for each functional dependency in Fc do

i := i + 1;Ri :=

if none of the schemas Rj, 1 j i contains a candidate key for Rthen

i := i + 1;Ri := any candidate key for R;

/* Optionally, remove redundant relations */

repeatif any schema Rj is contained in another schema Rk

then /* delete Rj */ Rj := Ri; I := i-1;

until no more Rj can be deleted

return (R1, R2, ..., Ri)

Note: This algorithm is also called Note: This algorithm is also called 3NF Synthesis Algorithm3NF Synthesis Algorithm

Page 32: Chapter 8:  Relational Database Design

8.32Database System Concepts - 6th Edition

※ ※ 3NF Decomposition: An Example3NF Decomposition: An Example

class (course_id, title, dept_name, credits, sec_id, semester, year, building, room_number, capacity, time_slot_id)

Functional dependencies: course_id→ title, dept_name, credits building, room_number→capacity course_id, sec_id, semester, year→building, room_number,

time_slot_id

→ a canonical cover ! A candidate key {course_id, sec_id, semester, year}. 3NF Decomposition:

course (course_id, title, dept_name, credits) classroom (building, room_number, capacity) section (course_id, sec_id, semester, year, building, room_number,

time_slot_id)

→ contains a candidate key of the original schema, done!

Page 33: Chapter 8:  Relational Database Design

8.33Database System Concepts - 6th Edition

Comparison of BCNF and 3NFComparison of BCNF and 3NF

It is always possible to decompose a relation into a set of relations that are in 3NF such that:

the decomposition is lossless

the dependencies are preserved

It is always possible to decompose a relation into a set of relations that are in BCNF such that:

the decomposition is lossless

it may not be possible to preserve dependencies.

Goal for a relational database design is:

BCNF.

Lossless join.

Dependency preservation.

If we cannot achieve this, we accept one of

Lack of dependency preservation

Redundancy due to use of 3NF

Page 34: Chapter 8:  Relational Database Design

8.34Database System Concepts - 6th Edition

Overall Database Design ProcessOverall Database Design Process

We have assumed schema R is given

R could have been generated when converting E-R diagram to a set of tables.

R could have been a single relation containing all attributes that are of interest (called universal relation).

R could have been the result of some ad hoc design of relations.

Normalization breaks R into smaller relations.

Page 35: Chapter 8:  Relational Database Design

8.35Database System Concepts - 6th Edition

ER Model and NormalizationER Model and Normalization

When an E-R diagram is carefully designed, identifying all entities correctly, the tables generated from the E-R diagram should not need further normalization.

However, in a real (imperfect) design, there can be functional dependencies from non-key attributes of an entity to other attributes of the entity Example: an employee entity with attributes

ID, department_name and building, and a functional dependency department_name building

The schema is employee( ID, department_name, building), which is not in 3NF or BCNF.

Good design would have made department an entity

Page 36: Chapter 8:  Relational Database Design

8.36Database System Concepts - 6th Edition

Denormalization for PerformanceDenormalization for Performance

May want to use non-normalized schema for performance

For example, displaying prereqs along with course_id and title requires join of course with prereq

Alternative 1: Use denormalized relation containing attributes of course as well as prereq with all above attributes

faster lookup

extra space and extra execution time for updates

extra coding work for programmer and possibility of error in extra code

Alternative 2: use a materialized view defined as course prereq

Benefits and drawbacks same as above, except no extra coding work for programmer and avoids possible errors

Page 37: Chapter 8:  Relational Database Design

8.37Database System Concepts - 6th Edition

Other Design IssuesOther Design Issues

Some aspects of database design are not caught by normalization

Examples of bad database design, to be avoided:

Instead of total_inst (dept_name, year, size ), use

total_inst _2007(dept_name, size ), total_inst _2008(dept_name, size ), total_inst _2009(dept_name, size ), etc.,

Above are in BCNF, but make querying across years difficult and needs new table each year

dept_year (dept_name, total_inst _2007, total_inst _2008, total_inst _2009)

Also in BCNF, but also makes querying across years difficult and requires new attribute each year.

Is an example of a crosstab, where values for one attribute become column names

Used in spreadsheets, and in data analysis tools

Page 38: Chapter 8:  Relational Database Design

8.38Database System Concepts - 6th Edition

※ ※ 2NF (2NF ( 完全函數相依完全函數相依 )) A functional dependency α → β is called a partial dependency if

there is a proper subset γ of α such that γ → β. We say that β is partially dependent on α.

A relation schema R is in second normal form (2NF) if each attribute A in R meets one of the following criteria:

It appears in a candidate key.

It is not partially dependent on a candidate key.

Page 39: Chapter 8:  Relational Database Design

8.39Database System Concepts - 6th Edition

※ ※ 3NF3NF ((不可遞移函數相依)不可遞移函數相依) Let a prime attribute be one that appears in at least one candidate

key.

Let α and β be sets of attributes such that α → β holds, but β → α does not hold. Let A be an attribute that is not in α, is not in β, and for which β → A holds. We say that A is transitively dependent on α.

A relation schema R is in 3NF with respect to a set F of functional dependencies if there are no nonprime attributes A in R for which A is transitively dependent on a key for R.