RDBMS and SQL Normal Forms - GitHub Pages

39
Venkatesh Vinayakarao (Vv) RDBMS and SQL Normal Forms Venkatesh Vinayakarao [email protected] http://vvtesh.co.in Chennai Mathematical Institute Slide contents are borrowed from the course text. For the authors’ original version of slides, visit: https://www.db-book.com/db6/slide-dir/index.html.

Transcript of RDBMS and SQL Normal Forms - GitHub Pages

Page 1: RDBMS and SQL Normal Forms - GitHub Pages

Venkatesh Vinayakarao (Vv)

RDBMS and SQL

Normal Forms

Venkatesh [email protected]

http://vvtesh.co.in

Chennai Mathematical Institute

Slide contents are borrowed from the course text. For the authors’ original version of slides, visit: https://www.db-book.com/db6/slide-dir/index.html.

Page 2: RDBMS and SQL Normal Forms - GitHub Pages

Agenda

• Review

• Normal Forms• BCNF

• 4th Normal Form

• Decomposition

Page 3: RDBMS and SQL Normal Forms - GitHub Pages

Story So Far…1 2

3

Relational AlgebraSQL

Page 4: RDBMS and SQL Normal Forms - GitHub Pages

Story So Far…

123

1

section (course_id, sec_id, semester, year)

DB Design with ER Model

Representation as a schema

2

Functional Dependency and Normal Forms

Page 5: RDBMS and SQL Normal Forms - GitHub Pages

Prime Attribute

• All attributes that form the key are called prime attributes.

124

R1(ABCD)

AB is the key. A and B are prime attributes.

Page 6: RDBMS and SQL Normal Forms - GitHub Pages

Quiz

• Let R(A,B,C) be a relation where the candidate keys are AB and AC. We chose AB as the primary key. What are the prime attributes?

125

Page 7: RDBMS and SQL Normal Forms - GitHub Pages

Quiz

• Let R(A,B,C) be a relation where the candidate keys are AB and AC. We chose AB as the primary key. What are the prime attributes?• Answer: A, B and C

126

Page 8: RDBMS and SQL Normal Forms - GitHub Pages

Review: Second Normal Form (2NF)

127

• A relation is in second normal form iff• it is in 1nf, and

• no non-prime attribute is functionally dependent on any proper subset of any candidate key.

No Partial Dependency!

Manufacturer →Manufacturer Country{Manufacturer, Model} →Model full name

Page 9: RDBMS and SQL Normal Forms - GitHub Pages

Quiz

• Is the relation R(A,B,C) in 2NF, • if B → C is a functional dependency?

• Assume AB and AC are candidate keys.

• Assume AB is the primary key.

128

Page 10: RDBMS and SQL Normal Forms - GitHub Pages

R is in 2NF

2NF requires that every non-prime attribute is fully dependent on every candidate key.

A,B and C are prime attributes! So, R is in 2NF.

Quiz

• Is the relation R(A,B,C) in 2NF, • if B → C is a functional dependency?

• Assume AB and AC are candidate keys.

• Assume AB is the primary key.

129

Page 11: RDBMS and SQL Normal Forms - GitHub Pages

Anomalies

• Delete• When deleting a value of an attribute, we should not

lose the value of some other attribute.

• Insert• To insert the value of an attribute, we need to know the

value of another attribute which we might not know.

• Storing the same data at multiple relations may lead to consistency issues.

• Update• Same as insert, but for record updates.

130

Page 12: RDBMS and SQL Normal Forms - GitHub Pages

Boyce-Codd Normal Form

• A relation schema R is in BCNF with respect to a set F of functional dependencies if for all functional dependencies in F+ of the form → where Rand R, at least one of the following holds:• → is trivial (i.e., )

• is a superkey for Rif there is an FD, LHS must be a

superkey.

Page 13: RDBMS and SQL Normal Forms - GitHub Pages

BCNF Violation

132

Candidate Keys

Assume the FD is

Relation is not in BCNF• FD is non-trivial• In FD, Rate type is not a superkey

Is this relation in 2NF?Yes, no non-prime attributes

RateTypes• SAVER: for Court 1 bookings made by

members• STANDARD: for Court 1 bookings made by

non-members• PREMIUM-A: for Court 2 bookings made by

members• PREMIUM-B: for Court 2 bookings made by

non-members

Page 14: RDBMS and SQL Normal Forms - GitHub Pages

Decompose to BCNF

133Example Source: Wikipedia

Page 15: RDBMS and SQL Normal Forms - GitHub Pages

A Lossy Decomposition

Not all decompositions are good!

Page 16: RDBMS and SQL Normal Forms - GitHub Pages

Example of Lossless-Join Decomposition

• Lossless join and dependency preserving too!

A B

12

A

B

12

r B,C(r)

A,B (r) B,C (r)

A B

12

C

AB

B

12

C

AB

C

AB

A,B(r)

R1 = (A, B) R2 = (B, C)R = (A, B, C)

