Database Design Theory CS405G: Introduction to Database Systems

35
Database Design Theory CS405G: Introduction to Database Systems Jinze Liu 05/25/22 1

description

Database Design Theory CS405G: Introduction to Database Systems. Jinze Liu. Normalization. Normalization We discuss four normal forms: first, second, third, and Boyce- Codd normal forms 1NF, 2NF, 3NF, and BCNF - PowerPoint PPT Presentation

Transcript of Database Design Theory CS405G: Introduction to Database Systems

Page 1: Database Design Theory  CS405G: Introduction to Database Systems

Database Design Theory CS405G: Introduction to Database

SystemsJinze Liu

04/21/23 1

Page 2: Database Design Theory  CS405G: Introduction to Database Systems

Normalization

04/21/23 2

Page 3: Database Design Theory  CS405G: Introduction to Database Systems

Normalization

04/21/23 3

There is a sequence to normal forms: 1NF is considered the weakest, 2NF is stronger than 1NF, 3NF is stronger than 2NF, and BCNF is considered the strongest

Also, any relation that is in BCNF, is in 3NF; any relation in 3NF is in 2NF; and any relation in 2NF is in 1NF.

Page 4: Database Design Theory  CS405G: Introduction to Database Systems

Normalization

04/21/23 4

BCNF

3NF

2NF

1NF a relation in BCNF, is also in 3NF

a relation in 3NF is also in 2NF

a relation in 2NF is also in 1NF

Page 5: Database Design Theory  CS405G: Introduction to Database Systems

Normalization

04/21/23 5

• The benefit of higher normal forms is that update semantics for the affected data are simplified.

• This means that applications required to maintain the database are simpler.

• A design that has a lower normal form than another design has more redundancy. Uncontrolled redundancy can lead to data integrity problems.

• First we introduce the concept of functional dependency

Page 6: Database Design Theory  CS405G: Introduction to Database Systems

Review

• Functional dependencies– X -> Y: X “determines” Y

• If two rows agree on X, they must agree on Y A generalization of the key concepts

04/21/23 6

X Y A

x y1 a1

x ? a2y1

Page 7: Database Design Theory  CS405G: Introduction to Database Systems

Functional Dependencies

04/21/23 8

EmpNum EmpEmail EmpNum EmpFname EmpNum EmpLname

EmpNumEmpEmail

EmpEmail

EmpEmail

EmpNum EmpEmail EmpFname EmpLname

3 different ways you might see FDs depicted

Page 8: Database Design Theory  CS405G: Introduction to Database Systems

Determinant

04/21/23 9

Functional Dependency

EmpNum EmpEmail

Attribute on the LHS is known as the determinant

• EmpNum is a determinant of EmpEmail

Page 9: Database Design Theory  CS405G: Introduction to Database Systems

EmployeeProject

ssn pnumber hours ename plocation

Essn pno hours

WORKS ON

What functional dependencies?

04/21/23 10

Page 10: Database Design Theory  CS405G: Introduction to Database Systems

Transitive dependency

04/21/23 11

Transitive dependency

Consider attributes A, B, and C, and where

A B and B C.

Functional dependencies are transitive, which

means that we also have the functional dependency

A C

We say that C is transitively dependent on A

through B.

Page 11: Database Design Theory  CS405G: Introduction to Database Systems

Transitive dependency

04/21/23 12

EmpNum EmpEmail DeptNum DeptNname

EmpNum EmpEmail DeptNum DeptNname

DeptName is transitively dependent on EmpNum via DeptNumEmpNum DeptName

EmpNum DeptNum

DeptNum DeptName

Page 12: Database Design Theory  CS405G: Introduction to Database Systems

Partial dependency

04/21/23 13

A partial dependency exists when an attribute B is functionally dependent on an attribute A, and A is a component of a multipart candidate key.

InvNum LineNum Qty InvDate

Candidate keys: {InvNum, LineNum} InvDate is partially dependent on {InvNum, LineNum} as InvNum is a determinant of InvDate and InvNum is part of a candidate key

Page 13: Database Design Theory  CS405G: Introduction to Database Systems

First Normal Form

04/21/23 14

First Normal Form

We say a relation is in 1NF if all values stored in the relation are single-valued and atomic.

1NF places restrictions on the structure of relations. Values must be simple.

Page 14: Database Design Theory  CS405G: Introduction to Database Systems

First Normal Form

04/21/23 15

The following is not in 1NF

EmpNum EmpPhone EmpDegrees123 233-9876333 233-1231 BA, BSc, PhD679 233-1231 BSc, MSc

