cse 303: database lecture 10 - bangladesh university...

63
CSE 303: Database Lecture 10 1 Chapter 3: Design Theory of Relational Database CSE 303: Ashikur Rahman

Upload: phungkhanh

Post on 08-Mar-2018

221 views

Category:

Documents


6 download

TRANSCRIPT

Page 1: CSE 303: Database Lecture 10 - Bangladesh University …teacher.buet.ac.bd/ashikur/CSE303/CSE_303_Lec_10_Design...CSE 303: Database Lecture 10 1 Chapter 3: Design Theory of Relational

CSE 303: Database

Lecture 10

11

Chapter 3: Design Theory of Relational Database

CSE 303: Ashikur Rahman

Page 2: CSE 303: Database Lecture 10 - Bangladesh University …teacher.buet.ac.bd/ashikur/CSE303/CSE_303_Lec_10_Design...CSE 303: Database Lecture 10 1 Chapter 3: Design Theory of Relational

Outline

1st Normal Form = all tables’ attributes are atomic

2nd Normal Form = obsolete

2

2nd Normal Form = obsolete

Boyce Codd Normal Form = will study

3rd Normal Form = see book

CSE 303: Ashikur Rahman

Page 3: CSE 303: Database Lecture 10 - Bangladesh University …teacher.buet.ac.bd/ashikur/CSE303/CSE_303_Lec_10_Design...CSE 303: Database Lecture 10 1 Chapter 3: Design Theory of Relational

First Normal Form (1NF)

• A database schema is in First Normal Form (1NF) if the domain of each attribute contains only atomic values, and the value of each attribute contains only a single

3

of each attribute contains only a single value from that domain.

CSE 303: Ashikur Rahman

Page 4: CSE 303: Database Lecture 10 - Bangladesh University …teacher.buet.ac.bd/ashikur/CSE303/CSE_303_Lec_10_Design...CSE 303: Database Lecture 10 1 Chapter 3: Design Theory of Relational

First Normal Form (1NF)

Customer ID First Name SurnameTelephone Number

123 Robert Ingram 555-861-2025

Customer

4

123 Robert Ingram 555-861-2025

456 Jane Wright 555-403-1659

789 Maria Fernandez 555-808-9633

CSE 303: Ashikur Rahman

Page 5: CSE 303: Database Lecture 10 - Bangladesh University …teacher.buet.ac.bd/ashikur/CSE303/CSE_303_Lec_10_Design...CSE 303: Database Lecture 10 1 Chapter 3: Design Theory of Relational

Not in First Normal Form (1NF)

Customer

Customer ID First Name SurnameTelephone Number

5

123 Robert Ingram 555-861-2025

456 Jane Wright555-403-1659555-776-4100

789 Maria Fernandez 555-808-9633

CSE 303: Ashikur Rahman

Page 6: CSE 303: Database Lecture 10 - Bangladesh University …teacher.buet.ac.bd/ashikur/CSE303/CSE_303_Lec_10_Design...CSE 303: Database Lecture 10 1 Chapter 3: Design Theory of Relational

Now in First Normal Form (1NF)

Customer

Customer ID First Name SurnameTelephone Number

6

123 Robert Ingram 555-861-2025

456 Jane Wright 555-403-1659

456 Jane Wright 555-776-4100

789 Maria Fernandez 555-808-9633

CSE 303: Ashikur Rahman

Page 7: CSE 303: Database Lecture 10 - Bangladesh University …teacher.buet.ac.bd/ashikur/CSE303/CSE_303_Lec_10_Design...CSE 303: Database Lecture 10 1 Chapter 3: Design Theory of Relational

Now in First Normal Form (1NF)

Customer ID First Name Surname

123 Robert Ingram

456 Jane Wright

7

789 Maria Fernandez

Customer ID Telephone Number

123 555-861-2025

456 555-403-1659

456 555-776-4100

789 555-808-9633

CSE 303: Ashikur Rahman

Page 8: CSE 303: Database Lecture 10 - Bangladesh University …teacher.buet.ac.bd/ashikur/CSE303/CSE_303_Lec_10_Design...CSE 303: Database Lecture 10 1 Chapter 3: Design Theory of Relational

First Normal Form (1NF)

A database schema is in First Normal Form if all

tables’ attributes contain only atomic values.

Student Name GPA

Alice 3.8

Student

8

Name GPA Courses

Alice 3.8

Bob 3.7

Carol 3.9

Math

DB

OS

DB

OS

Math

OS

Bob 3.7

Carol 3.9

Course

Math

DB

OS

Student Course

Alice Math

Carol Math

Alice DB

Bob DB

Alice OS

Carol OS

Takes Course

May needto add keys

CSE 303: Ashikur Rahman

