SEN2104 · Week 9 · Relational Database Design

Bad design, functional dependencies and closure

You learn why putting everything into one table produces anomalies, and then functional dependencies, Armstrong's axioms and finding keys with closure. All normalization questions rely on these calculations.

functional dependencyupdate anomalyinsertion anomalydeletion anomalytrivial dependencyArmstrong's axiomsattribute closuresuperkeycandidate keyclosure of F

This is the first week of the normalization section. The most reliable points after the midterm come from here, because most of the questions are mechanical: compute a closure, find a key, derive an FD. The three skills on this page are the basis of the next two weeks (normal forms, decomposition):

  1. Explaining why one big table causes problems with anomalies.
  2. Understanding what a functional dependency means and checking whether it holds in the sample data.
  3. Finding superkeys and candidate keys with attribute closure.

Bad design: everything in one table

In the university database we use all semester, takes, student, course and department are separate tables. Imagine you merged them into one “wide” table:

CREATE TABLE wide AS
SELECT t.stu_id, s.name AS stu_name, t.course_id, c.title, c.credits,
       c.dept_name AS course_dept, d.building, t.grade
FROM takes t JOIN student s ON s.stu_id = t.stu_id
             JOIN course c ON c.course_id = t.course_id
             JOIN department d ON d.dept_name = c.dept_name;

The result is 14 rows (I am not showing the stu_name and credits columns to save space):

stu_idcourse_idtitlecourse_deptbuildinggrade
S1CS101Intro to ProgrammingCSGalataA
S2CS101Intro to ProgrammingCSGalataB
S3CS101Intro to ProgrammingCSGalataNULL
S5CS101Intro to ProgrammingCSGalataF
S1CS201Data StructuresCSGalataB
S2CS201Data StructuresCSGalataC
S1CS305DatabasesCSGalataA
S4CS305DatabasesCSGalataB
S3EE210CircuitsEEGalataA
S1MA101CalculusMATHRumeliB
S3MA101CalculusMATHRumeliC
S4MA101CalculusMATHRumeliA
S4MA202Linear AlgebraMATHRumeliA
S7MA202Linear AlgebraMATHRumeliB

Writing queries became easier, but the table is open to three kinds of errors:

AnomalyHow it happens in this table
Update anomalyIf the CS department moves to another building, you have to change 8 rows. If you forget one, two different buildings appear for CS and the data is contradictory.
Insertion anomalyYou cannot add the course BI100 (Biology) while no student takes it. In the same way, S6 (Firat), who has taken no course, is not in the table.
Deletion anomalyEE210 is in only 1 row. If S3 drops the course and the row is deleted, the information “EE210 = Circuits, 3 credits” is lost too.

The root cause: the table squeezes independent facts (the name of a course, the building of a department, a student’s grade) into the same row. The way to express these facts formally is a functional dependency.

Functional dependency

Let XX and YY be sets of attributes of the schema RR. X→YX \to Y means: any two rows with the same XX values also have the same YY values. It is read ”XX determines YY”.

Real-world rules for the wide table:

  • stu_id→stu_name\text{stu\_id} \to \text{stu\_name}
  • course_id→title, credits, course_dept\text{course\_id} \to \text{title, credits, course\_dept}
  • course_dept→building\text{course\_dept} \to \text{building}
  • stu_id, course_id→grade\text{stu\_id, course\_id} \to \text{grade}

From these FDs, {stu_id, course_id} comes out as the only candidate key of the table (you will see below how to find it).

Checking on sample data

A single pair is enough to violate an FD: two rows with the same left side and a different right side. With SQL you find it by grouping:

-- does building -> course_dept hold?
SELECT building FROM wide
GROUP BY building
HAVING COUNT(DISTINCT course_dept) > 1;
Galata

Galata matches both CS and EE: building→course_dept\text{building} \to \text{course\_dept} is violated. When you run the same query for course_id/title or course_dept/building, the result is empty, so these FDs hold in this instance. For credits -> course_dept the query returns the values 3 and 4 (4-credit courses are in both CS and MATH).

PITFALL · sample data is not proof. If the query returned empty, it means the FD is not violated in these rows, not that it is valid in the schema. A row that arrives tomorrow can break it. An FD is a design decision (a real-world rule); data can only refute it.

Trivial dependency

If Y⊆XY \subseteq X, then X→YX \to Y is true in every relation: AB→AAB \to A, {stu_id, grade}→stu_id\{\text{stu\_id, grade}\} \to \text{stu\_id}. They carry no information. If the exam says “list the non-trivial FDs”, do not write these.

