Database Normalization is a technique of organizing the data in the database. Normalization is a systematic approach of decomposing tables to eliminate data redundancy(repetition) and undesirable characteristics like Insertion, Update and Deletion Anomalies. It is a multi-step process that puts data into tabular form, removing duplicated data from the relation tables
Normalization is used for mainly two purposes:
If a table is not properly normalized and have data redundancy then it will not only eat up extra memory space but will also make it difficult to handle and update the database, without facing data loss. Insertion, Updation and Deletion Anomalies are very frequent if database is not normalized. To understand these anomalies let us take an example of a Student table.
In the table above, we have data 2 of students. As we can see, data for the fields department, dept_head and office_phone is repeated for the students who are in the same department in the college, this is Data Redundancy.
Suppose for a new admission,Data of the student of department cannot be inserted, or else we will insert the department information as NULL.
Also, if we will insert data of 100 students of same department, then the department information will be repeated for all those 100 students.
hese scenarios are nothing but Insertion anomalies.
What if Mr. X leaves the college? or is no longer the dept_head of computer science department? In that case all the student records will have to be updated, and if by mistake we miss any record, it will lead to data inconsistency. This is Updation anomaly.
In our Student table, two different informations are kept together, Student information and department information. Hence, at the end of the academic year, if student records are deleted, we will also lose the department information. This is Deletion anomaly.
Normalization rules are divided into the following normal forms:
it should follow the following 4 rules:
it should follow the following 2 rules:
From the following table we can see
In this table, student_id is the primary key and will be unique for every row, hence we can use student_id to fetch any row of data from this table
Even for a case, where student names are same, if we know the student_id we can easily fetch the correct record.
Hence we can say a Primary Key(student_id) for a table is the column or a group of columns(student_id,name)(composite key) which can uniquely identify each record in the table.
I can get department name of student with student_id 1 Similarly,I can get name of student with student_id 1 or 2. So all I need is student_id and every other column depends on it, or can be fetched using it.
This is Dependency and we also call it Functional Dependency.
We can see from Subject and Score tables:
From Subject table,subject_id is a primary key
From Score table,student_id and subject_id together make a candidate key for this table this can be primary key
We can not get marks of student with student_id 1,because we don't know for which subject. Similarly if I give you subject_id, you would not know for which student. Hence we need student_id + subject_id to uniquely identify any row.
Now if you look at the Score table, we have a column names teacher which is only dependent on the subject, for Java it's Java Teacher and for C++ it's C++ Teacher & so on.
Now as we just discussed that the primary key for this table is a composition of two columns which is student_id & subject_id but the teacher's name only depends on subject, hence the subject_id, and has nothing to do with student_id.
This is Partial Dependency, where an attribute/field in a table depends on only a part of the primary key and not on the whole key.
We can see from following table where teacher does not depend on a part of primary key:
Keys are very important part of Relational database model. They are used to establish and identify relationships between tables and also to uniquely identify any record or row of data inside a table.
A Key can be a single attribute/field or a group of attributes/field, where the combination may act as a key.
Super Key is defined as a set of attributes within a table that can uniquely identify each record within a table. Super Key is a superset of Candidate key.
In the table defined above super key would include student_id, (student_id, name), phone etc where (student_id, name) is a candidate key
Candidate keys are defined as the minimal set of fields which can uniquely identify each record in a table.
It is an attribute/field or a set of attributes/field that can act as a Primary Key for a table to uniquely identify each record in that table. There can be more than one candidate key.
In our example, student_id and phone both are candidate keys for table Student.
Primary key is a candidate key that is most appropriate to become the main key for any table. It is a key that can uniquely identify each record in a table
For the above Student we can make the student_id column as the primary key.
Key that consists of two or more attributes/field that uniquely identify any record in a table is called Composite key. But the attributes/fields which together form the Composite key are not a key independentely or individually.
In above table student_id and subject_id together will form the primary key, hence it is a composite key.
The candidate key which are not selected as primary key are known as secondary keys or alternative keys.
Non-key attributes are the attributes or fields of a table, other than candidate key attributes/fields in a table.
Non-prime Attributes are attributes other than Primary Key attribute(s)
Let's we have 3 tables,Student, Subject and Score
In the Score table, we need to store some more information, which is the exam name and total marks, so let's add 2 more columns to the Score table.
With exam_name and total_marks added to our Score table, it saves more data now. Primary key for our Score table is a composite key, which means it's made up of two attributes or columns ? student_id + subject_id.
Our new column exam_name depends on both student and subject. For example, a mechanical engineering student will have Workshop exam but a computer science student won't. And for some subjects you have Prctical exams and for some you don't. So we can say that exam_name is dependent on both student_id and subject_id.
Well, the column total_marks depends on exam_name as with exam type the total score changes. For example, practicals are of less marks while theory exams are of more marks.
But, exam_name is just another column in the score table. It is not a primary key or even a part of the primary key, and total_marks depends on it.
This is Transitive Dependency. When a non-prime attribute depends on other non-prime attributes rather than depending upon the prime attributes or primary key.
Again the solution is very simple. Take out the columns exam_name and total_marks from Score table and put them in an Exam table and use the exam_id wherever required.
Boyce-Codd Normal Form or BCNF is an extension to the third normal form, and is also known as 3.5 Normal Form.
In the table above
in the table above student_id, subject together form the primary key, because using student_id and subject, we can find all the columns of the table.
there is a dependency between subject and professor here, where subject depends on the teacher name.
This table satisfies the 1st Normal form because all the values are atomic, column names are unique and all the values stored in a particular column are of same domain.
This table also satisfies the 2nd Normal Form as their is no Partial Dependency.
And, there is no Transitive Dependency, hence the table also satisfies the 3rd Normal Form.
But this table is not in Boyce-Codd Normal Form.
In the table above, student_id, subject form primary key, which means subject column is a prime attribute.
But, there is one more dependency, teacher -> subject.
And while subject is a prime attribute, professor is a non-prime attribute, which is not allowed by BCNF.
Below we have the structure for both the tables.
Fourth Normal Form comes into picture when Multi-valued Dependency occur in any relation. In this tutorial we will learn about Multi-valued Dependency, how to remove it and how to make any table satisfy the fourth normal form.
A table is said to have multi-valued dependency, if the following conditions are true
If all these conditions are true for any relation(table), it is said to have multi-valued dependency
As you can see in the table above, student with s_id 1 has opted for two courses, Science and Maths, and has two hobbies, Cricket and Hockey.
Well the two records for student with s_id 1, will give rise to two more records, as shown below, because for one student, two hobbies exists, hence along with both the courses, these hobbies should be specified.
And, in the table above, there is no relationship between the columns course and hobby. They are independent of each other.
So there is multi-value dependency, which leads to un-necessary repetition of data and other anomalies as well.
To make the above relation satify the 4th normal form, we can decompose the table into 2 tables.
Now this relation satisfies the fourth normal form.