Page 9: CSE 303: Database Lecture 10 - Bangladesh University …teacher.buet.ac.bd/ashikur/CSE303/CSE_303_Lec_10_Design...CSE 303: Database Lecture 10 1 Chapter 3: Design Theory of Relational

Relational Schema Design

PersonbuysProduct

name

price name ssn

Conceptual Model:

9

Relational Model (in 1NF)plus FD’s

Normalization:Eliminates anomalies

CSE 303: Ashikur Rahman

Page 10: CSE 303: Database Lecture 10 - Bangladesh University …teacher.buet.ac.bd/ashikur/CSE303/CSE_303_Lec_10_Design...CSE 303: Database Lecture 10 1 Chapter 3: Design Theory of Relational

Data Anomalies

When a database is poorly designed we get anomalies:

Redundancy: data is repeated

10

Update anomalies: need to change in several places

Delete anomalies: may lose data when we don’t want

CSE 303: Ashikur Rahman

Page 11: CSE 303: Database Lecture 10 - Bangladesh University …teacher.buet.ac.bd/ashikur/CSE303/CSE_303_Lec_10_Design...CSE 303: Database Lecture 10 1 Chapter 3: Design Theory of Relational

Relational Schema DesignRecall set attributes (persons with several phones):

Name SSN PhoneNumber City

Fred 123-45-6789 206-555-1234 Seattle

Fred 123-45-6789 206-555-6543 Seattle

11

Anomalies:• Redundancy = repeated data• Update anomalies = Fred moves to “Bellevue”• Deletion anomalies = Joe deletes his phone number:

what is his city ?

Joe 987-65-4321 908-555-2121 Westfield

One person may have multiple phones, but lives in only one city

CSE 303: Ashikur Rahman

Page 12: CSE 303: Database Lecture 10 - Bangladesh University …teacher.buet.ac.bd/ashikur/CSE303/CSE_303_Lec_10_Design...CSE 303: Database Lecture 10 1 Chapter 3: Design Theory of Relational

Relation DecompositionBreak the relation into two:

Name SSN PhoneNumber City

Fred 123-45-6789 206-555-1234 Seattle

Fred 123-45-6789 206-555-6543 Seattle

Joe 987-65-4321 908-555-2121 Westfield

12

Name SSN City

Fred 123-45-6789 Seattle

Joe 987-65-4321 Westfield

SSN PhoneNumber

123-45-6789 206-555-1234

123-45-6789 206-555-6543

987-65-4321 908-555-2121

Joe 987-65-4321 908-555-2121 Westfield

Anomalies are gone:• No more repeated data• Easy to move Fred to “Bellevue” (how?)• Easy to delete all Joe’s phone numbers (how?)

CSE 303: Ashikur Rahman

Page 13: CSE 303: Database Lecture 10 - Bangladesh University …teacher.buet.ac.bd/ashikur/CSE303/CSE_303_Lec_10_Design...CSE 303: Database Lecture 10 1 Chapter 3: Design Theory of Relational

Relational Schema Design(or Logical Design)

Main idea:

• Start with some relational schema

• Find out its functional dependencies

13

• Find out its functional dependencies

• Use them to design a better relational schema

CSE 303: Ashikur Rahman

Page 14: CSE 303: Database Lecture 10 - Bangladesh University …teacher.buet.ac.bd/ashikur/CSE303/CSE_303_Lec_10_Design...CSE 303: Database Lecture 10 1 Chapter 3: Design Theory of Relational

Functional Dependencies

• A form of constraint

– hence, part of the schema

• Finding them is part of the database design

14

• Finding them is part of the database design

• Also used in normalizing the relations

CSE 303: Ashikur Rahman

Page 15: CSE 303: Database Lecture 10 - Bangladesh University …teacher.buet.ac.bd/ashikur/CSE303/CSE_303_Lec_10_Design...CSE 303: Database Lecture 10 1 Chapter 3: Design Theory of Relational

Functional Dependencies

Definition:

If two tuples agree on the attributes

A1, A2, …, An

15

then they must also agree on the attributes

Formally:

A1, A2, …, An B1, B2, …, Bm

1 2 nA1, A2, …, An

B1, B2, …, Bm

CSE 303: Ashikur Rahman

Page 16: CSE 303: Database Lecture 10 - Bangladesh University …teacher.buet.ac.bd/ashikur/CSE303/CSE_303_Lec_10_Design...CSE 303: Database Lecture 10 1 Chapter 3: Design Theory of Relational

When Does an FD Hold

Definition: A1, ..., Am B1, ..., Bn holds in R if:

"t, t’ R, (t.A1=t’.A1 ... t.Am=t’.Am t.B1=t’.B1 ... t.Bn=t’.Bn )

R

16

A1 ... Am B1 ... Bm

if t, t’ agree here then t, t’ agree here

