Lecture05 PDF

Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

Functional Dependencies and

Relational Schema Design


Tuesday, March 05 2019

Outline
• Functional dependencies and keys (3.5)
• Normal forms: BCNF (3.7)

1
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

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

2
Examples
EmpID Name Phone Position
E0045 Smith 1234 Clerk
E1847 John 9876 Salesrep
E1111 Smith 9876 Salesrep
E9999 Mary 1234 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)

3
Example

EmpID Name Phone Position


E0045 Smith 1234 Clerk
E1847 John 9876 Salesrep
E1111 Smith 9876 Salesrep
E9999 Mary 1234 lawyer

More Examples
Product: name price, manufacturer
Person: ssn name, age
Company: name 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.

4
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 Person(address, name, ssn)

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 buys Person

price name ssn


date

buys(name, ssn, date)

5
Finding the Keys
Except: if there is an arrow from the relationship to E, then
we don’t need the key of E as part of the relation key.

Product sname

name Purchase Store

card-no
Payment Method
Person 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)

6
Rules for FD’s

A1, A2, … A n B1, B2, … B m Splitting rule


and
Is equivalent to
Combing rule
A1, A2, … A n B1
A1, A2, … A n B2

A1, A2, … A n Bm

Rules in FD’s (continued)

A1, A2, … A n Ai Trivial Rule

Why ?

7
Rules in FD’s (continued)

Transitive Closure Rule

If A1, A2, … A n B1, B2 …, B m

and B1, B2, … B m C1, C2 …, C p

then A1, A2, … A n C1, C2 …, C p


Why ?

Closure Algorithm
Start with X={A1, …, An}.

Repeat until X doesn’t change do:

if B , B , … B C is in S, and
1 2 n
B,B,…Bn are all in X, and
1 2
C is not in X

then

add C to X.

8
Example
Book Author Author_age

Game of Thrones George R. R. Martin 66

Harry Potter J. K. Rowling 49

Dying of the Light George R. R. Martin 66

{Book} ->{Author} (if we know the book, we knows the author name)
{Author} does not ->{Book}
{Author} -> {Author_age}
Rule of transitive dependency: {Book} -> {Author_age}

- That makes sense because if we know the book name we can know the
author’s age.

Why Is the Algorithm Correct ?


• Show the following by induction:
– For every B in X:
• A1, …, An B
• 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 C
• This shows that the algorithm is sound; need to
show it is complete

9
Relational Schema Design
(or Logical Design)
Main idea:
• Start with some relational schema
• Find out its FD’s
• Use them to design a better relational
schema

Relational Schema Design


name

Conceptual Model: Product buys Person

price name ssn

Relational Model:
(plus FD’s)

Normalization:

10
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 123-321-99 (201) 555-1234


Fred 123-321-99 (206) 572-4312
Joe 909-438-44 (908) 464-0028
Joe 909-438-44 (212) 555-4000

Note: SSN no longer a key here


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

11
Relation Decomposition
Break the relation into two:
SSN Name

123-321-99 Fred
909-438-44 Joe

SSN Phone Number


123-321-99 (201) 555-1234
123-321-99 (206) 572-4312
909-438-44 (908) 464-0028
909-438-44 (212) 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 C1, C2, … C l

Such that:
B1, B2, … B m È C1, C2, … C l = A1, A2, … A n

And
-- R1 is the projection of R on B1, B2, … B m

-- R2 is the projection of R on C1, C2, … C l

12
Incorrect Decomposition
• Sometimes it is incorrect:
Name Price Category
Gizmo 19.99 Gadget
OneClick 24.99 Camera
DoubleClick 29.99 Camera

Decompose on : Name, Category and Price, Category

Incorrect Decomposition

Name Category Price Category

Gizmo Gadget 19.99 Gadget


OneClick Camera 24.99 Camera
DoubleClick Camera 29.99 Camera

Name Price Category

Gizmo 19.99 Gadget


When we put it back: OneClick 24.99 Camera
OneClick 29.99 Camera
Cannot recover information DoubleClick 24.99 Camera
DoubleClick 29.99 Camera

13
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 A , A , … A B


1 2 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.

14
Example
Name SSN Phone Number

Fred 123-321-99 (201) 555-1234


Fred 123-321-99 (206) 572-4312
Joe 909-438-44 (908) 464-0028
Joe 909-438-44 (212) 555-4000

What are the dependencies?


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

Decompose it into BCNF


SSN Name

123-321-99 Fred
909-438-44 Joe SSN Name

SSN Phone Number


123-321-99 (201) 555-1234
123-321-99 (206) 572-4312
909-438-44 (908) 464-0028
909-438-44 (212) 555-4000
SSN PhoneNumber

15
What About This?
Name Price Category

Gizmo $19.99 gadgets


OneClick $24.99 camera

Name Price, Category

It is already in BCNF

BCNF Decomposition
Find a dependency that violates the BCNF condition:
A1, A2, … A n B1, B2, … B m
Heuristics: choose B1 , B2, … Bm“as large as possible”

Decompose:
Continue until
Others A’s B’s there are no
Find a BCNF violations
2-attribute left.
relation that is
not in BCNF. R1 R2

16
Example Decomposition
Person:
Name SSN Age EyeColor PhoneNumber

Functional dependencies:
SSN Name, Age, Eye Color

BNCF: 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) A B, B C

• Key: A, D
• Violations of BCNF: A B, A C, A BC
• Pick A BC: split into R1(A,BC) R2(A,D)
• What happens if we pick A B first ?

17
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

18

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy