Docsity

Introduction to Database Systems-Lecture 05 Slides-Computer Science, Slides of Introduction to Database Management Systems

Toggle sidebar
bg1
1
Relational Database Design Theory
Part I
CPS 116
Introduction to Database Systems
2
Announcements
Homework #1 due this Thursday (Sept. 9) at
midnight
Course project assigned today
First milestone due September 30
Details of (optional) student presentations will be
available this Thursday
Let me know if you still do not have a Gradiance or
DB2 account
3
Motivation
How do we tell if a design is bad, e.g.,
StudentEnroll (SID, name, CID)?
This design has redundancy, because the name of a student is
recorded multiple times, once for each course the student is taking
How about a systematic approach to detecting and
removing redundancy in designs?
Dependencies, decompositions, and normal forms
SID name CID
142 Bart CPS116
142 Bart CPS114
857 Lisa CPS116
857 Lisa CPS130
... ... ...
4
Functional dependencies
A functional dependency (FD) has the form X→Y,
where Xand Yare sets of attributes in a relation R
X→Ymeans that whenever two tuples in Ragree
on all the attributes in X, they must also agree on
all attributes in Y
XYZ
abc
a??
... ... ...
XYZ
abc
ab?
... ... ...
Must be bCould be anything
5
FD examples
Address (street_address, city, state, zip)
street_address, city, state →zip
zip →city, state
zip, state →zip?
This is a trivial FD
Trivial FD: LHS ⊇RHS
zip →state, zip?
This is non-trivial, but not completely non-trivial
Completely non-trivial FD: LHS ∩RHS = ∅
6
Keys redefined using FD’s
A set of attributes Kis a key for a relation Rif
K→all (other) attributes of R
That is, Kis a “super key”
No proper subset of Ksatisfies the above condition
That is, Kis minimal
pf3
pf4
pf5

Partial preview of the text

Download Introduction to Database Systems-Lecture 05 Slides-Computer Science and more Slides Introduction to Database Management Systems in PDF only on Docsity!

Relational Database Design Theory

Part I

CPS 116

Introduction to Database Systems

Announcements

™ Homework #1 due this Thursday (Sept. 9) at

midnight

™ Course project assigned today

ƒ First milestone due September 30

™ Details of (optional) student presentations will be

available this Thursday

™ Let me know if you still do not have a Gradiance or

DB2 account

3

Motivation

™ How do we tell if a design is bad, e.g., StudentEnroll ( SID , name , CID )? ƒ This design has redundancy, because the name of a student is recorded multiple times, once for each course the student is taking

™ How about a systematic approach to detecting and removing redundancy in designs? ƒ Dependencies, decompositions, and normal forms

SID name CID 142 Bart CPS 142 Bart CPS 857 Lisa CPS 857 Lisa CPS ... ... ...

4

Functional dependencies

™ A functional dependency (FD) has the form X → Y ,

where X and Y are sets of attributes in a relation R

™ X → Y means that whenever two tuples in R agree

on all the attributes in X , they must also agree on

all attributes in Y

X Y Z a b c a?? ... ... ...

X Y Z a b c a b? Must be b (^) ... ... ... Could be anything

5

FD examples

Address ( street_address , city , state , zip )

™ street_address , city , state → zip

™ zip → city , state

™ zip , state → zip?

ƒ This is a trivial FD ƒ Trivial FD: LHS ⊇ RHS

™ zip → state , zip?

ƒ This is non-trivial, but not completely non-trivial ƒ Completely non-trivial FD: LHS ∩ RHS = ∅

6

Keys redefined using FD’s

A set of attributes K is a key for a relation R if

™ K → all (other) attributes of R

ƒ That is, K is a “super key”

™ No proper subset of K satisfies the above condition

ƒ That is, K is minimal

Reasoning with FD’s

Given a relation R and a set of FD’s F

™ Does another FD follow from F?

ƒ Are some of the FD’s in F redundant (i.e., they follow from the others)?

™ Is K a key of R?

ƒ What are all the keys of R?

Attribute closure

™ Given R , a set of FD’s F that hold in R , and a set of

attributes Z in R :

The closure of Z (denoted Z +^ ) with respect to F is

the set of all attributes functionally determined by Z

™ Algorithm for computing the closure

ƒ Start with closure = Z ƒ If X → Y is in F and X is already in the closure, then also add Y to the closure ƒ Repeat until no more attributes can be added

9

A more complex example

StudentGrade ( SID , name , email , CID , grade )

™ SID → name , email

™ email → SID

™ SID , CID → grade

™ Not a good design, and we will see why later

10

Example of computing closure