t

t’

R

CSE 303: Ashikur Rahman

Page 17: CSE 303: Database Lecture 10 - Bangladesh University …teacher.buet.ac.bd/ashikur/CSE303/CSE_303_Lec_10_Design...CSE 303: Database Lecture 10 1 Chapter 3: Design Theory of Relational

Title Year Length Genre StudioName StarName

Star Wars 1977 124 SciFi Fox Carrie Fisher

Star Wars 1977 124 SciFi Fox Mark Hamill

Star Wars 1977 124 SciFi Fox Harrison Ford

Gone With the Wind 1939 231 Drama MGM Vivien Leigh

Example: Movie table

17

Gone With the Wind 1939 231 Drama MGM Vivien Leigh

Wayne’s World 1992 95 Comedy Paramount Dana Carvey

Wayne’s World 1992 95 Comedy Paramount Mike Meyers

title, year length

title, year length, genre, studioNametitle, year genre

title, year studioNameCSE 303: Ashikur Rahman

Page 18: CSE 303: Database Lecture 10 - Bangladesh University …teacher.buet.ac.bd/ashikur/CSE303/CSE_303_Lec_10_Design...CSE 303: Database Lecture 10 1 Chapter 3: Design Theory of Relational

Title Year Length Genre StudioName StarName

Star Wars 1977 124 SciFi Fox Carrie Fisher

Star Wars 1977 124 SciFi Fox Mark Hamill

Star Wars 1977 124 SciFi Fox Harrison Ford

Gone With the Wind 1939 231 Drama MGM Vivien Leigh

Example: Movie table

18

Gone With the Wind 1939 231 Drama MGM Vivien Leigh

Wayne’s World 1992 95 Comedy Paramount Dana Carvey

Wayne’s World 1992 95 Comedy Paramount Mike Meyers

title, year starName

How about?

CSE 303: Ashikur Rahman

Page 19: CSE 303: Database Lecture 10 - Bangladesh University …teacher.buet.ac.bd/ashikur/CSE303/CSE_303_Lec_10_Design...CSE 303: Database Lecture 10 1 Chapter 3: Design Theory of Relational

ExamplesAn FD holds, or does not hold on an instance:

EmpID Name Phone Position

E0045 Smith 1234 Clerk

E3542 Mike 9876 Salesrep

19

EmpID Name, Phone, Position

E3542 Mike 9876 Salesrep

E1111 Smith 9876 Salesrep

E9999 Mary 1234 Lawyer

but not Name EmpID

or Name PhoneCSE 303: Ashikur Rahman

Page 20: CSE 303: Database Lecture 10 - Bangladesh University …teacher.buet.ac.bd/ashikur/CSE303/CSE_303_Lec_10_Design...CSE 303: Database Lecture 10 1 Chapter 3: Design Theory of Relational

Example

EmpID Name Phone Position

E0045 Smith 1234 Clerk

20

Position Phone

E3542 Mike 9876 Salesrep

E1111 Smith 9876 Salesrep

E9999 Mary 1234 Lawyer

CSE 303: Ashikur Rahman

Page 21: CSE 303: Database Lecture 10 - Bangladesh University …teacher.buet.ac.bd/ashikur/CSE303/CSE_303_Lec_10_Design...CSE 303: Database Lecture 10 1 Chapter 3: Design Theory of Relational

Example

EmpID Name Phone Position

E0045 Smith 1234 Clerk

E3542 Mike 9876 Salesrep

21

E3542 Mike 9876 Salesrep

E1111 Smith 9876 Salesrep

E9999 Mary 1234 Lawyer

but not Phone Position

CSE 303: Ashikur Rahman

Page 22: CSE 303: Database Lecture 10 - Bangladesh University …teacher.buet.ac.bd/ashikur/CSE303/CSE_303_Lec_10_Design...CSE 303: Database Lecture 10 1 Chapter 3: Design Theory of Relational

name category color department price

Gizmo Gadget Green Toys 49

Inferring other dependencies from a set of FDs

22

Tweaker Gadget Black Toys 99

Gizmo Stationary Green Office-supp. 59

name colorcategory departmentcolor, category price

name, category price

CSE 303: Ashikur Rahman

Page 23: CSE 303: Database Lecture 10 - Bangladesh University …teacher.buet.ac.bd/ashikur/CSE303/CSE_303_Lec_10_Design...CSE 303: Database Lecture 10 1 Chapter 3: Design Theory of Relational

Armstrong’s Rules (1/3)

Is equivalent to

Splitting ruleand

A1, A2, …, An B1, B2, …, Bm

23

Is equivalent toand

Combing rule

A1 ... Am B1 ... Bm

1 2 n m

A1, A2, …, An B1

A1, A2, …, An B2

. . . . .A1, A2, …, An Bm