Armstrong’s axioms

Other FDs can be derived from a given set of FDs FF. The set of everything that can be derived is called the closure of F and is written F+F^+. Three basic rules are enough (sound and complete):

RuleStatement
ReflexivityIf Y⊆XY \subseteq X then X→YX \to Y
AugmentationIf X→YX \to Y then XZ→YZXZ \to YZ
TransitivityIf X→YX \to Y and Y→ZY \to Z then X→ZX \to Z

Practical rules derived from these:

RuleStatement
UnionIf X→YX \to Y and X→ZX \to Z then X→YZX \to YZ
DecompositionIf X→YZX \to YZ then X→YX \to Y and X→ZX \to Z
PseudotransitivityIf X→YX \to Y and WY→ZWY \to Z then WX→ZWX \to Z

Worked example. Given F={A→B, B→C, CD→E}F = \{A \to B,\ B \to C,\ CD \to E\}, derive AD→EAD \to E.

1. A -> B, B -> C           transitivity     A -> C
2. A -> C, add D            augmentation     AD -> CD
3. AD -> CD, CD -> E        transitivity     AD -> E

PITFALL · you cannot split the left side. The decomposition rule holds only for the right side. From AB→CAB \to C you do not get A→CA \to C. When you see “decomposition”, remember that you are splitting the right side.

Since F+F^+ is usually very large, it is not listed by hand. In practice the real tool is the closure of an attribute set.

Attribute closure

X+X^+: the set of all attributes that XX determines under FF. The algorithm:

result := X
repeat:
    for each  L -> M  in F:
        if L ⊆ result then  result := result ∪ M
until there is no change

Worked example. R(A,B,C,D,E,G)R(A,B,C,D,E,G), F={A→B, AC→D, B→E, DE→G}F = \{A \to B,\ AC \to D,\ B \to E,\ DE \to G\}.

{A}+    : A
          A -> B     AB
          B -> E     ABE
          (no C for AC -> D, no D for DE -> G)  →  {A,B,E}

{A,C}+  : AC
          A -> B     ABC
          AC -> D    ABCD
          B -> E     ABCDE
          DE -> G    ABCDEG   →  the whole schema

Closure answers three questions at once:

  1. Can X→YX \to Y be derived? Yes if Y⊆X+Y \subseteq X^+. {A}+={A,B,E}\{A\}^+ = \{A,B,E\}, so A→DA \to D cannot be derived but A→EA \to E can.
  2. Is XX a superkey? Yes if X+=RX^+ = R. AC is a superkey.
  3. Is XX a candidate key? Yes if it is a superkey and none of its proper subsets is a superkey. {A}+\{A\}^+ and {C}+={C}\{C\}^+ = \{C\} do not give the schema, so AC is a candidate key.

Finding all candidate keys

Brute force means 2n2^n subsets. In the exam the quick way is this observation:

  • An attribute that appears on the right side of no FD cannot be derived from anything else, so it must be in every key.
  • An attribute that appears only on the right side (never on the left) is in no minimal key.
  • First take the closure of the “mandatory” set. If it is not enough, add one attribute at a time, then two. Skip the supersets of a key you have found.

Worked example. R(A,B,C,D,E)R(A,B,C,D,E), F={AB→C, C→D, D→E, E→A}F = \{AB \to C,\ C \to D,\ D \to E,\ E \to A\}.

Never on a right side: B          → B is in every key
{B}+ = {B}                         → not enough
{A,B}+: AB -> C, C -> D, D -> E          = ABCDE   key
{B,C}+: C -> D, D -> E, E -> A, AB -> C  = ABCDE   key
{B,D}+: D -> E, E -> A, AB -> C          = ABCDE   key
{B,E}+: E -> A, AB -> C, C -> D          = ABCDE   key

Four candidate keys: AB, BC, BD, BE. You do not need to try the 3-sets, since all of them contain one of these.

An example with a cycle: R(A,B,C,D)R(A,B,C,D), F={A→B, B→C, C→A}F = \{A \to B,\ B \to C,\ C \to A\}. D is on no right side, {D}+={D}\{D\}^+ = \{D\}. Any one of A, B or C completes the cycle: the keys are AD, BD, CD.

TIP · the number of superkeys. If there is a single candidate key with kk attributes, the number of superkeys in a schema with nn attributes is 2n−k2^{n-k} (the key plus any subset of the rest). R(A,B,C,D)R(A,B,C,D) with key A: 23=82^3 = 8 superkeys. If there are several keys, be careful not to double count their intersections.