EmpDegrees is a multi-valued field:

employee 679 has two degrees: BSc and MSc

employee 333 has three degrees: BA, BSc, PhD

Page 15: Database Design Theory  CS405G: Introduction to Database Systems

First Normal Form

04/21/23 16

To obtain 1NF relations we must, without loss of information, replace the above with two relations - see next slide

EmpNum EmpPhone EmpDegrees123 233-9876333 233-1231 BA, BSc, PhD679 233-1231 BSc, MSc

What would the ERD be for the above situation with EmpNum, EmpPhone, EmpDegrees. Would we have generated the above table using our “mapping algorithm”?

Page 16: Database Design Theory  CS405G: Introduction to Database Systems

First Normal Form

04/21/23 17

EmpNum EmpDegree

333 BA

333 BSc

333 PhD

679 BSc

MSc679

EmpNum EmpPhone

123 233-9876

333 233-1231

679 233-1231

An outer join between Employee and EmployeeDegree will produce the information we saw before

EmployeeEmployeeDegree

Page 17: Database Design Theory  CS405G: Introduction to Database Systems

Boyce-Codd Normal Form

04/21/23 18

LineNum ProdNum QtyInvNum

InvNum, LineNum ProdNum

InvNum, ProdNum LineNum

There are two candidate keys.

Since every determinant is a candidate key, the relation is in BCNF

This relation is about Invoice lines only.

Qty{InvNum, LineNum} and {InvNum, ProdNum} are the two candidate keys.

Page 18: Database Design Theory  CS405G: Introduction to Database Systems

Second Normal Form

04/21/23 19

Second Normal Form

A relation is in 2NF if it is in 1NF, and every non-key attribute is fully dependent on each candidate key. (That is, we don’t have any partial functional dependency.)

• 2NF (and 3NF) both involve the concepts of key and non-key attributes. • A key attribute is any attribute that is part of a key; any attribute that is not a key attribute, is a non-key attribute. • Relations that are not in BCNF have data redundancies• A relation in 2NF will not have any partial dependencies

Page 19: Database Design Theory  CS405G: Introduction to Database Systems

Second Normal Form

04/21/23 20

LineNum ProdNum QtyInvNum

InvNum, LineNum ProdNum

InvNum, ProdNum LineNum

Since there is a determinant that is not a candidate key, InvLine is not BCNF

InvLine is not 2NF since there is a partial dependency of InvDate on InvNum

Qty

InvDate

InvDateInvNum

There are two candidate keys.

Qty is the only non-key attribute, and it is dependent on InvNum

InvLine is only in 1NF

Consider this InvLine table (in 1NF):

Page 20: Database Design Theory  CS405G: Introduction to Database Systems

Second Normal Form

04/21/23 21

LineNum ProdNum QtyInvNum InvDate

InvLine

The above relation has redundancies: the invoice date is repeated on each invoice line.

We can improve the database by decomposing the relation into two relations:

LineNum ProdNum QtyInvNum

InvDateInvNum

Question: What is the highest normal form for these relations? 2NF? 3NF? BCNF?

Page 21: Database Design Theory  CS405G: Introduction to Database Systems

2NF, but not in 3NF, nor in BCNF:

inv_no line_no prod_no prod_desc qty

since prod_no is not a candidate key and we have:

prod_no prod_desc.

04/21/23 22

Page 22: Database Design Theory  CS405G: Introduction to Database Systems

2NF, but not in 3NF, nor in BCNF:

since dnumber is not a candidate key and we have:

dnumber dname.

EmployeeDept

ename ssn bdate address dnumber dname

04/21/23 23

Page 23: Database Design Theory  CS405G: Introduction to Database Systems

Third Normal Form

04/21/23 24

Third Normal Form

a relation is in 3NF if the relation is in 1NF and all determinants of non-key attributes are candidate keysThat is, for any functional dependency: X Y, where Y is a non-key attribute (or a set of non-key attributes), X is a candidate key.

this definition of 3NF differs from BCNF only in the specification of non-key attributes - 3NF is weaker than BCNF. (BCNF requires all determinants to be candidate keys.)

A relation in 3NF will not have any transitive dependencies

Page 24: Database Design Theory  CS405G: Introduction to Database Systems

Boyce-Codd Normal Form

04/21/23 25

Boyce-Codd Normal Form

BCNF is defined very simply:

a relation is in BCNF if it is in 1NF and if every determinant is a candidate key.