CSE 303: Ashikur Rahman

Page 24: CSE 303: Database Lecture 10 - Bangladesh University …teacher.buet.ac.bd/ashikur/CSE303/CSE_303_Lec_10_Design...CSE 303: Database Lecture 10 1 Chapter 3: Design Theory of Relational

Armstrong’s Rules (2/3)

Trivial Rule

where i = 1, 2, ..., n

A1, A2, …, An Ai

24

Why ?

A1 … Am

where i = 1, 2, ..., n

CSE 303: Ashikur Rahman

Page 25: CSE 303: Database Lecture 10 - Bangladesh University …teacher.buet.ac.bd/ashikur/CSE303/CSE_303_Lec_10_Design...CSE 303: Database Lecture 10 1 Chapter 3: Design Theory of Relational

Armstrong’s Rules (3/3)

Transitive Closure Rule

25

If

and

then

Why ?

A1, A2, …, An B1, B2, …, Bm

B1, B2, …, Bm C1, C2, …, Cp

A1, A2, …, An C1, C2, …, Cp

CSE 303: Ashikur Rahman

Page 26: CSE 303: Database Lecture 10 - Bangladesh University …teacher.buet.ac.bd/ashikur/CSE303/CSE_303_Lec_10_Design...CSE 303: Database Lecture 10 1 Chapter 3: Design Theory of Relational

A1 … Am B1 … Bm C1 ... Cp

26CSE 303: Ashikur Rahman

Page 27: CSE 303: Database Lecture 10 - Bangladesh University …teacher.buet.ac.bd/ashikur/CSE303/CSE_303_Lec_10_Design...CSE 303: Database Lecture 10 1 Chapter 3: Design Theory of Relational

name category color department price

Gizmo Gadget Green Toys 49

Inferring other dependencies from a set of FDs

27

Tweaker Gadget Black Toys 99

Gizmo Stationary Green Office-supp. 59

name colorcategory departmentcolor, category price

name, category price

CSE 303: Ashikur Rahman

Page 28: CSE 303: Database Lecture 10 - Bangladesh University …teacher.buet.ac.bd/ashikur/CSE303/CSE_303_Lec_10_Design...CSE 303: Database Lecture 10 1 Chapter 3: Design Theory of Relational

Example (continued)Start from the following FDs:

Infer the following FDs:

1. name color2. category department3. color, category price

THIS IS TOO HARD! Let’s see an easier way.

name, category price

28

Infer the following FDs:

Inferred FDWhich Ruledid we apply ?

4. name, category name

5. name, category color

6. name, category category

7. name, category color, category

8. name, category price

Trivial

Transitive 1, 4

Transitive 7, 3

Trivial

Split/combine

Page 29: CSE 303: Database Lecture 10 - Bangladesh University …teacher.buet.ac.bd/ashikur/CSE303/CSE_303_Lec_10_Design...CSE 303: Database Lecture 10 1 Chapter 3: Design Theory of Relational

Closure of a set of Attributes

Given a set of attributes A1, …, An and a set of FDs

29

The closure, {A1, …, An}+ = the set of attributes B

s.t. A1, …, An B

CSE 303: Ashikur Rahman

Page 30: CSE 303: Database Lecture 10 - Bangladesh University …teacher.buet.ac.bd/ashikur/CSE303/CSE_303_Lec_10_Design...CSE 303: Database Lecture 10 1 Chapter 3: Design Theory of Relational

name category color department price

Gizmo Gadget Green Toys 49

Tweaker Gadget Black Toys 99

Gizmo Stationary Green Office-supp. 59

Closures Example

CSE 303: Ashikur Rahman 30

Gizmo Stationary Green Office-supp. 59

name colorcategory departmentcolor, category price

Example:

Closures:name+ = {name, color}{name, category}+ = {name, category, color, department, price}color+ = {color}

Page 31: CSE 303: Database Lecture 10 - Bangladesh University …teacher.buet.ac.bd/ashikur/CSE303/CSE_303_Lec_10_Design...CSE 303: Database Lecture 10 1 Chapter 3: Design Theory of Relational

Closure Algorithm

X={A1, …, An}.

Repeat until X doesn’t change do: name colorcategory departmentcolor, category price

Example:

31

if B1, …, Bn C is a FD andB1, …, Bn are all in X

then add C to X.

{name, category}+ = { }

color, category price

name, category, color, department, price

Hence: name, category color, department, price

Page 32: CSE 303: Database Lecture 10 - Bangladesh University …teacher.buet.ac.bd/ashikur/CSE303/CSE_303_Lec_10_Design...CSE 303: Database Lecture 10 1 Chapter 3: Design Theory of Relational

More Examples

R(A,B,C,D,E,F) A, B CB, C A, DD E

In class:

32

