• Что бы вступить в ряды "Принятый кодер" Вам нужно:
    Написать 10 полезных сообщений или тем и Получить 10 симпатий.
    Для того кто не хочет терять время,может пожертвовать средства для поддержки сервеса, и вступить в ряды VIP на месяц, дополнительная информация в лс.

  • Пользаватели которые будут спамить, уходят в бан без предупреждения. Спам сообщения определяется администрацией и модератором.

  • Гость, Что бы Вы хотели увидеть на нашем Форуме? Изложить свои идеи и пожелания по улучшению форума Вы можете поделиться с нами здесь. ----> Перейдите сюда
  • Все пользователи не прошедшие проверку электронной почты будут заблокированы. Все вопросы с разблокировкой обращайтесь по адресу электронной почте : info@guardianelinks.com . Не пришло сообщение о проверке или о сбросе также сообщите нам.

Database Normalization in SQL — 1NF, 2NF, and 3NF Explained (Student–Course Case Study)

Sascha Оффлайн

Sascha

Заместитель Администратора
Команда форума
Администратор
Регистрация
9 Май 2015
Сообщения
1,565
Баллы
155
? Objective

Database normalization
is one of the most important concepts in database design. It ensures that data is stored efficiently, redundancy is minimized, and data integrity is maintained.

In this tutorial, we’ll walk through First Normal Form (1NF), Second Normal Form (2NF), and Third Normal Form (3NF) — step by step — with clear SQL examples using a Student–Course–Instructor scenario.

By the end, you’ll understand not only how to normalize a table, but why it matters.

? Base Table — The Starting Point

Let’s begin with a simple (but flawed) table design that stores students, their enrolled courses, and instructor details:


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.



At first glance, this table might look fine — it gives us all the details in one place. But let’s look deeper.

⚠ Data Anomalies in the Base Table

  • Insertion anomaly: You can’t add a new course unless a student enrolls in it.
  • Update anomaly: If Dr. Kumar changes his phone number, you must update it in every row where he appears.
  • Deletion anomaly: If Priya withdraws from DBMS, all information about the course “DBMS” and Dr. Kumar may be lost.

To overcome these issues, we apply Normalization — a step-by-step process of structuring the database.

? Step 1: Convert to 1NF (First Normal Form)

✅ Rule:
Every attribute (column) must contain atomic values — no repeating groups or arrays.

Our table already follows 1NF since each field holds only a single value (no lists or sets).
However, we’ll still define it formally in SQL to set a proper structure.


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.



➕ Insert Sample Data


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.



? Note: Even though this satisfies 1NF, redundancy still exists — course and instructor details are repeated multiple times.

? Step 2: Convert to 2NF (Second Normal Form)

✅ Rule:
Remove partial dependencies — every non-key attribute should depend on the entire primary key, not just part of it.

In the current table, the composite key could be (StudentID, CourseID), but columns like CourseName, Instructor, and InstructorPhone depend only on CourseID.
This violates 2NF.
So, we’ll split the data into separate tables: Student, Course, and Enrollment.

? Student Table
Stores only student-related details.


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.



? Course Table
Stores course information and the instructor details.


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.



? Enrollment Table
Connects students and courses, forming a many-to-many relationship.


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.



➕ Insert Data


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.




Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.



✅ Now: Each table has data that depends entirely on its key.
We’ve removed redundancy between students and courses, but one more dependency remains — between instructors and phone numbers.

? Step 3: Convert to 3NF (Third Normal Form)

✅ Rule:
Remove transitive dependencies — non-key attributes should not depend on other non-key attributes.
In our case, InstructorPhone depends on Instructor, not directly on CourseID.
That’s a transitive dependency.
To fix it, we’ll create a separate Instructor table.

?‍? Instructor Table
Stores instructor names and phone numbers independently.


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.



? Course Table (Revised)
Now, instead of storing instructor details directly, we’ll link each course to its instructor via InstructorID.


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.



➕ Insert Data


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.



✅ Now: Each column depends only on the key — there’s no transitive dependency, and data updates are easier, safer, and cleaner.

? Step 4: Query — Combining All Tables Using JOINs
Finally, let’s bring everything together and view the full data using JOIN statements.


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.



? Output:


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.



✅ The query clearly shows the relationship between students, courses, and instructors — all retrieved efficiently from the normalized schema.

? Conclusion

Normalization is more than a theoretical rule — it’s a practical design principle that helps you build efficient, scalable, and error-free databases.

By applying 1NF → 2NF → 3NF:

  • We eliminated redundant data
  • Prevented update, insertion, and deletion anomalies
  • Created a clean, modular design for easier maintenance

Through this example, we’ve seen how a complex, redundant table can be systematically broken down into clean, well-structured tables following 1NF, 2NF, and 3NF.



Источник:

Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.

 
Вверх Снизу