Natural join (on B)

Joined on columns with common name.

Assume,F = {A→B, B→C}

Page 17: RDBMS and SQL Normal Forms - GitHub Pages

Decomposition and Lossless Joins

• Consider a schema R(A, B, C, D) and functional dependencies A -> B and C -> D. Then the decomposition of R into R1(A, B) and R2(C, D) is

(a) dependency preserving and lossless join

(b) lossless join but not dependency preserving

(c) dependency preserving but not lossless join

(d) not dependency preserving and not lossless join

[GATE CS 2001]

Page 18: RDBMS and SQL Normal Forms - GitHub Pages

Decomposition and Lossless Joins

• Consider a schema R(A, B, C, D) and functional dependencies A -> B and C -> D. Then the decomposition of R into R1(A, B) and R2(C, D) is

(a) dependency preserving and lossless join

(b) lossless join but not dependency preserving

(c) dependency preserving but not lossless join

(d) not dependency preserving and not lossless join

[GATE CS 2001]

Page 19: RDBMS and SQL Normal Forms - GitHub Pages

Quiz

• Consider a relation inst_info (ID, child_name, phone)• where an instructor may have multiple phones and can

have multiple children

138

ID child_name phone

99999999999999999999

DavidDavidWilliamWillian

512-555-1234512-555-4321512-555-1234512-555-4321

inst_info

Is this in BCNF?

Page 20: RDBMS and SQL Normal Forms - GitHub Pages

How good is BCNF? (Cont.)

• There are no non-trivial functional dependencies and therefore the relation is in BCNF

• Insertion anomalies exist– i.e., if we add a phone 981-992-3443 to 99999, we need to add two tuples

(99999, David, 981-992-3443)(99999, William, 981-992-3443)

Page 21: RDBMS and SQL Normal Forms - GitHub Pages

Redundancy in BCNF

• There are database schemas in BCNF that do not seem to be sufficiently normalized

ID child_name phone

99999999999999999999

DavidDavidWilliamWillian

512-555-1234512-555-4321512-555-1234512-555-4321

inst_info

Page 22: RDBMS and SQL Normal Forms - GitHub Pages

Quiz

• Let the set of functional dependencies F = {QR → S, R → P, S → Q} hold on a relation schema X = (PQRS). Suppose X is decomposed into two schemas Y and Z where Y = (PR) and Z = (QRS).• Is the decomposition dependency preserving?

141

Page 23: RDBMS and SQL Normal Forms - GitHub Pages

Quiz

• Let the set of functional dependencies F = {QR → S, R → P, S → Q} hold on a relation schema X = (PQRS). Suppose X is decomposed into two schemas Y and Z where Y = (PR) and Z = (QRS).• Is the decomposition dependency preserving?

• Y = (PR) : F = {R → P}

• Z = (QRS) : F = {QR → S, S → Q}

• We see that the decomposition is dependency preserving.

142

Page 24: RDBMS and SQL Normal Forms - GitHub Pages

Quiz

• Let the set of functional dependencies F = {QR → S, R → P, S → Q} hold on a relation schema X = (PQRS). Suppose X is decomposed into two schemas Y and Z where Y = (PR) and Z = (QRS).• Is the decomposition lossless on join?

143

Page 25: RDBMS and SQL Normal Forms - GitHub Pages

Quiz

• Let the set of functional dependencies F = {QR → S, R → P, S → Q} hold on a relation schema X = (PQRS). Suppose X is decomposed into two schemas Y and Z where Y = (PR) and Z = (QRS).• Is the decomposition lossless on join?

144

Z

Q R S

Y

R P

{QR → S, S → Q}

{R → P}

R is a key! So, the decomposition is lossless.

Page 26: RDBMS and SQL Normal Forms - GitHub Pages

Quiz

• Let the set of functional dependencies F = {QR → S, R → P, S → Q} hold on a relation schema X = (PQRS). Suppose X is decomposed into two schemas Y and Z where Y = (PR) and Z = (QRS).• Is Y in BCNF?

• Is Z in BCNF?

145

Page 27: RDBMS and SQL Normal Forms - GitHub Pages

Quiz

• Let the set of functional dependencies F = {QR → S, R → P, S → Q} hold on a relation schema X = (PQRS). Suppose X is decomposed into two schemas Y and Z where Y = (PR) and Z = (QRS).• Is Y in BCNF?

• Is Z in BCNF?

146

Y

R P{R → P}

R is a super key. So, Y is in BCNF.

Page 28: RDBMS and SQL Normal Forms - GitHub Pages

Quiz

• Let the set of functional dependencies F = {QR → S, R → P, S → Q} hold on a relation schema X = (PQRS). Suppose X is decomposed into two schemas Y and Z where Y = (PR) and Z = (QRS).• Is Y in BCNF?

• Is Z in BCNF?

147

Z

Q R S{QR → S, S → Q}

S is not a super key. So, Z is not in BCNF.

Page 29: RDBMS and SQL Normal Forms - GitHub Pages