Compute {A,B}+ X = {A, B, }

Compute {A, F}+ X = {A, F, }

D EC, F B

CSE 303: Ashikur Rahman

Page 33: CSE 303: Database Lecture 10 - Bangladesh University …teacher.buet.ac.bd/ashikur/CSE303/CSE_303_Lec_10_Design...CSE 303: Database Lecture 10 1 Chapter 3: Design Theory of Relational

More Examples

R(A,B,C,D,E,F) A, B CB, C A, DD E

In class:

33

Compute {A,B}+ X = {A, B, C, D, E}

Compute {A, F}+ X = {A, F}

D EC, F B

CSE 303: Ashikur Rahman

Page 34: CSE 303: Database Lecture 10 - Bangladesh University …teacher.buet.ac.bd/ashikur/CSE303/CSE_303_Lec_10_Design...CSE 303: Database Lecture 10 1 Chapter 3: Design Theory of Relational

Does a new FD logically follows from a set of FD

Application of Closures

34

Inferring All FDs that logically follows from a set of FD

Finding all keys and superkeys

CSE 303: Ashikur Rahman

Page 35: CSE 303: Database Lecture 10 - Bangladesh University …teacher.buet.ac.bd/ashikur/CSE303/CSE_303_Lec_10_Design...CSE 303: Database Lecture 10 1 Chapter 3: Design Theory of Relational

(1) Does a new FD logically follows from a set of FDs?

R(A1, A2, ….., Am)

Set of FDs A new FD

35

Set of FDs

• To check if X A (new FD)

– Using given set of FDs Compute X+ i.e., {left side}+

– Check if A X+

CSE 303: Ashikur Rahman

Page 36: CSE 303: Database Lecture 10 - Bangladesh University …teacher.buet.ac.bd/ashikur/CSE303/CSE_303_Lec_10_Design...CSE 303: Database Lecture 10 1 Chapter 3: Design Theory of Relational

Example

R(A,B,C,D) A, B CB, C DC, D A

A, D C

36

1. Compute {A,D}+

2. If it contains C then the new FD logically follows

C, D AA, D B

CSE 303: Ashikur Rahman

Page 37: CSE 303: Database Lecture 10 - Bangladesh University …teacher.buet.ac.bd/ashikur/CSE303/CSE_303_Lec_10_Design...CSE 303: Database Lecture 10 1 Chapter 3: Design Theory of Relational

Example

R(A,B,C,D)

B, D AA, B CB, C DC, D A

37

1. Compute {B, D}+ = {B, D}

2. A is not a member of the set, hence it doesn’t logically follow

CSE 303: Ashikur Rahman

C, D AA, D B

Page 38: CSE 303: Database Lecture 10 - Bangladesh University …teacher.buet.ac.bd/ashikur/CSE303/CSE_303_Lec_10_Design...CSE 303: Database Lecture 10 1 Chapter 3: Design Theory of Relational

Using Closure to Infer ALL FDs

A, B CA, D BB D

Example:

Step 1: Compute X+, for every X {A,B,C,D}:

38

Step 1: Compute X+, for every X {A,B,C,D}:

A+ = A, B+ = BD, C+ = C, D+ = DAB+ =ABCD, AC+=AC, AD+=ABCD,

BC+=BCD, BD+=BD, CD+=CDABC+ = ABD+ = ACD+ = ABCD (no need to compute– why ?)BCD+ = BCD, ABCD+ = ABCD

Step 2: Enumerate all FD’s X Y, s.t. Y X+ and XY = :

AB CD, ADBC, BC D, ABC D, ABD C, ACD B

Page 39: CSE 303: Database Lecture 10 - Bangladesh University …teacher.buet.ac.bd/ashikur/CSE303/CSE_303_Lec_10_Design...CSE 303: Database Lecture 10 1 Chapter 3: Design Theory of Relational

Another Example

• Enrollment(student, major, course, room, time)

student major

major, course room

course time

39

course time

What else can we infer ? [in class, or at home]

CSE 303: Ashikur Rahman

Page 40: CSE 303: Database Lecture 10 - Bangladesh University …teacher.buet.ac.bd/ashikur/CSE303/CSE_303_Lec_10_Design...CSE 303: Database Lecture 10 1 Chapter 3: Design Theory of Relational

Application of Closure: Finding Keys

• A superkey is a set of attributes A1, ..., An s.t. for any other attribute B, we have A1, ..., An B

40

• A key is a minimal superkey

– i.e. set of attributes which is a superkey and for which no subset is a superkey

CSE 303: Ashikur Rahman

Page 41: CSE 303: Database Lecture 10 - Bangladesh University …teacher.buet.ac.bd/ashikur/CSE303/CSE_303_Lec_10_Design...CSE 303: Database Lecture 10 1 Chapter 3: Design Theory of Relational