Back to the wide table, write it with letters: S = stu_id, N = stu_name, C = course_id, T = title, K = credits, D = course_dept, B = building, G = grade. F={S→N, C→TKD, D→B, SC→G}F = \{S \to N,\ C \to TKD,\ D \to B,\ SC \to G\}. S and C are on no right side, and {S,C}+\{S,C\}^+ gives everything: the only candidate key is {stu_id, course_id}. {C}+={C,T,K,D,B}\{C\}^+ = \{C,T,K,D,B\}: only a part of the key determines this many attributes. Next week we will give this a name: partial dependency.

Common mistakes

Wrong ideaThe truth
If there is no violation in the sample data, the FD is validData only refutes; an FD is a design rule
If AB→CAB \to C then A→CA \to CThe left side is not split; only the right side decomposes
One pass over the FDs is enough in closureScan again until no change; a newly added attribute can unlock an old FD
Superkey = candidate keyA candidate key is a minimal superkey
There is only one candidate keyThere can be several (AB, BC, BD, BE)
Skipping the attribute that is on no right sideThat attribute must be in every key
COUNT(DISTINCT y) also counts NULLIt does not count NULLs; check a right side that has NULLs separately

Test yourself

Exam-style questions. Solve them on paper first, then reveal the answer.

Question 1easy

In the wide table wide(stu_id, stu_name, course_id, title, credits, course_dept, building, grade), student S3 drops EE210 and we delete that row. Afterwards the database no longer knows that EE210 is called *Circuits* and has 3 credits. Which anomaly is this?

Show answer
Deletion anomaly

EE210 appears in only 1 row of the wide table (only S3 took it). When the record is deleted, the course's own information (title, credits) goes with it. Losing information you did not want to lose together with a deletion is a deletion anomaly.

Question 2easy

Is the functional dependency AB→AAB \to A trivial? Explain in one sentence.

Show answer
Yes

X→YX \to Y is trivial if Y⊆XY \subseteq X. Here {A}⊆{A,B}\{A\} \subseteq \{A,B\}, so it always holds in every relation. It carries no information.

Question 3easy

The department table contains (CS, Galata, 500000), (EE, Galata, 350000), (MATH, Rumeli, 200000), (BIO, Kadikoy, 150000). Does the FD building -> dept_name hold on this instance?

Show answer
No (Galata maps to both CS and EE)

To violate an FD, two rows with the same left-side value and different right sides are enough. Galata matches both CS and EE. Conversely, dept_name -> building holds in this instance, but an instance cannot prove that an FD is valid in the schema, it can only refute it.

Question 4medium

Write an SQL query that returns every course_id that violates the FD course_id -> title in table wide. (An empty result means the FD holds on this instance.)

Show answer
SELECT course_id FROM wide GROUP BY course_id HAVING COUNT(DISTINCT title) > 1;

Group by the left side and count how many distinct values the right side has in each group. If it is more than 1, there is a violation. On the sample data the result is empty. Note: COUNT(DISTINCT ...) does not count NULLs, so check separately if the right side can be NULL.

Question 5medium

Let R(A,B,C,D,E,G)R(A,B,C,D,E,G) with F={A→B, AC→D, B→E, DE→G}F = \{A \to B,\ AC \to D,\ B \to E,\ DE \to G\}. Compute {A}+\{A\}^+.

Show answer
{A, B, E}

Start: {A}. A→BA \to B: {A,B}. B→EB \to E: {A,B,E}. For AC→DAC \to D we need C, which we do not have. For DE→GDE \to G we need D, which we do not have. No more changes: {A}+={A,B,E}\{A\}^+ = \{A,B,E\}.

Question 6medium

Same RR and FF as above. Is ACAC a superkey of RR? Is it a candidate key?

Show answer
Yes, and it is the only candidate key

{A,C}+\{A,C\}^+: with A→BA \to B we get {A,B,C}, with AC→DAC \to D {A,B,C,D}, with B→EB \to E {A,B,C,D,E}, with DE→GDE \to G everything. It is a superkey. A and C are on the right side of no FD, so they must be in every key. {A}+\{A\}^+ and {C}+\{C\}^+ do not give the whole schema, so AC is minimal and the only candidate key.

Question 7medium

Let R(A,B,C,D,E)R(A,B,C,D,E) with F={A→C, B→D, CD→E, E→A}F = \{A \to C,\ B \to D,\ CD \to E,\ E \to A\}. Is BDBD a superkey?

