⬅ Previous Topic
Dynamic SQLNext Topic ⮕
SQL Denormalization⬅ Previous Topic
Dynamic SQLNext Topic ⮕
SQL DenormalizationDatabase design is like planning a school timetable — it must be clean, consistent, and conflict-free. Normalization helps us organize data in a way that removes redundancy, improves integrity, and makes maintenance easier. In this tutorial, we’ll walk through the key normal forms — 1NF, 2NF, 3NF, and BCNF — using relatable school-based examples.
Normalization is the process of restructuring a relational database to reduce data redundancy and improve data integrity. Each “normal form” adds a layer of structure and removes anomalies in insertions, deletions, and updates.
CREATE TABLE student_subjects (
roll_no INT,
name VARCHAR(50),
subjects VARCHAR(100) -- e.g., "Maths, Science"
);
The subjects column contains multiple values. This violates 1NF.
CREATE TABLE student_subjects_1nf (
roll_no INT,
name VARCHAR(50),
subject VARCHAR(30)
);
CREATE TABLE result (
roll_no INT,
subject VARCHAR(30),
student_name VARCHAR(50),
marks INT,
PRIMARY KEY (roll_no, subject)
);
student_name
depends only on roll_no
, not the whole key (roll_no, subject). That’s a partial dependency.
CREATE TABLE students (
roll_no INT PRIMARY KEY,
student_name VARCHAR(50)
);
CREATE TABLE marks (
roll_no INT,
subject VARCHAR(30),
marks INT,
PRIMARY KEY (roll_no, subject),
FOREIGN KEY (roll_no) REFERENCES students(roll_no)
);
CREATE TABLE student_details (
roll_no INT PRIMARY KEY,
name VARCHAR(50),
city VARCHAR(30),
pincode INT
);
pincode
depends on city
, not directly on the primary key. That’s a transitive dependency.
CREATE TABLE students (
roll_no INT PRIMARY KEY,
name VARCHAR(50),
city VARCHAR(30)
);
CREATE TABLE cities (
city VARCHAR(30) PRIMARY KEY,
pincode INT
);
Let’s say a teacher is assigned uniquely to each subject, but a teacher can teach only one subject.
CREATE TABLE subject_teacher (
subject VARCHAR(30),
teacher_name VARCHAR(50),
room_no INT,
PRIMARY KEY (subject, room_no)
);
subject → teacher_name
but subject
is not a superkey here. This violates BCNF.
CREATE TABLE subjects (
subject VARCHAR(30) PRIMARY KEY,
teacher_name VARCHAR(50)
);
CREATE TABLE subject_rooms (
subject VARCHAR(30),
room_no INT,
PRIMARY KEY (subject, room_no),
FOREIGN KEY (subject) REFERENCES subjects(subject)
);
Normalization is the foundation of clean, scalable databases. From splitting repeating values in 1NF to eliminating complex dependencies in BCNF, each normal form brings you closer to efficient and error-free data design. Especially in large-scale systems like school databases, normalization ensures your structure remains solid as data grows.
Up next: Denormalization – when and why you might intentionally break normalization rules for performance.
StudentID, CourseID, InstructorName
Which form is violated if InstructorName depends only on CourseID?⬅ Previous Topic
Dynamic SQLNext Topic ⮕
SQL DenormalizationYou can support this website with a contribution of your choice.
When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.