Courses / Computer Science XII (HSSC-II) – FBISE Complete SLO-Based Course

3.6 Data Normalisation: 1NF, 2NF and 3NF

Unit 3 – Programming Fundamentals · 90 min read · 7 MCQs to check yourself

Decomplexify – learn database normalisation (1NF, 2NF, 3NF and beyond)

1. Why normalise?

Normalisation organises tables to reduce redundancy (repeated data) and avoid anomalies:

Anomaly Example in a single big Orders table
Update Ahmed Khan moves from Karachi – his city must be changed in many rows; missing one makes data inconsistent
Insert A new product cannot be added until someone orders it
Delete Deleting Bilal’s only order also deletes the fact that Keyboard is product P003

Dependencies

Normal form Rule
1NF Each cell holds one (atomic) value; no repeating groups; each row unique (has a primary key)
2NF In 1NF and no partial dependency – every non-key field depends on the whole key
3NF In 2NF and no transitive dependency – non-key fields depend only on the key (“the key, the whole key, and nothing but the key”)

2. Worked example (FBISE model paper)

Starting table – Orders:

OrderID CustomerName CustomerCity ProductID ProductName Quantity
101 Ahmed Khan Karachi P001 Laptop 2
101 Ahmed Khan Karachi P002 Mouse 1
102 Sana Baloch Lahore P001 Laptop 1
103 Bilal Ahmed Islamabad P003 Keyboard 3

The key is (OrderID, ProductID) – each row is one product in one order. The table is in 1NF (atomic values).

Dependencies: OrderID → CustomerName → CustomerCity · ProductID → ProductName · (OrderID, ProductID) → Quantity.

Step to 2NF – remove partial dependencies: customer details depend only on OrderID; ProductName depends only on ProductID.

Step to 3NF – remove the transitive dependency OrderID → CustomerName → CustomerCity by giving customers their own table:

Customers

CustomerID CustomerName CustomerCity
C1 Ahmed Khan Karachi
C2 Sana Baloch Lahore
C3 Bilal Ahmed Islamabad

Orders

OrderID CustomerID
101 C1
102 C2
103 C3

Products

ProductID ProductName
P001 Laptop
P002 Mouse
P003 Keyboard

OrderDetails

OrderID ProductID Quantity
101 P001 2
101 P002 1
102 P001 1
103 P003 3

Now each fact is stored once: changing Ahmed’s city is one update; a new product can be added at any time; deleting an order does not delete product information.

3. Another example – 1NF

RollNo Name Subjects
1 Ali CS, Physics

“Subjects” holds two values – not 1NF. Fix: one row per subject, or a separate table StudentSubjects(RollNo, Subject).

Short and long questions

1. A school keeps one table: Enrolment(StudentID, StudentName, ClassTeacher, TeacherPhone, CourseID, CourseName, Grade), key (StudentID, CourseID). Identify the anomalies and normalise the table to 3NF, showing each step and the final tables with keys. (2 + 4)

Model answer

Anomalies (2): StudentName and CourseName are repeated in many rows (update anomaly); a course cannot be added until a student enrols (insert anomaly); deleting the last student in a course deletes the course name (delete anomaly).

Dependencies: StudentID → StudentName, ClassTeacher; ClassTeacher → TeacherPhone (transitive); CourseID → CourseName; (StudentID, CourseID) → Grade.

1NF: already atomic with key (StudentID, CourseID). 2NF (remove partial dependencies): Students(StudentID, StudentName, ClassTeacher, TeacherPhone); Courses(CourseID, CourseName); Enrolment(StudentID, CourseID, Grade). 3NF (remove transitive dependency ClassTeacher → TeacherPhone) (4):

  • Students(StudentID PK, StudentName, ClassTeacher FK)
  • Teachers(ClassTeacher PK, TeacherPhone)
  • Courses(CourseID PK, CourseName)
  • Enrolment(StudentID FK, CourseID FK, Grade) – composite PK (StudentID, CourseID).

2. Differentiate between partial and transitive dependency with examples.

Model answer
  • Partial dependency: a non-key attribute depends on part of a composite key. In (OrderID, ProductID, ProductName, Qty), ProductName depends only on ProductID.
  • Transitive dependency: a non-key attribute depends on another non-key attribute. In (StudentID, DeptID, DeptName), StudentID → DeptID → DeptName.

3. Define 1NF, 2NF and 3NF.

Model answer
  • 1NF: every field holds atomic (single) values, no repeating groups, each record unique with a primary key.
  • 2NF: in 1NF and every non-key field depends on the whole primary key (no partial dependency).
  • 3NF: in 2NF and no non-key field depends on another non-key field (no transitive dependency).

4. What is normalisation? Explain update, insert and delete anomalies.

Model answer

Normalisation is organising tables to reduce redundancy and remove anomalies, using normal forms (1NF, 2NF, 3NF).

  • Update anomaly: repeated data must be changed in many rows; missing one makes data inconsistent.
  • Insert anomaly: some data cannot be added without other unrelated data (a product before any order).
  • Delete anomaly: deleting a row accidentally removes other facts (deleting the only order deletes the product details).

5. Convert the following table from Second Normal Form (2NF) to Third Normal Form (3NF): Orders(OrderID, CustomerName, CustomerCity, ProductID, ProductName, Quantity) with rows (101, Ahmed Khan, Karachi, P001, Laptop, 2), (101, Ahmed Khan, Karachi, P002, Mouse, 1), (102, Sana Baloch, Lahore, P001, Laptop, 1), (103, Bilal Ahmed, Islamabad, P003, Keyboard, 3).

Model answer

Dependencies: OrderID → CustomerName → CustomerCity (transitive); ProductID → ProductName; (OrderID, ProductID) → Quantity. 3NF tables:

  • Customers(CustomerID PK, CustomerName, CustomerCity): C1 Ahmed Khan Karachi; C2 Sana Baloch Lahore; C3 Bilal Ahmed Islamabad
  • Orders(OrderID PK, CustomerID FK): 101 C1; 102 C2; 103 C3
  • Products(ProductID PK, ProductName): P001 Laptop; P002 Mouse; P003 Keyboard
  • OrderDetails(OrderID, ProductID – composite PK, Quantity): 101 P001 2; 101 P002 1; 102 P001 1; 103 P003 3 Each fact is now stored once – no redundancy or anomalies.

This lesson ends with a quiz of 7 MCQs. Open the lesson on PrepOasis to take it and save your progress.

Previous: 3.5 Databases, Keys and SQL: Create, Insert, Update, Delete and Select · Next: 3.7 Connecting Python to a Database (SQLite)

All lessons in Computer Science XII (HSSC-II) – FBISE Complete SLO-Based Course