Show answer
No, {B,D}+ = {B,D}

Start with {B,D}. B→DB \to D adds nothing new. A→CA \to C needs A, CD→ECD \to E needs C, E→AE \to A needs E. We have none of them. The closure stays at {B,D}, not the whole schema.

Question 8hard

Same RR and FF as the previous question. Find all candidate keys.

Show answer
AB, BC, BE

B is on no right side, so it is in every key. {B}+ = {B,D}, not enough. Add one attribute to B:
AB: with A→CA \to C, B→DB \to D, CD→ECD \to E we get everything. A key.
BC: with B→DB \to D, CD→ECD \to E, E→AE \to A we get everything. A key.
BE: with E→AE \to A, A→CA \to C, B→DB \to D we get everything. A key.
BD: {B,D}, not a key. Supersets of AB, BC, BE are not minimal; try the 3-sets that contain BD: ABD, BCD, BDE already contain a key. Result: AB, BC, BE.

Question 9medium

Let R(A,B,C,D,E,G)R(A,B,C,D,E,G) with F={A→B, AC→D, B→E, DE→G}F = \{A \to B,\ AC \to D,\ B \to E,\ DE \to G\}. Does FF imply A→DA \to D?

Show answer
No

The answer to whether X→YX \to Y can be derived is: is Y⊆X+Y \subseteq X^+? {A}+={A,B,E}\{A\}^+ = \{A,B,E\}, and D is not in it. So A→DA \to D does not follow from F. (But AC→GAC \to G does follow, because {A,C}+\{A,C\}^+ contains everything.)

Question 10hard

Given F={A→B, B→C, CD→E}F = \{A \to B,\ B \to C,\ CD \to E\}, derive AD→EAD \to E using Armstrong's axioms. Name each rule.

Show answer
A->B, B->C ⇒ A->C (transitivity); augment with D: AD->CD (augmentation); AD->CD, CD->E ⇒ AD->E (transitivity)

1) A→BA \to B and B→CB \to C, transitivity: A→CA \to C.
2) Add D to both sides, augmentation: AD→CDAD \to CD.
3) AD→CDAD \to CD and CD→ECD \to E, transitivity: AD→EAD \to E.
Check: {A,D}+={A,B,C,D,E}\{A,D\}^+ = \{A,B,C,D,E\}, and E is in it.

Question 11hard

Let R(A,B,C,D)R(A,B,C,D) with F={A→B, B→A}F = \{A \to B,\ B \to A\}. List all candidate keys and give the number of superkeys.

Show answer
ACD, BCD; 3 superkeys

C and D are on no right side: they are in every key. {C,D}+ = {C,D}. Add A: {A,C,D}+ = everything. Add B: {B,C,D}+ = everything. Both are minimal. The superkeys are the sets that contain CD and at least one of A or B: ACD, BCD, ABCD. Total 3.

Question 12easy

The instructor table has (I1, Aksoy, CS, 95000) and (I2, Demir, CS, 80000). A student claims dept_name -> salary because 'every department has a fixed salary scale'. Does the instance support this FD?

Show answer
No, CS has two salaries (95000 and 80000)

For the same dept_name (CS) there are two different salaries, so the FD is violated in this instance. What makes an FD valid is a real-world rule; instance data can only refute it. Here it is refuted. MATH violates it too, with two different salaries.

Verification: The wide table and FD violation checks were run on the sample database with node:sqlite; all closure, key and derivation answers were computed with the verify/SEN2104/lib.mjs functions (verify/SEN2104/09.mjs, 39 checks, 0 errors).

Review card

Anomalies: update (same information in many rows), insert (a course cannot be added without a student), delete (information goes with the last record) X -> Y: two rows equal on X must be equal on Y too Instance data only REFUTES an FD, it does not prove it SQL check: GROUP BY X HAVING COUNT(DISTINCT Y) > 1 (empty = no violation) Trivial: Y ⊆ X (AB -> A) Armstrong: reflexivity, augmentation (XZ -> YZ), transitivity Derived: union, decomposition, pseudotransitivity (X->Y, WY->Z => WX->Z) X+ algorithm: start with X, for every FD whose left side you contain add its right side, until nothing changes Does X -> Y follow? Y ⊆ X+ ? X is a superkey <=> X+ = R Candidate key = minimal superkey An attribute never on a right side is in EVERY key; take their closure first, then add one at a time