How many Superkeys?

Suppose R is a relation with attributes A1, A2,….An. As a function of n, tell how many superkeys R has, if:

CSE 303: Ashikur Rahman 41

a) The only key is A1

b) The only keys are A1 and A2

Page 42: CSE 303: Database Lecture 10 - Bangladesh University …teacher.buet.ac.bd/ashikur/CSE303/CSE_303_Lec_10_Design...CSE 303: Database Lecture 10 1 Chapter 3: Design Theory of Relational

How many Superkeys?

Suppose R is a relation with attributes A1, A2,….An. As a function of n, tell how many superkeys R has, if:

n-1

CSE 303: Ashikur Rahman 42

a) The only key is A1 2n-1

b) The only keys are A1 and A2 3.2n-2

Page 43: CSE 303: Database Lecture 10 - Bangladesh University …teacher.buet.ac.bd/ashikur/CSE303/CSE_303_Lec_10_Design...CSE 303: Database Lecture 10 1 Chapter 3: Design Theory of Relational

Computing (Super)Keys

• Compute X+ for all sets X

• If X+ = all attributes, then X is a key

• List only the minimal X’s

43

• List only the minimal X’s

CSE 303: Ashikur Rahman

Page 44: CSE 303: Database Lecture 10 - Bangladesh University …teacher.buet.ac.bd/ashikur/CSE303/CSE_303_Lec_10_Design...CSE 303: Database Lecture 10 1 Chapter 3: Design Theory of Relational

Example 1

R(A,B,C,D) A, B CB, C DC, D AA, D B

CSE 303: Ashikur Rahman 44

What are all the keys?

What are all the superkeys that are not keys?

Page 45: CSE 303: Database Lecture 10 - Bangladesh University …teacher.buet.ac.bd/ashikur/CSE303/CSE_303_Lec_10_Design...CSE 303: Database Lecture 10 1 Chapter 3: Design Theory of Relational

Example

R(A,B,C,D) A, B CB, C DC, D AA, D B

Keys:AB , AD, BC, CD

CSE 303: Ashikur Rahman 45

What are all the keys?

What are all the superkeys that are not keys?

A+ = A, B+ = B, C+ = C, D+ = DAB+ =ABCD, AC+=AC, AD+=ABCD,

BC+=ABCD, BD+=BD, CD+=ABCDABC+ = ABD+ = ACD+ = ABCD (no need to compute– why ?)BCD+ = ABCD, ABCD+ = ABCD

Page 46: CSE 303: Database Lecture 10 - Bangladesh University …teacher.buet.ac.bd/ashikur/CSE303/CSE_303_Lec_10_Design...CSE 303: Database Lecture 10 1 Chapter 3: Design Theory of Relational

Example

R(A,B,C,D) A, B CB, C DC, D AA, D B

Superkeys that are not Keys:ABC , ABD, BCD, ACD, ABCD

CSE 303: Ashikur Rahman 46

What are all the keys?

What are all the superkeys that are not keys?

A+ = A, B+ = B, C+ = C, D+ = DAB+ =ABCD, AC+=AC, AD+=ABCD,

BC+=ABCD, BD+=BD, CD+=ABCDABC+ = ABD+ = ACD+ = ABCD (no need to compute– why ?)BCD+ = ABCD, ABCD+ = ABCD

Page 47: CSE 303: Database Lecture 10 - Bangladesh University …teacher.buet.ac.bd/ashikur/CSE303/CSE_303_Lec_10_Design...CSE 303: Database Lecture 10 1 Chapter 3: Design Theory of Relational

Example 2

A, B CA, D BB D

Keys:AB , AD

CSE 303: Ashikur Rahman 47

What are all the keys?

What are all the superkeys that are not keys?

A+ = A, B+ = BD, C+ = C, D+ = DAB+ =ABCD, AC+=AC, AD+=ABCD,

BC+=BCD, BD+=BD, CD+=CDABC+ = ABD+ = ACD+ = ABCD (no need to compute– why ?)BCD+ = BCD, ABCD+ = ABCD

Page 48: CSE 303: Database Lecture 10 - Bangladesh University …teacher.buet.ac.bd/ashikur/CSE303/CSE_303_Lec_10_Design...CSE 303: Database Lecture 10 1 Chapter 3: Design Theory of Relational

Example 2

A, B CA, D BB D

Superkeys that are not Keys:ABC , ABD, ACD, ABCD

CSE 303: Ashikur Rahman 48

What are all the keys?

What are all the superkeys that are not keys?

A+ = A, B+ = BD, C+ = C, D+ = DAB+ =ABCD, AC+=AC, AD+=ABCD,

BC+=BCD, BD+=BD, CD+=CDABC+ = ABD+ = ACD+ = ABCD (no need to compute– why ?)BCD+ = BCD, ABCD+ = ABCD

