Top Banner
CS 3630 Database Design and Implementation
21

CS 3630 Database Design and Implementation. Unnormalized Form (UNF) student courses John CS363 CS334 CS323 Multi-Value attribute Common in reports 2.

Dec 13, 2015

Download

Documents

Anne Barton
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CS 3630 Database Design and Implementation. Unnormalized Form (UNF) student courses John CS363 CS334 CS323 Multi-Value attribute Common in reports 2.

CS 3630 Database Design and Implementation

Page 2: CS 3630 Database Design and Implementation. Unnormalized Form (UNF) student courses John CS363 CS334 CS323 Multi-Value attribute Common in reports 2.

Unnormalized Form (UNF)

student courses

John CS363

CS334

CS323

• Multi-Value attribute

• Common in reports

2

Page 3: CS 3630 Database Design and Implementation. Unnormalized Form (UNF) student courses John CS363 CS334 CS323 Multi-Value attribute Common in reports 2.

First Normal Form (1NF)

• No multi-value attributes

• Done when mapping E-R model to relational schema

• DBDL

3

Page 4: CS 3630 Database Design and Implementation. Unnormalized Form (UNF) student courses John CS363 CS334 CS323 Multi-Value attribute Common in reports 2.

Second Normal Form (2NF)

A relation R is in 1NF, and

every non-primary-key attribute is fully functionally dependent on the primary key

Then R is in 2NF

No Partial FDs on the PK.

4

Page 5: CS 3630 Database Design and Implementation. Unnormalized Form (UNF) student courses John CS363 CS334 CS323 Multi-Value attribute Common in reports 2.

5

Partial FDs

X, Y W is partial (W partially depends on X or Y) if

X W or Y W is true

Example:

Sno, Bno staffAddress, staffPhone staffAddress and staffPhone are Partially depend on Sno,

since the following is true

Sno staffAddress, staffPhone

Page 6: CS 3630 Database Design and Implementation. Unnormalized Form (UNF) student courses John CS363 CS334 CS323 Multi-Value attribute Common in reports 2.

6

Full FDs

A FD is a full FD if it’s not partial.

A, B X, Y is a full FD if both of the following are false: A X, Y B X, Y

Sno staffAddress, staffPhone

Cannot be partial!

Page 7: CS 3630 Database Design and Implementation. Unnormalized Form (UNF) student courses John CS363 CS334 CS323 Multi-Value attribute Common in reports 2.

7

Full FDs

A FD is a full FD if it’s not partial.

Pno, Start Rno, Rent, Finish

Both of the following are false: Pno Rno, Rent, Finish Start Rno, Rent, Finish

Page 8: CS 3630 Database Design and Implementation. Unnormalized Form (UNF) student courses John CS363 CS334 CS323 Multi-Value attribute Common in reports 2.

Second Normal Form (2NF) No Partial FDs on the PK.

PK ---> All other attributes

Assume a table with a simple PK of Att1

Att1 ---> All other attributes

Cannot have partial FD on simple PK

Assume a table with a composite PK of Att1 and Att2

Att1, Att2 ---> Att3, Att4, Att5 (All other attributes)

It’s possible

Att2 ---> Att5

Att1, Att2 ---> Att3, Att48

Page 9: CS 3630 Database Design and Implementation. Unnormalized Form (UNF) student courses John CS363 CS334 CS323 Multi-Value attribute Common in reports 2.

ExampleLease (RNo, RName, PNo, PAddress, Start, Finish, Rent, ONo, OName)

PK: PNo, Start

AK: PNo, Finish

PAddress, Start

PAddress, Finish

FDs:

PNo, Start ---> All

PNo, Finish ---> All

PAddress, Start ---> All

PAddress, Finish ---> All

RNo ---> RName

ONo ---> Oname

PAddress ---> PNo, ONo, OName

PNo ---> PAddress, ONo, OName (Partial on Primary Key)

9

