CSC265 ยท 2079 ยท 5 marks
Define functional dependencies. Explain 2nd normal form with example.
Define functional dependencies. Explain 2nd normal form with example. [5]
Quick answer
A functional dependency is a constraint between two sets of attributes in a relation. For a relation R, a functional dependency X โ Y means that the value of attribute set X uniquely determines the value of attribute set Y.
Functional Dependencies and Second Normal Form (2NF)
Functional Dependency (FD)
- X is called the determinant
- Y is called the dependent
Example:
In a Student table, StudentID โ StudentName means that knowing the StudentID uniquely determines the StudentName.
A set of functional dependencies is given for each relation, and each relation has a designated primary key, which is then evaluated for normalization. (from notes)
Second Normal Form (2NF)
"To be in second normal form, a relation must be in First Normal Form AND the relation must not contain any partial dependency." (from notes)
Key Definitions:
- Non-prime attribute: An attribute that is not part of any candidate key
- Partial Dependency: When a proper subset of a candidate key determines a non-prime attribute
Partial Dependency = Proper subset of candidate key โ non-prime attribute
Conditions for 2NF:
- The relation must be in 1NF
- There must be no partial dependency (every non-prime attribute must be fully functionally dependent on the whole candidate key)
Example
Table NOT in 2NF:
| StudentID | CourseID | StudentName | CourseName | Grade |
|---|---|---|---|---|
| S01 | C01 | Ram | DBMS | A |
| S01 | C02 | Ram | OS | B |
| S02 | C01 | Sita | DBMS | A+ |
- Primary Key (Composite): {StudentID, CourseID}
- Functional Dependencies:
StudentID, CourseID โ Grade(full dependency - OK)StudentID โ StudentName(partial dependency - violation)CourseID โ CourseName(partial dependency - violation)
Since StudentName depends only on StudentID (a proper subset of the composite key), this is a partial dependency, so the table is NOT in 2NF.
Decomposition to achieve 2NF:
Table 1: Student
| StudentID | StudentName |
|---|---|
| S01 | Ram |
| S02 | Sita |
Table 2: Course
| CourseID | CourseName |
|---|---|
| C01 | DBMS |
| C02 | OS |
Table 3: Enrollment
| StudentID | CourseID | Grade |
|---|---|---|
| S01 | C01 | A |
| S01 | C02 | B |
| S02 | C01 | A+ |
Now every non-prime attribute is fully dependent on the entire primary key. All three tables are in 2NF.
Summary
| Concept | Description |
|---|---|
| 1NF required | Yes |
| Partial dependency | Not allowed |
| Full dependency | Every non-prime attribute depends on the whole candidate key |
Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.