Design Theory Lecture - cs145-fa21.github.io

23
Lecture: Design Theory

Transcript of Design Theory Lecture - cs145-fa21.github.io

Page 1: Design Theory Lecture - cs145-fa21.github.io

Lecture:Design Theory

Page 2: Design Theory Lecture - cs145-fa21.github.io

Example Enrollment table - “v0” ~375

cs145 students

~300 cs245students

ProblemsRepeats?Room/time change?Deletes?

PropertiesClass -> Room/timeRoom -> Lat, Lng

(more compact)

Page 3: Design Theory Lecture - cs145-fa21.github.io

Example Enrollment table - “v1” 375

cs145 students

300 cs245students

Page 4: Design Theory Lecture - cs145-fa21.github.io

Design Theory

• Design theory is about how to represent your data to avoid anomalies.

• Simple algorithms for “best practices”

Page 5: Design Theory Lecture - cs145-fa21.github.io

Data Anomalies & Constraints

Page 6: Design Theory Lecture - cs145-fa21.github.io

Constraints Prevent (some) Anomalies in the Data

Student Course Room

Mary CS145 B01

Joe CS145 B01

Sam CS145 B01

.. .. ..

If every course is in only one room, contains redundant information!

A poorly designed database causes anomalies:

Page 7: Design Theory Lecture - cs145-fa21.github.io

Constraints Prevent (some) Anomalies in the Data

Student Course Room

Mary CS145 B01

Joe CS145 C12

Sam CS145 B01

.. .. ..

If we update the room number for one tuple, we get inconsistent data = an update anomaly

A poorly designed database causes anomalies:

Page 8: Design Theory Lecture - cs145-fa21.github.io

Constraints Prevent (some) Anomalies in the Data

Student Course Room

.. .. ..

If everyone drops the class, we lose what room the class is in! = a delete anomaly

A poorly designed database causes anomalies:

Page 9: Design Theory Lecture - cs145-fa21.github.io

Constraints Prevent (some) Anomalies in the Data

Student Course Room

Mary CS145 B01

Joe CS145 B01

Sam CS145 B01

.. .. ..

Similarly, we can’t reserve a room without students = an insert anomaly

A poorly designed database causes anomalies:

… CS229 C12

Page 10: Design Theory Lecture - cs145-fa21.github.io

Constraints Prevent (some) Anomalies in the Data

Student Course

Mary CS145

Joe CS145

Sam CS145

.. ..

Course Room

CS145 B01

CS229 C12

What are “good” decompositions?

Is this form better?

• Redundancy? • Update anomaly? • Delete anomaly?• Insert anomaly?

Page 11: Design Theory Lecture - cs145-fa21.github.io

Functional Dependencies

Page 12: Design Theory Lecture - cs145-fa21.github.io

Functional Dependency

A->B means that “whenever two tuples agree on A then they agree on B.”

Def: Let A,B be sets of attributesWe write A → B or say A functionally determines B if, for any tuples t1 and t2:

t1[A] = t2[A] implies t1[B] = t2[B]and we call A → B a functional dependency

Page 13: Design Theory Lecture - cs145-fa21.github.io

A Picture Of FDs

A1 … Am B1 … Bn

Defn (again):Given attribute sets A={A1,…,Am} and B = {B1,…Bn} in R,

Page 14: Design Theory Lecture - cs145-fa21.github.io

A Picture Of FDs

A1 … Am B1 … Bn

ti

tj

Defn (again):Given attribute sets A={A1,…,Am} and B = {B1,…Bn} in R,

The functional dependency A→ B on R holds if for any ti,tj in R:

Page 15: Design Theory Lecture - cs145-fa21.github.io

A Picture Of FDs

Defn (again):Given attribute sets A={A1,…,Am} and B = {B1,…Bn} in R,

The functional dependency A→ B on R holds if for any ti,tj in R:

if ti[A1] = tj[A1] AND ti[A2]=tj[A2] AND … AND ti[Am] = tj[Am]

A1 … Am B1 … Bn

ti

tj

If ti,tj agree here..

Page 16: Design Theory Lecture - cs145-fa21.github.io

A Picture Of FDs

Defn (again):Given attribute sets A={A1,…,Am} and B = {B1,…Bn} in R,

The functional dependency A→ B on R holds if for any ti,tj in R:

if ti[A1] = tj[A1] AND ti[A2]=tj[A2] AND … AND ti[Am] = tj[Am]

then ti[B1] = tj[B1] AND ti[B2]=tj[B2] AND … AND ti[Bn] = tj[Bn]

A1 … Am B1 … Bn

ti

tj

If ti,tj agree here.. …they also agree here!

Page 17: Design Theory Lecture - cs145-fa21.github.io

FDs for Relational Schema Design

High-level idea: why do we care about FDs?

1. Start with some relational schema

2. Find functional dependencies (FDs)

3. Use these to design a better schemaOne which minimizes the possibility of anomalies

Page 18: Design Theory Lecture - cs145-fa21.github.io

Functional Dependencies as Constraints

Student Course Room

Mary CS145 B01

Joe CS145 B01

Sam CS145 B01

.. .. ..

Note: The FD {Course} -> {Room} holds on this table instance

However, cannot prove that the FD {Course} -> {Room} holds on all instances. That is, FDs are for an instance and not for schema

Page 19: Design Theory Lecture - cs145-fa21.github.io

Functional Dependencies as Constraints

Student Course Room

Mary CS145 B01

Joe CS145 B01

Sam CS145 B01

.. .. ..

Note that:

• You can check if an FD is violated by examining a single instance;

• However, you cannot prove that an FD is part of the schema by examining a single instance. • This would require checking every valid

instance

Page 20: Design Theory Lecture - cs145-fa21.github.io

More Examples

An FD is a constraint which holds, or does not hold on an instance:

EmpID Name Phone Position

E0045 Smith 1234 Clerk

E3542 Mike 9876 Salesrep

E1111 Smith 9876 Salesrep

E9999 Mary 1234 Lawyer

Page 21: Design Theory Lecture - cs145-fa21.github.io

More Examples

{Position} → {Phone}

EmpID Name Phone Position

E0045 Smith 1234 Clerk

E3542 Mike 9876 ← Salesrep

E1111 Smith 9876 ← Salesrep

E9999 Mary 1234 Lawyer

Page 22: Design Theory Lecture - cs145-fa21.github.io

More Examples

EmpID Name Phone Position

E0045 Smith 1234 → Clerk

E3542 Mike 9876 Salesrep

E1111 Smith 9876 Salesrep

E9999 Mary 1234 → Lawyer

but not {Phone} → {Position}

Page 23: Design Theory Lecture - cs145-fa21.github.io

Example (mega)Enrollment table - “v0”

~375cs145 students

~300 cs245students

ProblemsRepeats?Room/time change?Deletes?

FDsClass -> Room,TimeRoom -> Lat, Lng

(more compact)