Tell me about normal forms (1NF, 2NF, 3NF)
Normal forms are rules for organizing tables in a relational database that help reduce data duplication and improve integrity. The main ones are 1NF, 2NF, and 3NF.
1NF (First Normal Form)
- The rule: every field in the table must be atomic, meaning it can't be split into parts, and every record is unique.
- What this means in practice: you can't store several values in one field, e.g. "Phones = 12345, 67890". Every value needs to be its own record.
- An example that violates 1NF:
| ID | Name | Phones |
|---|---|---|
| 1 | Ivan | 12345, 67890 |
- Correct (1NF): create a separate
Phonestable withEmployeeIDandPhonefields.
2NF (Second Normal Form)
- The rule: the table must be in 1NF, and every field must fully depend on the primary key, not just part of it (this matters for composite keys).
- What this means in practice: if the primary key is made of two columns, a separate column shouldn't depend on only one of them.
- An example that violates 2NF:
A table with the key
(StudentID, CourseID)and aStudentNamefield.StudentNamedepends only onStudentID, not on the whole key. - The fix: move
StudentIDandStudentNameinto a separateStudentstable.
3NF (Third Normal Form)
- The rule: the table must be in 2NF, and there's no transitive dependency, meaning a field shouldn't depend on another field besides the primary key.
- What this means in practice: you shouldn't store data that can be computed or looked up through another field.
- An example that violates 3NF:
| EmployeeID | DepartmentID | DepartmentName |
DepartmentNamedepends onDepartmentID, not directly onEmployeeID. - The fix: create a
Departmentstable withDepartmentIDandDepartmentName, and keep onlyDepartmentIDin theEmployeestable.
Put simply:
- 1NF: fields are atomic, no repeating groups.
- 2NF: every field fully depends on the primary key.
- 3NF: no field depends on another non-key field.
Short Answer
Interview readyPremium
A concise answer to help you respond confidently on this topic during an interview.