Page 10: CS 3630 Database Design and Implementation. Unnormalized Form (UNF) student courses John CS363 CS334 CS323 Multi-Value attribute Common in reports 2.

Table Instance RNo RName PNo PAddress Start Finish Rent Ono OName R101 John P1001 1001 main 1-1-04 12-31-04 350 O100 Tina R102 Mike P1001 1001 main 1-1-05 2-28-05 380 O100 Tina R103 Kay P1001 1001 main 3-1-05 12-31-05 380 O100 Tina R101 John P1002 2001 main 1-1-05 12-31-05 550 O110 Tony R105 Scot P1009 1009 first 1-1-04 5-31-04 350 O109 Tony

PNo, Start ---> All other attributes

(Partial on Primary Key) PNo ---> PAddress, ONo, OName

Not in 2NF!

Poor Performance Redundancy Inconsistency

What to Do?New Table!

10

Page 11: CS 3630 Database Design and Implementation. Unnormalized Form (UNF) student courses John CS363 CS334 CS323 Multi-Value attribute Common in reports 2.

Decompose Lease into 2NF Lease (RNo, RName, PNo, PAddress, Start, Finish, Rent, ONo, OName) PK: PNo, Start Partial FD on Primary Key PNo ---> PAddress, ONo, OName

Remove partial dependent on Primary KeyCreate a new relationWhich attributes are in the new relation? All attributes in the partial FD (both side of) Lease1 (PNo, PAddress, ONo, OName)

Which attributes are left in the original relation? PK remains the same Lease2 (RNo, RName, PNo, Start, Finish, Rent)

PK and FK?11

Page 12: CS 3630 Database Design and Implementation. Unnormalized Form (UNF) student courses John CS363 CS334 CS323 Multi-Value attribute Common in reports 2.

Lease1 (PNo, PAddress, ONo, OName)

PK: PNo

AK: PAddress