Quiz

• Let the set of functional dependencies F = {QR → S, R → P, S → Q} hold on a relation schema X = (PQRS). Suppose X is decomposed into two schemas Y and Z where Y = (PR) and Z = (QRS).• What are the candidate keys of Z?

148

Z

Q R S{QR → S, S → Q}

Page 30: RDBMS and SQL Normal Forms - GitHub Pages

Quiz

• Let the set of functional dependencies F = {QR → S, R → P, S → Q} hold on a relation schema X = (PQRS). Suppose X is decomposed into two schemas Y and Z where Y = (PR) and Z = (QRS).• What are the candidate keys of Z?

149

Z

Q R S{QR → S, S → Q}

Candidate Keys = RQ, RS

Page 31: RDBMS and SQL Normal Forms - GitHub Pages

Quiz

• Let the set of functional dependencies F = {QR → S, R → P, S → Q} hold on a relation schema X = (PQRS). Suppose X is decomposed into two schemas Y and Z where Y = (PR) and Z = (QRS).• Is Z in 3NF?

150

Z

Q R S{QR → S, S → Q}

Candidate Keys = RQ, RS

Page 32: RDBMS and SQL Normal Forms - GitHub Pages

Quiz

• Let the set of functional dependencies F = {QR → S, R → P, S → Q} hold on a relation schema X = (PQRS). Suppose X is decomposed into two schemas Y and Z where Y = (PR) and Z = (QRS).• Is Z in 3NF?

151

Z

Q R S{QR → S, S → Q}

Candidate Keys = RQ, RSYes! Z is in 3NF. No non-prime attributes.

Page 33: RDBMS and SQL Normal Forms - GitHub Pages

Quiz

• Let the set of functional dependencies F = {QR → S, R → P, S → Q} hold on a relation schema X = (PQRS). Suppose X is decomposed into two schemas Y and Z where Y = (PR) and Z = (QRS).

I. Both Y and Z are in BCNFII. Decomposition of X into Y and Z is dependency preserving

and lossless

• Which of the above statements is/are correct?a) I onlyb) Neither I nor II c) Both I and IId) II only

152

Page 34: RDBMS and SQL Normal Forms - GitHub Pages

Quiz

• Let the set of functional dependencies F = {QR → S, R → P, S → Q} hold on a relation schema X = (PQRS). Suppose X is decomposed into two schemas Y and Z where Y = (PR) and Z = (QRS).

I. Both Y and Z are in BCNFII. Decomposition of X into Y and Z is dependency preserving

and lossless

• Which of the above statements is/are correct?a) I onlyb) Neither I nor II c) Both I and IId) II only

153

[GATE CS 2019]

Page 35: RDBMS and SQL Normal Forms - GitHub Pages

Fourth Normal Form (4NF)

• Motivation• The Pizza Delivery relation is in BCNF.

• Yet there is redundancy!

• Restaurant does not functionally determine variety, but it multi-determines it.

154

{Restaurant} →→ {Pizza Variety}{Restaurant} →→ {Delivery Area}

{Restaurant} → {Pizza Variety}{Restaurant} → {Delivery Area}

Page 36: RDBMS and SQL Normal Forms - GitHub Pages

4NF Decomposition

155

Page 37: RDBMS and SQL Normal Forms - GitHub Pages

Review

156

Not in 1NF

Not in 2NF

Not in 3NF

Not in 4NFNot in BCNF

Page 38: RDBMS and SQL Normal Forms - GitHub Pages

Quiz

• Consider a schema R(VOLUME, NUMBER, STARTPGE, ENDPAGE, TITLE, YEAR, PRICE)

• Functional dependencies are:(VOLUME, NUMBER, STARTPAGE, ENDPAGE) -> TITLE

(VOLUME, NUMBER) -> YEAR

(VOLUME, NUMBER, STARTPAGE, ENDPAGE) -> PRICE

• The database is redesigned to use the following schemas.

R1(VOLUME, NUMBER, STARTPAGE, ENDPAGE, TITLE, PRICE)

R2(VOLUME, NUMBER, YEAR)

• Which is the weakest normal form that the new database satisfies, but the old one does not?

157[GATE CS 2016]

Page 39: RDBMS and SQL Normal Forms - GitHub Pages

Quiz

• Consider a schema R(VOLUME, NUMBER, STARTPGE, ENDPAGE, TITLE, YEAR, PRICE)

• Functional dependencies are:(VOLUME, NUMBER, STARTPAGE, ENDPAGE) -> TITLE

(VOLUME, NUMBER) -> YEAR

(VOLUME, NUMBER, STARTPAGE, ENDPAGE) -> PRICE

• The database is redesigned to use the following schemas.

R1(VOLUME, NUMBER, STARTPAGE, ENDPAGE, TITLE, PRICE)

R2(VOLUME, NUMBER, YEAR)

• Which is the weakest normal form that the new database satisfies, but the old one does not? 2NF

158[GATE CS 2016]