DSA, Database System & Operating System β Data Models, Normalization, and SQL, NEC licence examination syllabus (Nepal Engineering Council).
Functional Dependencies
X β Y means "X determines Y" β the formal tool that turns normalization from guesswork into computation.
π Where this lives: functional dependencies are what a query optimiser reasons about when it decides it can skip a sort or eliminate a join. If the planner knows roll β name, then GROUP BY roll, name costs the same as GROUP BY roll β PostgreSQL implements exactly this optimisation for columns functionally dependent on a grouped primary key, which is why GROUP BY id lets you select any column of that table without listing it. FDs are also what data-profiling tools infer automatically to reverse-engineer a schema nobody documented. Search "functional dependency discovery algorithms"; finding all FDs in a table is a genuinely hard computational problem.
Three inference rules that are SOUND (never derive a false
FD) and COMPLETE (can derive every FD that logically
follows).
A1 REFLEXIVITY if Y β X then X β Y
(trivially, X determines its own parts)
A2 AUGMENTATION if X β Y then XZ β YZ
(adding the same attributes to both
sides preserves the dependency)
A3 TRANSITIVITY if X β Y and Y β Z then X β Z
DERIVED RULES β provable from A1βA3, and far more useful in
practice:
UNION if X β Y and X β Z then X β YZ
DECOMPOSITION if X β YZ then X β Y and X β Z
PSEUDOTRANSITIVITY
if X β Y and WY β Z then WX β Z
COMPOSITION if X β Y and Z β W then XZ β YW
PROVING UNION from the axioms β a standard exam question:
1. X β Y given
2. X β Z given
3. X β XY from 1 by augmentation
(add X to both sides)
4. XY β YZ from 2 by augmentation
(add Y to both sides)
5. X β YZ from 3 and 4 by
transitivity β
PROVING DECOMPOSITION:
1. X β YZ given
2. YZ β Y reflexivity (Y β YZ)
3. X β Y transitivity of 1, 2
4. YZ β Z reflexivity
5. X β Z transitivity of 1, 4 β
WHAT IS **NOT** VALID β the two classic traps:
β if X β Y then Y β X (FDs are DIRECTIONAL)
β if XY β Z then X β Z or Y β Z (you may NOT decompose
the LEFT side)
Counterexample for the second:
(roll, course) β grade is true
roll β grade is FALSE β a student has many
grades, one per course
That left-side rule is where most exam mistakes happen.
Decomposition applies to the RIGHT side only.
Attribute closure β the central algorithm
The CLOSURE of an attribute set X under a set of FDs F,
written XβΊ, is every attribute determined by X.
ALGORITHM (iterative, guaranteed to terminate):
XβΊ := X
repeat
for each FD A β B in F:
if A β XβΊ then XβΊ := XβΊ βͺ B
until XβΊ stops changing
return XβΊ
WHY IT MATTERS β closure answers all three key questions:
1. Does X β Y hold? β yes iff Y β XβΊ
2. Is X a superkey? β yes iff XβΊ = all attributes
3. Is X a candidate key? β yes iff XβΊ = all attributes
AND no proper subset of X
has that property
WORKED EXAMPLE β R(A, B, C, D, E, F) with
F = { A β BC, CD β E, B β D, E β A }
Compute {A}βΊ :
start {A}
A β BC {A,B,C}
B β D {A,B,C,D}
CD β E (CD β {A,B,C,D} β)
{A,B,C,D,E}
E β A already have A
no change {A,B,C,D,E}
β {A}βΊ = ABCDE. F is MISSING, so A is not a superkey.
Compute {AF}βΊ :
start {A,F}
A β BC {A,B,C,F}
B β D {A,B,C,D,F}
CD β E {A,B,C,D,E,F}
β {AF}βΊ = ABCDEF = all attributes. AF IS a superkey.
Minimal? {A}βΊ = ABCDE β all; {F}βΊ = F β all.
Neither subset works β AF is a CANDIDATE KEY. β
Compute {E}βΊ :
E β A {A,E}
A β BC {A,B,C,E}
B β D {A,B,C,D,E}
β {E}βΊ = ABCDE, so {EF}βΊ = ABCDEF β EF is a CANDIDATE KEY.
BUT DO NOT STOP THERE. Two more exist, and missing them is
the commonest error in this question type:
Compute {BC}βΊ :
start {B,C}
B β D {B,C,D}
CD β E {B,C,D,E}
E β A {A,B,C,D,E}
β {BC}βΊ = ABCDE, so {BCF}βΊ = ABCDEF
Minimal? {BF}βΊ = BDF β, {CF}βΊ = CF β
β BCF is a CANDIDATE KEY.
Compute {CD}βΊ :
CD β E {C,D,E}
E β A {A,C,D,E}
A β BC {A,B,C,D,E}
β {CD}βΊ = ABCDE, so {CDF}βΊ = ABCDEF
Minimal? {CF}βΊ = CF β, {DF}βΊ = DF β
β CDF is a CANDIDATE KEY.
ALL FOUR CANDIDATE KEYS: AF, EF, BCF, CDF
prime attributes : A, B, C, D, E, F (all of them)
non-prime attributes : none
THE LESSON: finding one or two candidate keys is not the
answer β you must find them ALL, because 2NF/3NF/BCNF are
defined in terms of EVERY candidate key. Here every attribute
turns out to be prime, which (as the next topic shows) means
this relation is automatically in 3NF.
BE SYSTEMATIC. F is mandatory (never on a right side), so
test F with each subset of {A,B,C,D,E} in increasing size,
skipping any superset of a key already found:
size 1: AF β BF β CF β DF β EF β
size 2: BCF β BDF β CDF β ...
(skip anything containing A or E β already keys)
stop when no smaller candidates remain.
THE SHORTCUT worth memorising:
Β· an attribute appearing on NO right-hand side must be in
every candidate key
Β· an attribute appearing on NO left-hand side is in no
candidate key
Here F is on no right side β F is in every candidate key.
That immediately tells you where to start.
The "attribute on no right-hand side must be in every candidate key" rule turns a search over 2βΏ subsets into a handful of closure computations. Start with the mandatory attributes, compute their closure, and add only what is still missing. For a six-attribute relation that is the difference between checking 63 subsets and checking three.
Worked example β finding all candidate keys
R(A, B, C, D, E) with F = { AB β C, C β D, D β B, A β E }
STEP 1 β classify every attribute:
on LEFT only : A (appears left, never right)
on BOTH : B, C, D
on RIGHT only : E
on NEITHER : none
β A must be in EVERY candidate key (never determined)
β E can be in NO candidate key (never determines anything)
STEP 2 β compute {A}βΊ :
{A} β(AβE) {A,E}
nothing else applies (AB β C needs B)
{A}βΊ = AE. Not a superkey β missing B, C, D.
STEP 3 β A alone is insufficient, so try A + one more:
{AB}βΊ : {A,B}
AB β C {A,B,C}
C β D {A,B,C,D}
A β E {A,B,C,D,E} = ALL β superkey
minimal? {A}βΊ=AE β, {B}βΊ=B β β CANDIDATE KEY
{AC}βΊ : {A,C}
C β D {A,C,D}
D β B {A,B,C,D}
A β E {A,B,C,D,E} = ALL β superkey
minimal? {A}βΊ=AE β, {C}βΊ=CDB β β CANDIDATE KEY
{AD}βΊ : {A,D}
D β B {A,B,D}
AB β C {A,B,C,D}
A β E {A,B,C,D,E} = ALL β superkey
minimal? {A}βΊ=AE β, {D}βΊ=DB β β CANDIDATE KEY
{AE}βΊ : {A,E} β nothing fires. Not a superkey.
STEP 4 β RESULT: three candidate keys
AB, AC, AD
PRIME attributes (in some candidate key): A, B, C, D
NON-PRIME attribute (in no candidate key) : E
That prime/non-prime split is exactly what 2NF and 3NF are
stated in terms of, which is why closure comes before
normalization.
closure_check.sql
-- FDs are schema-level claims, so SQL cannot declare them
-- directly β but you can TEST whether data violates one.
-- The test: does any determinant value map to more than one
-- dependent value?
CREATE TABLE enrolment_flat (
roll INTEGER,
course CHAR(8),
sname VARCHAR(60),
ctitle VARCHAR(60),
grade CHAR(2),
PRIMARY KEY (roll, course)
);
INSERT INTO enrolment_flat VALUES
(101, 'ACtE0703', 'Ram Bahadur', 'Database Systems', 'B'),
(101, 'AExE0101', 'Ram Bahadur', 'Circuit Theory', 'A'),
(102, 'ACtE0703', 'Sita Devi', 'Database Systems', 'A'),
(103, 'ACtE0703', 'Ram Bahadur', 'Database Systems', 'C');
-- note: roll 103 is ALSO named 'Ram Bahadur' β a different
-- person who happens to share a name.-- ===== TEST 1: does roll -> sname hold? =====
-- Look for a roll with more than one distinct sname.SELECT roll, COUNT(DISTINCT sname) AS distinct_names
FROM enrolment_flat
GROUP BY roll
HAVING COUNT(DISTINCT sname) > 1;-- (0 rows) -> NOT VIOLATED by this data, so roll -> sname
-- is consistent with what we have.-- ===== TEST 2: does sname -> roll hold? =====
SELECT sname, COUNT(DISTINCT roll) AS distinct_rolls
FROM enrolment_flat
GROUP BY sname
HAVING COUNT(DISTINCT roll) > 1;
-- sname | distinct_rolls
-- -------------+----------------
-- Ram Bahadur | 2
-- -> VIOLATED. sname -> roll is FALSE.-- ===== TEST 3: does course -> ctitle hold? =====
SELECT course, COUNT(DISTINCT ctitle) AS distinct_titles
FROM enrolment_flat
GROUP BY course
HAVING COUNT(DISTINCT ctitle) > 1;
-- (0 rows) -> consistent. course -> ctitle holds.-- ===== A REUSABLE GENERIC TEST =====
-- Any FD X -> Y is violated iff this returns rows:
-- SELECT X FROM t GROUP BY X HAVING COUNT(DISTINCT Y) > 1;
-- Data can only REFUTE an FD. Zero rows means "not
-- contradicted yet", never "proved".-- ===== enforcing an FD once you know it holds =====
-- roll -> sname means sname belongs in a student table, not
-- repeated here. That is the 2NF decomposition:
CREATE TABLE student (roll INTEGER PRIMARY KEY,
sname VARCHAR(60) NOT NULL);
CREATE TABLE course (code CHAR(8) PRIMARY KEY,
ctitle VARCHAR(60) NOT NULL);
CREATE TABLE enrolment (
roll INTEGER REFERENCES student(roll),
course CHAR(8) REFERENCES course(code),
grade CHAR(2),
PRIMARY KEY (roll, course)
);
-- Now roll -> sname is enforced STRUCTURALLY: sname is
-- stored once, so it CANNOT disagree with itself.
Closure of a set of FDs, and minimal cover
FβΊ β the closure of the FD SET F β is every FD derivable
from F. It is usually huge and never computed in full.
EQUIVALENCE: two FD sets F and G are equivalent if
FβΊ = GβΊ. Test it by checking that every FD in G is derivable
from F (using closures) and vice versa.
MINIMAL COVER (canonical cover, Fc) β the smallest FD set
equivalent to F. Three conditions:
1. every right-hand side is a SINGLE attribute
2. no FD is redundant (removing it changes FβΊ)
3. no determinant has a redundant attribute
ALGORITHM:
Step 1 split right sides: X β YZ becomes X β Y, X β Z
Step 2 remove redundant attributes from left sides:
for each FD A β b and each attribute a β A,
if b β (A β a)βΊ computed under F, drop a
Step 3 remove redundant FDs:
for each FD X β Y, if Y β XβΊ computed WITHOUT
that FD, delete it
WORKED EXAMPLE
F = { A β BC, B β C, AB β D, AC β D }
Step 1 β single right sides:
A β B, A β C, B β C, AB β D, AC β D
Step 2 β redundant LEFT attributes:
AB β D : is B redundant? {A}βΊ under F = ABCD β D β
β drop B, giving A β D
AC β D : now A β D exists, so AC β D is redundant in
step 3 anyway
Result: A β B, A β C, B β C, A β D, AC β D
Step 3 β redundant FDs:
A β C : remove it; {A}βΊ using {AβB, BβC, AβD}
= A,B,C,D β C β still derivable β DELETE
AC β D: remove it; {AC}βΊ using the rest β D β β DELETE
A β B : remove it; {A}βΊ = A,D β B not derivable β KEEP
B β C : remove it; {B}βΊ = B β C not derivable β KEEP
A β D : remove it; {A}βΊ = A,B,C β D not derivable β KEEP
MINIMAL COVER: Fc = { A β B, B β C, A β D }
Check: {A}βΊ under Fc = A,B,C,D β same as under F. β
Reduced from 4 FDs (with multi-attribute sides) to 3 simple
ones, with identical logical content.
WHY IT MATTERS: 3NF synthesis (the next topic) builds one
relation per FD in the minimal cover. A non-minimal cover
produces redundant tables, so this computation directly
determines the schema you get.
π Go further: functional dependencies are the simplest of a family. Multivalued dependencies (X β Y) capture independence between two multivalued facts about the same entity and give rise to 4NF; join dependencies give 5NF. There is also an inclusion dependency, which is exactly what a foreign key declares. And the modern twist: real-world data is dirty, so research now focuses on approximate and conditional functional dependencies β "zip β city holds for 99.7% of rows" β which is how data-cleaning tools spot errors. Search "conditional functional dependencies data cleaning".
π‘ Exam angle: state the FD definition precisely β if two tuples agree on X they must agree on Y β and stress that an FD is a schema constraint that data can refute but never prove. Know Armstrong's axioms (reflexivity, augmentation, transitivity) and be able to derive union and decomposition from them. The two guaranteed computational questions are attribute closure (XβΊ) to find candidate keys, and minimal cover. Remember the trap: you may decompose the right side of an FD but never the left.
Syllabus points
FD, closure of attributes
Armstrong's axioms
Create a free account to tick topics off, take notes as you read, watch the video lessons and get a day-by-day study plan built around your exam date.
Related topics in Data Models, Normalization, and SQL