0% found this document useful (0 votes)
52 views

Functional Dependencies and Relational Schema Design

1. The document discusses functional dependencies and relational schema design. Functional dependencies are constraints that define relationships between attributes in a relation. 2. Normalization is the process of organizing data to minimize redundancy and dependency. It involves decomposing relations so they are in Boyce-Codd normal form (BCNF), where functional dependencies only depend on candidate keys. 3. The process of relational schema design starts with a conceptual model and finds functional dependencies to decompose the schema into BCNF to eliminate anomalies like redundancy, deletion dependencies, and update anomalies.
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views

Functional Dependencies and Relational Schema Design

1. The document discusses functional dependencies and relational schema design. Functional dependencies are constraints that define relationships between attributes in a relation. 2. Normalization is the process of organizing data to minimize redundancy and dependency. It involves decomposing relations so they are in Boyce-Codd normal form (BCNF), where functional dependencies only depend on candidate keys. 3. The process of relational schema design starts with a conceptual model and finds functional dependencies to decompose the schema into BCNF to eliminate anomalies like redundancy, deletion dependencies, and update anomalies.
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 36

Functional Dependencies and

Relational Schema Design

Relational Schema Design


Conceptual Model:

name
Product
price

Relational Model:
(plus FDs)

Normalization:

Person

buys
name

ssn

Functional Dependencies
A form of constraint (hence, part of the
schema)
Finding them is part of the database design
Also used in normalizing the relations

Outline
Functional dependencies and keys (3.4,3.5)
Normal forms: BCNF (3.6)

Functional Dependencies
Definition:
If two tuples agree on the attributes
A1, A2, A n
then they must also agree on the attributes
B1, B2, B m
Formally:

A1, A2, A n

B1, B2, B m

Main (and simplest) example: keys


How many different FDs are there?

Examples
EmpID
E0045
E1847
E1111
E9999

Name
Smith
John
Smith
Mary

Phone
1234
9876
9876
1234

Position
Clerk
Salesrep
Salesrep
lawyer

EmpID
Name, Phone, Position
Position
Phone
but Phone
Position

In General
To check A

B, erase all other columns

A B
X1
Y1
X2
Y2

check if the remaining relation is many-one


(called functional in mathematics)

Example
EmpID
E0045
E1847
E1111
E9999

Name
Smith
John
Smith
Mary

Phone
1234
9876
9876
1234

Position
Clerk
Salesrep
Salesrep
lawyer

More Examples
Product: name
Person:
ssn
Company: name

price, manufacturer
name, age
stock price, president

Key of a relation is a set of attributes that:


- functionally determines all the attributes of the relation
- none of its subsets determines all the attributes.
Superkey: a set of attributes that contains a key.

Finding the Keys of a Relation


Given a relation constructed from an E/R diagram, what is its key?
Rules:
1. If the relation comes from an entity set,
the key of the relation is the set of attributes which is the
key of the entity set.
Person(address, name, ssn)

Person
address

name

ssn

Finding the Keys


Rules:
2. If the relation comes from a many-many relationship,
the key of the relation is the set of all attribute keys in the
relations corresponding to the entity sets
name
Product

Person

buys

price

name
date

buys(name, ssn, date)

ssn

Finding the Keys


But: if there is an arrow from the relationship to E, then
we dont need the key of E as part of the relation key.
sname

Product

name

card-no

Purchase

Payment Method

Person

Store

ssn

Purchase(name , sname, ssn, card-no)

Finding the Keys


More rules:
Many-one, one-many, one-one relationships
Multi-way relationships
Weak entity sets
(Try to find them yourself, check book)

Rules for FDs


A1, A2, A n

B1, B2, B m

Is equivalent to
A1, A2, A n

B1

A1, A2, A n

B2

A1, A2, A n

Bm

Splitting rule
and
Combing rule

Rules in FDs (continued)

A1, A2, A n
Why ?

Ai

Trivial Rule

Rules in FDs (continued)


Transitive Closure Rule
If

A1, A2, A n

and

B1, B2, B m

C1, C2 , C

then

A1, A2, A n

C1, C2 , C

Why ?

B1, B2 , B

Closure of a set of Attributes


Given a set of attributes {A1, , An} and a set of dependencies S.
Problem: find all attributes B such that:
any relation which satisfies S also satisfies:
A1, , An
B
The closure of {A1, , An}, denoted {A1, , An} +,
is the set of all such attributes B

Closure Algorithm
Start with X={A1, , An}.
Repeat until X doesnt change do:
if B1, B2, B n
B,B,Bn
1

C is not in X
then
add C to X.

C is in S, and
are all in X, and

Example
A B
A D
B
A F

C
E
D
B

Closure of {A,B}:

X = {A, B,

Closure of {A, F}:

X = {A, F,

Why Is the Algorithm Correct ?


Show the following by induction:
For every B in X:
A1, , An

Initially X = {A1, , An} -- holds


Induction step: B1, , Bm in X
Implies A1, , An
B1, , Bm
We also have B1, , Bm
C
By transitivity we have A1, , An

This shows that the algorithm is sound; need to show


it is complete

Relational Schema Design


(or Logical Design)
Main idea:
Start with some relational schema
Find out its FDs
Use them to design a better relational
schema

Relational Schema Design


Conceptual Model:

name
Product
price

Relational Model:
(plus FDs)

Normalization:

Person

buys
name

ssn

Relational Schema Design


Goal: eliminate anomalies
Redundancy anomalies
Deletion anomalies
Update anomalies

Relational Schema Design


Recall set attributes (persons with several phones):
Name
SSN
Phone Number
Fred
Fred
Joe
Joe

Anomalies:

123-321-99
123-321-99
909-438-44
909-438-44

(201)
(206)
(908)
(212)

555-1234
572-4312
464-0028
555-4000

Note: SSN no longer a key here

Redundancy
= repeat data
update anomalies = need to update in many places
deletion anomalies = need to delete many tuples

Relation Decomposition
Break the relation into two:
SSN
Name
123-321-99
909-438-44
SSN
123-321-99
123-321-99
909-438-44
909-438-44

Fred
Joe
Phone Number
(201)
(206)
(908)
(212)

555-1234
572-4312
464-0028
555-4000

Decompositions in General
Let R be a relation with attributes A , A , A
1
2
n
Create two relations R1 and R2 with attributes
B1, B2, B m
Such that:
B1, B2, B m

C1, C2, C l

C1, C2, C l

And
-- R1 is the projection of R on
-- R2 is the projection of R on

A1, A2, A n

B1, B2, B m
C1, C2, C l

Incorrect Decomposition
Name

Price

Category

Gizmo

19.99

Gadget

OneClick

24.99

Camera

DoubleClick

29.99

Camera

Decompose on : Name, Category and Price, Category


Name

Category

Price

Category

Gizmo

Gadget

19.99

Gadget

OneClick

Camera

24.99

Camera

DoubleClick

Camera

29.99

Camera

When we put it back:


Cannot recover information

Name

Price

Category

Gizmo

19.99

Gadget

OneClick

24.99

Camera

OneClick

29.99

Camera

DoubleClick

24.99

Camera

DoubleClick

29.99

Camera

Normal Forms
First Normal Form = all attributes are atomic
Second Normal Form (2NF) = old and obsolete
Third Normal Form (3NF) = this lecture
Boyce Codd Normal Form (BCNF) = this lecture
Others...

Boyce-Codd Normal Form


A simple condition for removing anomalies from relations:
A relation R is in BCNF if and only if:
Whenever there is a nontrivial dependency A1, A2, A n
for R , it is the case that { A , A , A }
1
2
n
a super-key for R.

In English (though a bit vague):


Whenever a set of attributes of R is determining another attribute,
should determine all the attributes of R.

Example
Name
Fred
Fred
Joe
Joe

SSN
123-321-99
123-321-99
909-438-44
909-438-44

What are the dependencies?


SSN Name
What are the keys?
Is it in BCNF?

Phone Number
(201)
(206)
(908)
(212)

555-1234
572-4312
464-0028
555-4000

Decompose it into BCNF


SSN
123-321-99
909-438-44
SSN
123-321-99
123-321-99
909-438-44
909-438-44

Name
Fred
Joe

SSN

Phone Number
(201)
(206)
(908)
(212)

555-1234
572-4312
464-0028
555-4000

Name

What About This?


Name

Price

Category

Gizmo
OneClick

$19.99
$24.99

gadgets
camera

Name

Price, Category

BCNF Decomposition
Find a dependency that violates the BCNF condition:
A1, A2, A n

B1, B2, B m

Heuristics: choose B1 , B2, Bmas large as possible


Decompose:
Others
Find a
2-attribute
relation that is
not in BCNF.

R1

As

Bs

R2

Continue until
there are no
BCNF violations
left.

Example Decomposition
Person:
Name SSN Age EyeColor PhoneNumber

Functional dependencies:
SSN
BNCF:

Name, Age, Eye Color

Person1(SSN, Name, Age, EyeColor),


Person2(SSN, PhoneNumber)

What if we also had an attribute Draft-worthy, and the FD:


Age

Draft-worthy

Other Example
R(A,B,C,D)

B,

Key:
Violations of BCNF:
Pick
: split into R1( ) R2( )

Correct Decompositions
A decomposition is lossless if we can recover:
R(A,B,C)

R1(A,B)

R2(A,C)

R(A,B,C) = R(A,B,C)
R is in general larger than R. Must ensure R = R

You might also like