-
Notifications
You must be signed in to change notification settings - Fork 95
Expand file tree
/
Copy path29.Normalization
More file actions
172 lines (133 loc) · 5.61 KB
/
Copy path29.Normalization
File metadata and controls
172 lines (133 loc) · 5.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
My complete SQL Masterclass Notes in Tanglish is now live! Covers 30+ topics with syntax, examples, and step-by-step explanations.
Perfect for both beginners and professionals to master SQL easily. Check it out here 👉 https://topmate.io/dataengineering/1697791
====================================================================================
Normalization in SQL is a systematic process of organizing the columns (attributes) and tables (relations) of a relational database to minimize data redundancy
and improve data integrity. In simpler terms, normalization helps ensure that each piece of data is stored in the best possible place,
reducing duplication and making it easier to maintain and update the database.
Why Normalize?
Reduce Data Redundancy: Storing the same data multiple times can waste space and cause inconsistencies if that data needs to change.
Improve Data Integrity: Ensures that your data follows certain rules, preventing errors.
Make Maintenance Easier: Updates, deletions, and insertions become simpler and more efficient when data is well-organized.
Common Normal Forms
The most commonly taught normal forms are:
First Normal Form (1NF)
Second Normal Form (2NF)
Third Normal Form (3NF)
(There are higher normal forms like 4NF and 5NF, but they are more specialized and not always required for most practical applications.
Typically, going up to 3NF or BCNF is sufficient.)
1. First Normal Form (1NF)
Definition & Purpose (1NF)
1NF requires that every column holds only atomic (indivisible) values and that there are no repeating groups.
Purpose: To ensure each field contains a single piece of data.
Before
-- Non-1NF Table: contains multi-valued phone_numbers in a single column
CREATE TABLE Students_Non1NF (
student_id INT,
student_name VARCHAR(100),
phone_numbers VARCHAR(100) -- e.g., '123-4567,987-6543'
);
-- Sample data insertion (non-atomic phone numbers)
INSERT INTO Students_Non1NF (student_id, student_name, phone_numbers)
VALUES (1, 'Alice', '123-4567,987-6543'),
(2, 'Bob', '555-1212');
After
-- Main Students table with atomic values
CREATE TABLE Students (
student_id INT PRIMARY KEY,
student_name VARCHAR(100)
);
-- Separate table for phone numbers, ensuring each phone number is atomic
CREATE TABLE StudentPhones (
student_id INT,
phone VARCHAR(20),
PRIMARY KEY (student_id, phone),
FOREIGN KEY (student_id) REFERENCES Students(student_id)
);
-- Insert students
INSERT INTO Students (student_id, student_name)
VALUES (1, 'Alice'),
(2, 'Bob');
-- Insert phone numbers (each phone number in its own row)
INSERT INTO StudentPhones (student_id, phone)
VALUES (1, '123-4567'),
(1, '987-6543'),
(2, '555-1212');
2. Second Normal Form (2NF)
Definition & Purpose (2NF)
2NF requires the table to be in 1NF and that all non-key columns are fully functionally dependent on the entire primary key.
Purpose: To remove partial dependencies (where a column depends on only part of a composite key).
Before
-- This table is in 1NF but not in 2NF because course details depend only on course_id.
CREATE TABLE Enrollment_Non2NF (
student_id INT,
course_id INT,
course_name VARCHAR(100),
instructor VARCHAR(100),
PRIMARY KEY (student_id, course_id)
);
-- Sample data insertion
INSERT INTO Enrollment_Non2NF (student_id, course_id, course_name, instructor)
VALUES (1, 101, 'Intro to SQL', 'Dr. Smith'),
(2, 101, 'Intro to SQL', 'Dr. Smith'),
(1, 102, 'Database Design', 'Dr. Jones');
After
-- Table recording enrollments (relationship)
CREATE TABLE Enrollment (
student_id INT,
course_id INT,
PRIMARY KEY (student_id, course_id),
FOREIGN KEY (course_id) REFERENCES Courses(course_id)
);
-- Table holding course details
CREATE TABLE Courses (
course_id INT PRIMARY KEY,
course_name VARCHAR(100),
instructor VARCHAR(100)
);
-- Insert course details
INSERT INTO Courses (course_id, course_name, instructor)
VALUES (101, 'Intro to SQL', 'Dr. Smith'),
(102, 'Database Design', 'Dr. Jones');
-- Insert enrollment data
INSERT INTO Enrollment (student_id, course_id)
VALUES (1, 101),
(2, 101),
(1, 102);
3. Third Normal Form (3NF)
Definition & Purpose (3NF)
3NF requires that the table is in 2NF and that all the columns are directly dependent on the primary key
(i.e., no transitive dependencies).
Purpose: To remove transitive dependencies where a non-key column depends on another non-key column.
Before
-- This table is in 2NF but has a transitive dependency:
CREATE TABLE Courses_Non3NF (
course_id INT PRIMARY KEY,
course_name VARCHAR(100),
instructor VARCHAR(100),
instructor_office VARCHAR(100)
);
-- Sample data insertion
INSERT INTO Courses_Non3NF (course_id, course_name, instructor, instructor_office)
VALUES (101, 'Intro to SQL', 'Dr. Smith', 'Room 101'),
(102, 'Database Design', 'Dr. Jones', 'Room 102');
After
-- Revised Courses table (now 3NF): holds course-specific data
CREATE TABLE Courses (
course_id INT PRIMARY KEY,
course_name VARCHAR(100),
instructor VARCHAR(100),
FOREIGN KEY (instructor) REFERENCES Instructors(instructor)
);
-- New table for instructor details
CREATE TABLE Instructors (
instructor VARCHAR(100) PRIMARY KEY,
instructor_office VARCHAR(100)
);
-- Insert instructor details
INSERT INTO Instructors (instructor, instructor_office)
VALUES ('Dr. Smith', 'Room 101'),
('Dr. Jones', 'Room 102');
-- Insert courses with reference to instructors
INSERT INTO Courses (course_id, course_name, instructor)
VALUES (101, 'Intro to SQL', 'Dr. Smith'),
(102, 'Database Design', 'Dr. Jones');