FK: None (from this conversion

Functional Dependencies:

PNo ---> All other attributes

PAddress ---> PNo, ONo, OName

ONo ---> OName

In 2NF?

Lease2 (RNo, RName, PNo, Start, Finish, Rent)

PK: PNo, Start

AK: PNo, Finish

FK: PNo references Lease1

Functional Dependencies:

PNo, Start ---> All other attributes

PNo, Finish ---> All other attributes

RNo ---> Rname

In 2NF? 12

Page 13: CS 3630 Database Design and Implementation. Unnormalized Form (UNF) student courses John CS363 CS334 CS323 Multi-Value attribute Common in reports 2.

Relation/Table Instances Lease

Rno RName PNo PAddress Start Finish Rent Ono OName R101 John P1001 1001 main 1-1-04 12-31-04 350 O100 Tina R102 Mike P1001 1001 main 1-1-05 2-28-05 380 O100 Tina R103 Kay P1001 1001 main 3-1-05 12-31-05 380 O100 Tina R101 John P1002 2001 main 1-1-05 12-31-05 550 O110 Tony R105 Scot P1009 1009 first 1-1-04 5-31-04 350 O109 Tony

Lease1 PNo PAddress ONo OName P1001 1001 main O100 Tina P1001 1001 main O100 Tina P1001 1001 main O100 Tina P1002 2001 main O110 Tony P1009 1009 first O109 Tony

Lease1 = PNo, PAddress, Ono, OName (Lease)

13

Page 14: CS 3630 Database Design and Implementation. Unnormalized Form (UNF) student courses John CS363 CS334 CS323 Multi-Value attribute Common in reports 2.

Removing Duplicates Lease1

PNo PAddress ONo OName P1001 1001 main O100 Tina P1001 1001 main O100 Tina P1001 1001 main O100 Tina P1002 2001 main O110 Tony P1009 1009 first O109 Tony

Lease1 P1001 1001 main O100 Tina P1002 2001 main O110 Tony P1009 1009 first O109 Tony

14

Page 15: CS 3630 Database Design and Implementation. Unnormalized Form (UNF) student courses John CS363 CS334 CS323 Multi-Value attribute Common in reports 2.

Lease RNo RName PNo PAddress Start Finish Rent Ono

OName R101 John P1001 1001 main 1-1-04 12-31-04 350 O100 Tina R102 Mike P1001 1001 main 1-1-05 2-28-05 380 O100 Tina R103 Kay P1001 1001 main 3-1-05 12-31-05 380 O100 Tina R101 John P1002 2001 main 1-1-05 12-31-05 550 O110 Tony R105 Scot P1009 1009 first 1-1-04 5-31-04 350 O109 Tony

Lease2 RNo RName PNo Start Finish Rent R101 John P1001 1-1-04 12-31-04 350 R102 Mike P1001 1-1-05 2-28-05 380 R103 Kay P1001 3-1-05 12-31-05 380 R101 John P1002 1-1-05 12-31-05 550 R105 Scot P1009 1-1-04 5-31-04 350

Lease2 = RNo, RName, RNo, Start, Finish, Rent (Lease)

15

Page 16: CS 3630 Database Design and Implementation. Unnormalized Form (UNF) student courses John CS363 CS334 CS323 Multi-Value attribute Common in reports 2.

Some FDs are lost

Lease (RNo, RName, PNo, PAddress, Start, Finish, Rent, ONo, OName)

PAddress, Start ---> All other attributes

PAddress, Finish ---> All other attributes

Lease1 (PNo, PAddress, ONo, OName)

Lease2 (RNo, RName, PNo, Start, Finish, Rent)

16

Page 17: CS 3630 Database Design and Implementation. Unnormalized Form (UNF) student courses John CS363 CS334 CS323 Multi-Value attribute Common in reports 2.

Example

Relation R (A, B, C, D, E, F)

PK: A, B

AK: None

FK: None

Functional Dependencies:

A, B ---> All

A ---> C

B ---> E, F

In 2NF?

NO!

17

Page 18: CS 3630 Database Design and Implementation. Unnormalized Form (UNF) student courses John CS363 CS334 CS323 Multi-Value attribute Common in reports 2.

Table Instance

A B C D E F

1 x 10 100 cs se

1 y 10 200 cs ee

2 x 20 300 cs se

2 y 20 400 cs ee

Functional Dependencies:

A, B ---> All

A ---> C

B ---> E, F

18

Page 19: CS 3630 Database Design and Implementation. Unnormalized Form (UNF) student courses John CS363 CS334 CS323 Multi-Value attribute Common in reports 2.

R1 (A, C) Primary Key: A Alternate Key: None Foreign Key: None Functional Dependencies: A ---> C

R2 (B, E, F) Primary Key: B Alternate Key: None Foreign Key: None Functional Dependencies: B ---> E, F

R3 (A, B, D) Primary Key: A, B Alternate Key: None Foreign Key: A References R1 B References R2 Functional Dependencies: A, B ---> D 19

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

Functional Dependencies: A, B ---> All A ---> C B ---> E, F

Decompose table R into 2NF

Page 20: CS 3630 Database Design and Implementation. Unnormalized Form (UNF) student courses John CS363 CS334 CS323 Multi-Value attribute Common in reports 2.

Table Instance

R

A B C D E F

1 x 10 100 cs se

1 y 10 200 cs ee

2 x 20 300 cs se

2 y 20 400 cs ee

R1

A C

1 10

2 20

R2

B E F

x cs se

y cs ee

R3

A B D

1 x 100

1 y 200

2 x 300

2 y 40020

Page 21: CS 3630 Database Design and Implementation. Unnormalized Form (UNF) student courses John CS363 CS334 CS323 Multi-Value attribute Common in reports 2.

Schedule

Assignment 5-1

Due Wednesday

Assignment 6-1

Due Monday, March 2

Quiz 2

Friday, March 621