Page 49: CSE 303: Database Lecture 10 - Bangladesh University …teacher.buet.ac.bd/ashikur/CSE303/CSE_303_Lec_10_Design...CSE 303: Database Lecture 10 1 Chapter 3: Design Theory of Relational

Example

Product(name, price, category, color)

name, category price

49

category color

What is the key ?

CSE 303: Ashikur Rahman

Page 50: CSE 303: Database Lecture 10 - Bangladesh University …teacher.buet.ac.bd/ashikur/CSE303/CSE_303_Lec_10_Design...CSE 303: Database Lecture 10 1 Chapter 3: Design Theory of Relational

Example

Product(name, price, category, color)

name, category price

50

category color

What is the key ?

(name, category) + = name, category, price, color

Hence (name, category) is a keyCSE 303: Ashikur Rahman

Page 51: CSE 303: Database Lecture 10 - Bangladesh University …teacher.buet.ac.bd/ashikur/CSE303/CSE_303_Lec_10_Design...CSE 303: Database Lecture 10 1 Chapter 3: Design Theory of Relational

Examples of Keys

Enrollment(student, address, course, room, time)

student address

51

room, time course

student, course room, time

(find keys at home)

CSE 303: Ashikur Rahman

Page 52: CSE 303: Database Lecture 10 - Bangladesh University …teacher.buet.ac.bd/ashikur/CSE303/CSE_303_Lec_10_Design...CSE 303: Database Lecture 10 1 Chapter 3: Design Theory of Relational

Key or Keys ?

Can we have more than one key ?

Given R(A,B,C) define FD’s s.t. there are two keys

52

Given R(A,B,C) define FD’s s.t. there are two keys

ABCBCA

ABCBAC

or

what are the keys here ?

Can you design FDs such that there are three keys ?

Page 53: CSE 303: Database Lecture 10 - Bangladesh University …teacher.buet.ac.bd/ashikur/CSE303/CSE_303_Lec_10_Design...CSE 303: Database Lecture 10 1 Chapter 3: Design Theory of Relational

Eliminating Anomalies

Main idea:

• X A is OK if X is a (super)key

53

• X A is OK if X is a (super)key

• X A is not OK otherwise

CSE 303: Ashikur Rahman

Page 54: CSE 303: Database Lecture 10 - Bangladesh University …teacher.buet.ac.bd/ashikur/CSE303/CSE_303_Lec_10_Design...CSE 303: Database Lecture 10 1 Chapter 3: Design Theory of Relational

Example

Name SSN PhoneNumber City

Fred 123-45-6789 206-555-1234 Seattle

Fred 123-45-6789 206-555-6543 Seattle

Joe 987-65-4321 908-555-2121 Westfield

54

What is the key?{SSN, PhoneNumber}

Joe 987-65-4321 908-555-2121 Westfield

Joe 987-65-4321 908-555-1234 Westfield

SSN Name, City

Hence SSN Name, Cityis a “bad” dependency

CSE 303: Ashikur Rahman

Page 55: CSE 303: Database Lecture 10 - Bangladesh University …teacher.buet.ac.bd/ashikur/CSE303/CSE_303_Lec_10_Design...CSE 303: Database Lecture 10 1 Chapter 3: Design Theory of Relational

Boyce-Codd Normal Form (BCNF)

A simple condition for removing anomalies from relations:

A relation R is in BCNF if:

If A , ..., A B is a non-trivial dependency

55

In other words: there are no “bad” FDs, that is the left side of every nontrivial FD must be a super key.

If A1, ..., An B is a non-trivial dependency

in R, then {A1, ..., An} is a superkey for R

Equivalently: " X, either (X+ = X) or (X+ = all attributes)

CSE 303: Ashikur Rahman

Page 56: CSE 303: Database Lecture 10 - Bangladesh University …teacher.buet.ac.bd/ashikur/CSE303/CSE_303_Lec_10_Design...CSE 303: Database Lecture 10 1 Chapter 3: Design Theory of Relational

All two-attribute relations are in BCNF

Case 1: Suppose there is no dependency

Nothing is being violated, so fine

Case 2: Suppose A B is the only dependency

A+ = AB, so left side of A B is a key

R(A,B)

CSE 303: Ashikur Rahman 56

A = AB, so left side of A B is a key

Case 3: Suppose B A is the only dependency

B+ = AB, so left side of B A is a key

Case 4: Suppose A B and B A are two dependencies

A+ = AB, B+ = AB,

so left side of B A is a key and left side of AB is also a key

Page 57: CSE 303: Database Lecture 10 - Bangladesh University …teacher.buet.ac.bd/ashikur/CSE303/CSE_303_Lec_10_Design...CSE 303: Database Lecture 10 1 Chapter 3: Design Theory of Relational

