Chapter 7: Relational Database Design

31
Chapter 7: Relational Database Chapter 7: Relational Database Design Design

description

CS157B. Lecture 8 MVD. Chapter 7: Relational Database Design. Example R = (student_id, student_name, course_id, course_name ) F = {student_id  student_name, course_id  course_name } { student_id, course_id } is a candidate key. F c = F R 1 = ( student_id, student_name ) - PowerPoint PPT Presentation

Transcript of Chapter 7: Relational Database Design

Page 1: Chapter 7:  Relational Database Design

Chapter 7: Relational Database DesignChapter 7: Relational Database Design

Page 2: Chapter 7:  Relational Database Design

©Silberschatz, Korth and Sudarshan7.2Database System Concepts

Page 3: Chapter 7:  Relational Database Design

©Silberschatz, Korth and Sudarshan7.3Database System Concepts

Page 4: Chapter 7:  Relational Database Design

©Silberschatz, Korth and Sudarshan7.4Database System Concepts

Page 5: Chapter 7:  Relational Database Design

©Silberschatz, Korth and Sudarshan7.5Database System Concepts

ExampleR = ( student_id, student_name, course_id, course_name )

F = { student_id student_name,

course_id course_name }

{ student_id, course_id } is a candidate key.

Fc = F

R1 = ( student_id, student_name )

R2 = ( course_id, course_name )

R3 = ( student_id, course_id)

Page 6: Chapter 7:  Relational Database Design

©Silberschatz, Korth and Sudarshan7.6Database System Concepts

Page 7: Chapter 7:  Relational Database Design

©Silberschatz, Korth and Sudarshan7.7Database System Concepts

Example 2

R = ( A, B, C )

F = { A BC, B C }R is not in 3NF

Fc = { A B, B C }

Decomposition into: R1 = ( A, B ), R2 = ( B, C )

R1 and R2 are in 3NF

Page 8: Chapter 7:  Relational Database Design

©Silberschatz, Korth and Sudarshan7.8Database System Concepts

BCNF VS 3NFBCNF VS 3NF

always possible to decompose a relation into relations in 3NF and the decomposition is lossless dependencies are preserved

always possible to decompose a relation into relations in BCNF and the decomposition is lossless may not be possible to preserve dependencies

Page 9: Chapter 7:  Relational Database Design

©Silberschatz, Korth and Sudarshan7.9Database System Concepts

Candidate keys are (sid, part_id)

and (sname, part_id).

{ sid, part_id } qty{ sname, part_id } qtysid snamesname sid

The relation is in 3NF:

For sid sname, … sname is in a candidate key.

For sname sid, … sid is in a candidate key.

However, this leads to redundancy and loss of information

More ExamplesMore Examples

SSP

sid

sname part_id

qty

Page 10: Chapter 7:  Relational Database Design

©Silberschatz, Korth and Sudarshan7.10Database System Concepts

If we decompose the schema into

R1 = ( sid, sname ), R2 = ( sid, part_id, qty )These are in BCNF.

The decomposition is dependency preserving.{ sname, part_id } qty can be deduced from

(1) sname sid (given)(2) { sname, part_id } { sid, part_id } (augmentation on (1))(3) { sid, part_id } qty (given)

and finally transitivity on (2) and (3).

SSP

sid

sname part_id

qty

Page 11: Chapter 7:  Relational Database Design

©Silberschatz, Korth and Sudarshan7.11Database System Concepts

At a city, for a certain part, the supplier is unique:city part_id sid.Also, sid city

The relation is not in BCNF:sid city is not trivial, and … sid is not a superkey

It is in 3NF:sid city … city is in the candidate key of { city, part_id }.

If we decompose into ( sid, city ) and ( sid, part_id ) we have BCNF, however { city, part_id } sid will not be preserved.

SUPPLY

part_id

SUPPLY city part_id sid

city sidMore Examples

Page 12: Chapter 7:  Relational Database Design

©Silberschatz, Korth and Sudarshan7.12Database System Concepts

Design GoalsDesign Goals

Goal for a relational database design is: BCNF lossless join Dependency preservation

If we cannot achieve this, we accept: 3NF lossless join Dependency preservation

Page 13: Chapter 7:  Relational Database Design

©Silberschatz, Korth and Sudarshan7.13Database System Concepts

Page 14: Chapter 7:  Relational Database Design

©Silberschatz, Korth and Sudarshan7.14Database System Concepts

Page 15: Chapter 7:  Relational Database Design

©Silberschatz, Korth and Sudarshan7.15Database System Concepts

Multivalued DependenciesMultivalued Dependencies There are database schemas in BCNF that do not seem to be

sufficiently normalized Consider a database

classes(course, teacher, book)such that (c,t,b) classes means that t is qualified to teach c, and b is a required textbook for c

The database is supposed to list for each course the set of teachers any one of which can be the course’s instructor, and the set of books, all of which are required for the course (no matter who teaches it).

Page 16: Chapter 7:  Relational Database Design

©Silberschatz, Korth and Sudarshan7.16Database System Concepts

There are no non-trivial functional dependencies and therefore the relation is in BCNF

Insertion anomalies – i.e., if Sara is a new teacher that can teach database, two tuples need to be inserted

(database, Sara, DB Concepts)(database, Sara, Ullman)

course teacher book

databasedatabasedatabasedatabasedatabasedatabaseoperating systemsoperating systemsoperating systemsoperating systems

AviAviHankHankSudarshanSudarshanAviAvi Jim Jim

DB ConceptsUllmanDB ConceptsUllmanDB ConceptsUllmanOS ConceptsShawOS ConceptsShaw

classes

Multivalued Dependencies (Cont.)Multivalued Dependencies (Cont.)

Page 17: Chapter 7:  Relational Database Design

©Silberschatz, Korth and Sudarshan7.17Database System Concepts

Therefore, it is better to decompose classes into:

course teacher

databasedatabasedatabaseoperating systemsoperating systems

AviHankSudarshanAvi Jim

teaches

course book

databasedatabaseoperating systemsoperating systems

DB ConceptsUllmanOS ConceptsShaw

text

We shall see that these two relations are in Fourth Normal Form (4NF)

Multivalued Dependencies (Cont.)Multivalued Dependencies (Cont.)

Page 18: Chapter 7:  Relational Database Design

©Silberschatz, Korth and Sudarshan7.18Database System Concepts

Multivalued Dependencies (MVDs)Multivalued Dependencies (MVDs)

Let R be a relation schema and let R and R. The multivalued dependency

holds on R if in any legal relation r(R), for all pairs for tuples t1 and t2 in r such that t1[] = t2 [], there exist tuples t3 and t4 in r such that:

t1[] = t2 [] = t3 [] = t4 [] t3[] = t1 [] t3[R – ] = t2[R – ] t4 [] = t2[] t4[R – ] = t1[R – ]

Page 19: Chapter 7:  Relational Database Design

©Silberschatz, Korth and Sudarshan7.19Database System Concepts

Page 20: Chapter 7:  Relational Database Design

©Silberschatz, Korth and Sudarshan7.20Database System Concepts

MVD (Cont.)MVD (Cont.)

Tabular representation of

Page 21: Chapter 7:  Relational Database Design

©Silberschatz, Korth and Sudarshan7.21Database System Concepts

Page 22: Chapter 7:  Relational Database Design

©Silberschatz, Korth and Sudarshan7.22Database System Concepts

Page 23: Chapter 7:  Relational Database Design

©Silberschatz, Korth and Sudarshan7.23Database System Concepts

Page 24: Chapter 7:  Relational Database Design

©Silberschatz, Korth and Sudarshan7.24Database System Concepts

Page 25: Chapter 7:  Relational Database Design

©Silberschatz, Korth and Sudarshan7.25Database System Concepts

Page 26: Chapter 7:  Relational Database Design

©Silberschatz, Korth and Sudarshan7.26Database System Concepts

Page 27: Chapter 7:  Relational Database Design

©Silberschatz, Korth and Sudarshan7.27Database System Concepts

Page 28: Chapter 7:  Relational Database Design

©Silberschatz, Korth and Sudarshan7.28Database System Concepts

X ->> Y is trivial if

(a) Y X or

(b) Y U X = R

Page 29: Chapter 7:  Relational Database Design

©Silberschatz, Korth and Sudarshan7.29Database System Concepts

Page 30: Chapter 7:  Relational Database Design

©Silberschatz, Korth and Sudarshan7.30Database System Concepts

Page 31: Chapter 7:  Relational Database Design

©Silberschatz, Korth and Sudarshan7.31Database System Concepts