™ F includes: ƒ SID → name , email ƒ email → SID ƒ SID , CID → grade ™ { CID , email } +^ =? ™ email → SID ƒ Add SID ; closure is now { CID , email , SID } ™ SID → name , email ƒ Add name , email ; closure is now { CID , email , SID , name } ™ SID , CID → grade ƒ Add grade ; closure is now all the attributes in StudentGrade

11

Using attribute closure

Given a relation R and set of FD’s F

™ Does another FD X → Y follow from F?

ƒ Compute X +^ with respect to F ƒ If Y ⊆ X +^ , then X → Y follow from F

™ Is K a key of R?

ƒ Compute K +^ with respect to F ƒ If K +^ contains all the attributes of R , K is a super key ƒ Still need to verify that K is minimal (how?)

12

Rules of FD’s

™ Armstrong’s axioms

ƒ Reflexivity: If Y ⊆ X , then X → Y ƒ Augmentation: If X → Y , then XZ → YZ for any Z ƒ Transitivity: If X → Y and Y → Z , then X → Z

™ Rules derived from axioms

ƒ Splitting: If X → YZ , then X → Y and X → Z ƒ Combining: If X → Y and X → Z , then X → YZ

Lossless join decomposition

™ Decompose relation R into relations S and T

ƒ attrs ( R ) = attrs ( S ) ∪ attrs ( T ) ƒ S = π attrs ( S ) ( R ) ƒ T = π attrs ( T ) ( R )

™ The decomposition is a lossless join decomposition if,

given constraints such as FD’s, we can guarantee

that R = S  T

™ Any decomposition gives R ⊆ S  T (why?)

ƒ A lossy decomposition is one with R ⊂ S  T

Loss? But I got more rows!

™ “Loss” refers not to the loss of tuples, but to the loss

of information

ƒ Or, the ability to distinguish different original relations

SID CID grade 142 CPS116 B- 142 CPS114 B 123 CPS116 B+ 857 CPS116 A+ 857 CPS130 A+ 456 CPS114 C ... ... ...

SID CID 142 CPS 142 CPS 123 CPS 857 CPS 857 CPS 456 CPS ... ...

SID grade 142 B- 142 B 123 B+ 857 A+ 857 A+ 456 C ... ...

No way to tell which is the original relation

SID CID grade 142 CPS116 B 142 CPS114 B- 123 CPS116 B+ 857 CPS116 A+ 857 CPS130 A+ 456 CPS114 C ... ... ...

21

Questions about decomposition

™ When to decompose

™ How to come up with a correct decomposition (i.e.,

lossless join decomposition)

22

An answer: BCNF

™ A relation R is in Boyce-Codd Normal Form if

ƒ For every non-trivial FD X → Y in R , X is a super key ƒ That is, all FDs follow from “key → other attributes”

™ When to decompose

ƒ As long as some relation is not in BCNF

™ How to come up with a correct decomposition

ƒ Always decompose on a BCNF violation )Then it is guaranteed to be a lossless join decomposition!

23

BCNF decomposition algorithm

™ Find a BCNF violation

ƒ That is, a non-trivial FD X → Y in R where X is not a super key of R

™ Decompose R into R 1 and R 2 , where

ƒ R 1 has attributes X ∪ Y ƒ R 2 has attributes X ∪ Z , where Z contains all attributes of R that are in neither X nor Y

™ Repeat until all relations are in BCNF

24

BCNF decomposition example

StudentGrade ( SID , name , email , CID , grade ) BCNF violation: SID → name , email

Student ( SID , name , email ) Grade ( SID , CID , grade ) BCNF BCNF

Another example

StudentGrade ( SID , name , email , CID , grade ) BCNF violation: email → SID

StudentID ( email , SID ) BCNF StudentGrade’ ( email , name , CID , grade ) BCNF violation: email → name

StudentName ( email , name ) BCNF^ Grade^ ( email ,^ CID ,^ grade ) BCNF

Why is BCNF decomposition lossless

Given non-trivial X → Y in R where X is not a super

key of R , need to prove:

™ Anything we project always comes back in the join:

R ⊆ π XY ( R )  π XZ ( R )

ƒ Sure; and it doesn’t depend on the FD

™ Anything that comes back in the join must be in the

original relation:

R ⊇ π XY ( R )  π XZ ( R )

ƒ Proof makes use of the fact that X → Y

27

Recap

™ Functional dependencies: a generalization of the key

concept

™ Non-key functional dependencies: a source of

redundancy

™ BCNF decomposition: a method for removing

redundancies

ƒ BNCF decomposition is a lossless join decomposition

™ BCNF: schema in this normal form has no

redundancy due to FD’s