BCNF Decomposition Algorithm

repeatchoose A1, …, Am B1, …, Bn that violates BCNF split R into R1(A1, …, Am, B1, …, Bn) and R2(A1, …, Am, [others])continue with both R1 and R2

until no more violations

57

A’s OthersB’s

R1

until no more violations

R2

In practice, we havea better algorithm (coming up)

Page 58: CSE 303: Database Lecture 10 - Bangladesh University …teacher.buet.ac.bd/ashikur/CSE303/CSE_303_Lec_10_Design...CSE 303: Database Lecture 10 1 Chapter 3: Design Theory of Relational

Example

Name SSN PhoneNumber City

Fred 123-45-6789 206-555-1234 Seattle

Fred 123-45-6789 206-555-6543 Seattle

Joe 987-65-4321 908-555-2121 Westfield

58

What is the key?

Joe 987-65-4321 908-555-2121 Westfield

Joe 987-65-4321 908-555-1234 Westfield

SSN Name, City use SSN Name, Cityto split

{SSN, PhoneNumber}

CSE 303: Ashikur Rahman

R1(SSN, Name, City)R2(SSN, PhoneNumber)

Page 59: CSE 303: Database Lecture 10 - Bangladesh University …teacher.buet.ac.bd/ashikur/CSE303/CSE_303_Lec_10_Design...CSE 303: Database Lecture 10 1 Chapter 3: Design Theory of Relational

Example

Name SSN City

Fred 123-45-6789 Seattle

Joe 987-65-4321 Westfield

SSN Name, City

59

SSN PhoneNumber

123-45-6789 206-555-1234

123-45-6789 206-555-6543

987-65-4321 908-555-2121

987-65-4321 908-555-1234

Let’s check anomalies:• Redundancy ?• Update ?• Delete ?

CSE 303: Ashikur Rahman

Page 60: CSE 303: Database Lecture 10 - Bangladesh University …teacher.buet.ac.bd/ashikur/CSE303/CSE_303_Lec_10_Design...CSE 303: Database Lecture 10 1 Chapter 3: Design Theory of Relational

BCNF Decomposition Algorithm

BCNF_Decompose(R)

find X s.t.: X ≠X+ ≠ [all attributes]

60

if (not found) then “R is in BCNF”

let Y = X+ - Xlet Z = [all attributes] - X+

decompose R into R1(X Y) and R2(X Z)continue to decompose recursively R1 and R2

CSE 303: Ashikur Rahman

Page 61: CSE 303: Database Lecture 10 - Bangladesh University …teacher.buet.ac.bd/ashikur/CSE303/CSE_303_Lec_10_Design...CSE 303: Database Lecture 10 1 Chapter 3: Design Theory of Relational

ExampleA BB C

R(A,B,C,D)A+ = ABC ≠ ABCD

R(A,B,C,D) Find X s.t.: X ≠X+ ≠ [all attributes]

61

What arethe keys ?

R1(A,B,C)B+ = BC ≠ ABC

R2(A,D)

R11(B,C) R12(A,B)

CSE 303: Ashikur Rahman

Page 62: CSE 303: Database Lecture 10 - Bangladesh University …teacher.buet.ac.bd/ashikur/CSE303/CSE_303_Lec_10_Design...CSE 303: Database Lecture 10 1 Chapter 3: Design Theory of Relational

Example2: BCNF DecompositionPerson(name, SSN, age, hairColor, phoneNumber)

SSN name, ageage hairColor

Iteration 1: PersonSSN+ = SSN, name, age, hairColor

Find X s.t.: X ≠X+ ≠ [all attributes]

62

SSN+ = SSN, name, age, hairColorDecompose into: P(SSN, name, age, hairColor)

Phone(SSN, phoneNumber)Iteration 2: PSSN+ = SSN, name, age, hairColorage+ = age, hairColorDecompose: People(SSN, name, age)

Hair(age, hairColor)Phone(SSN, phoneNumber)

What arethe keys ?

Page 63: CSE 303: Database Lecture 10 - Bangladesh University …teacher.buet.ac.bd/ashikur/CSE303/CSE_303_Lec_10_Design...CSE 303: Database Lecture 10 1 Chapter 3: Design Theory of Relational

Decompositions in General

R(A1, ..., An, B1, ..., Bm, C1, ..., Cp)

R1(A1, ..., An, B1, ..., Bm) R2(A1, ..., An, C1, ..., Cp)

63

R1 = projection of R on A1, ..., An, B1, ..., Bm

R2 = projection of R on A1, ..., An, C1, ..., Cp

R1(A1, ..., An, B1, ..., Bm) R2(A1, ..., An, C1, ..., Cp)

CSE 303: Ashikur Rahman