If our database will be used for OLTP (on line transaction processing), then BCNF is our target. Usually, we meet this objective. However, we might denormalize (3NF, 2NF, or 1NF) for performance reasons.

Page 25: Database Design Theory  CS405G: Introduction to Database Systems

Third Normal Form

04/21/23 26

EmpNum EmpName DeptNum DeptName

EmpName, DeptNum, and DeptName are non-key attributes.

DeptNum determines DeptName, a non-key attribute, and DeptNum is not a candidate key.

Consider this Employee relation

Is the relation in 3NF? … no

Is the relation in 2NF? … yes

Is the relation in BCNF? … no

Candidate keys are? …

Page 26: Database Design Theory  CS405G: Introduction to Database Systems

Third Normal Form

04/21/23 27

EmpNum EmpName DeptNum DeptName

We correct the situation by decomposing the original relation into two 3NF relations. Note the decomposition is lossless.

EmpNum EmpName DeptNum DeptNameDeptNum

Verify these two relations are in 3NF.

Are they in BCNF?

Page 27: Database Design Theory  CS405G: Introduction to Database Systems

student_no course_no instr_no

Instructor teaches one course only.

Student takes a course and has one instructor.

In 3NF, but not in BCNF:

{student_no, course_no} instr_noinstr_no course_no

since we have instr_no course-no, but instr_no is not aCandidate key.

04/21/23 28

Page 28: Database Design Theory  CS405G: Introduction to Database Systems

course_no instr_no

student_no instr_no

student_no course_no instr_no

BCNF

{student_no, instr_no} student_no{student_no, instr_no} instr_noinstr_no course_no

04/21/23 29

Page 29: Database Design Theory  CS405G: Introduction to Database Systems

Another example: 3NF

• Address (street_address, city, state, zip)– street_address, city, state -> zip– zip -> city, state

• Keys– {street_address, city, state}– {street_address, zip}

• BCNF?– Violation: zip -> city, state

04/21/23 30

Page 30: Database Design Theory  CS405G: Introduction to Database Systems

To decompose or not to decompose

Address1 (zip, city, state)Address2 (street_address, zip)• FD’s in Address1

– zip -> city, state• FD’s in Address2

– None!• Hey, where is street_address, city, state -> zip?

– Cannot check without joining Address1 and Address2 back together

• Dilemma: Should we get rid of redundancy at the expense of making constraints harder to enforce?

04/21/23 31

Page 31: Database Design Theory  CS405G: Introduction to Database Systems

BCNF = no redundancy?

• Student (SID, CID, club)– Suppose your classes have nothing to do with the

clubs you join– FD’s?

• None

– BCNF?• Yes

– Redundancies?• Tons!

04/21/23 32

SID CID club142 CPS116 ballet142 CPS116 sumo142 CPS114 ballet142 CPS114 sumo123 CPS116 chess123 CPS116 golf... ... ...

Page 32: Database Design Theory  CS405G: Introduction to Database Systems

Multivalued dependencies

• A multivalued dependency (MVD) has the formX ->>Y, where X and Y are sets of attributes in a relation R

• X ->>Y means that whenever two rows in R agree on all the attributes of X, then we can swap their Y components and get two new rows that are also in R

04/21/23 33

Must be in R too

X Y Za b1c1a b2c2a b1c2a b2c1

Page 33: Database Design Theory  CS405G: Introduction to Database Systems

4NF decomposition example

04/21/23 34

Student (SID, CID, club)4NF violation: SID ->>CID

Enroll (SID, CID) Join (SID, club)4NF 4NF

SID CID club

142CPS116 ballet

142CPS116 sumo

142CPS114 ballet

142CPS114 sumo

123CPS116 chess

123CPS116 golf

... ... ...SID CID142 CPS116142 CPS114123 CPS116... ...

SID club142 ballet142 sumo123 chess123 golf... ...

Page 34: Database Design Theory  CS405G: Introduction to Database Systems

3NF, BCNF, 4NF, and beyond

• Of historical interests– 1NF: All column values must be atomic– 2NF: Slightly more relaxed than 3NF

04/21/23 35

Anomaly/normal form 3NF BCNF 4NF

Lose FD’s? No Possible Possible

Redundancy due to FD’s Possible No No

Redundancy due to MVD’s

Possible Possible No

Page 35: Database Design Theory  CS405G: Introduction to Database Systems

Summary

• Philosophy behind BCNF, 4NF:Data should depend on the key, the whole key, and nothing but the key!

• Philosophy behind 3NF: … But not at the expense of more expensive constraint enforcement!

04/21/23 36