Lecture:
Design Theory
Example
Enrollment
~375 Problems
table - “v0” cs145 Repeats?
students
Room/time change?
Deletes?
Properties
Class -> Room/time
~300 Room -> Lat, Lng
cs245
students (more compact)
Example
Enrollment
375
table - “v1” cs145
students
300
cs245
students
Design Theory
• Design theory is about how to represent your data to
avoid anomalies.
• Simple algorithms for “best practices”
Data Anomalies & Constraints
Constraints Prevent (some)
Anomalies in the Data
A poorly designed database causes anomalies:
Student Course Room
Mary CS145 B01
If every course is in
Joe CS145 B01 only one room,
contains redundant
Sam CS145 B01 information!
.. .. ..
Constraints Prevent (some)
Anomalies in the Data
A poorly designed database causes anomalies:
Student Course Room
If we update the
Mary CS145 B01 room number for
one tuple, we get
Joe CS145 C12 inconsistent data =
Sam CS145 B01 an update anomaly
.. .. ..
Constraints Prevent (some)
Anomalies in the Data
A poorly designed database causes anomalies:
Student Course Room
.. .. ..
If everyone drops the class, we lose what
room the class is in! = a delete anomaly
Constraints Prevent (some)
Anomalies in the Data
A poorly designed database causes anomalies:
Student Course Room
Mary CS145 B01 Similarly, we can’t
reserve a room
Joe CS145 B01 without students =
an insert
Sam CS145 B01 anomaly
… CS229 C12 .. .. ..
Constraints Prevent (some)
Anomalies in the Data
Is this form better?
Student Course
Course Room
Mary CS145 • Redundancy?
CS145 B01 • Update anomaly?
Joe CS145
• Delete anomaly?
CS229 C12
Sam CS145 • Insert anomaly?
.. ..
What are “good” decompositions?
Functional Dependencies
Functional Dependency
Def: Let A,B be sets of attributes
We 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
A->B means that
“whenever two tuples agree on A then they agree on B.”
A Picture Of FDs
Defn (again):
Given attribute sets A={A1,…,Am}
and B = {B1,…Bn} in R,
A1 … Am B1 … Bn
A Picture Of FDs
Defn (again):
Given attribute sets A={A1,…,Am}
and B = {B1,…Bn} in R,
A1 … Am B1 … Bn The functional dependency A→
B on R holds if for any ti,tj in R:
ti
tj
A Picture Of FDs
Defn (again):
Given attribute sets A={A1,…,Am}
and B = {B1,…Bn} in R,
A1 … Am B1 … Bn The functional dependency A→
B on R holds if for any ti,tj in R:
ti
if ti[A1] = tj[A1] AND ti[A2]=tj[A2]
AND … AND ti[Am] = tj[Am]
tj
If ti,tj agree here..
A Picture Of FDs
Defn (again):
Given attribute sets A={A1,…,Am}
and B = {B1,…Bn} in R,
A1 … Am B1 … Bn The functional dependency A→
B on R holds if for any ti,tj in R:
ti
if ti[A1] = tj[A1] AND ti[A2]=tj[A2] AND
… AND ti[Am] = tj[Am]
tj
then ti[B1] = tj[B1] AND ti[B2]=tj[B2]
AND … AND ti[Bn] = tj[Bn]
If ti,tj agree here.. …they also agree here!
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 schema
One which minimizes the possibility of anomalies
Functional Dependencies as
Constraints
Student Course Room
Mary CS145 B01
Joe CS145 B01
Sam CS145 B01
.. .. ..
Note: The FD {Course} -> However, cannot prove that the FD {Course}
{Room} holds on this -> {Room} holds on all instances. That is,
table instance FDs are for an instance and not for schema
Functional Dependencies as
Constraints
Note that:
Student Course Room
• You can check if an FD is violated
Mary CS145 B01
by examining a single instance;
Joe CS145 B01
• However, you cannot prove that an
Sam CS145 B01
FD is part of the schema by
examining a single instance. .. .. ..
• This would require checking every valid
instance
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
More Examples
EmpID Name Phone Position
E0045 Smith 1234 Clerk
E3542 Mike 9876 ← Salesrep
E1111 Smith 9876 ← Salesrep
E9999 Mary 1234 Lawyer
{Position} → {Phone}
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}
Example
(mega)
~375 Problems
Enrollment cs145 Repeats?
students
table - “v0” Room/time change?
Deletes?
FDs
Class -> Room,Time
~300 Room -> Lat, Lng
cs245
students (more compact)