AfterAcademy Tech
•
07 Jan 2020

In the last blog, we have learned about Primary Keys and we saw how to find the primary key from a given relation. Apart from the primary key, there is one key called a unique key. Hold on, what is this unique key? Is it the same as that of primary key or there is some difference between these two? In this blog, we will learn about the unique key and we will also see the difference between primary key and unique key. So let's get started by revising the primary key.
The primary key is the minimal set of attributes which uniquely identifies any row of a table. The primary key cannot have a NULL value. It cannot have duplicate values. Primary key enforces the integrity of an entity type. Since a row represents a single instance of a table or entity, the primary key helps in enforcing the integrity of an entity type.
Example: We have a STUDENT table where Roll_no is the Primary key of the table. This Roll_no can be used to uniquely identify the rows of a table.

Note: A table can have only one Primary Key.
Unique Key is a key which has a unique value and is used to prevent duplicate values in a column. But, the primary key also has a unique value for each column. So, how is it different from a primary key? A unique key can have a NULL value which is not allowed in a primary key. But, if a unique key can have NULL values then how can it implement uniqueness in a column? By uniqueness, we mean that if we have a value present then either it would be unique or no value would be present at all. So, the main motive of a unique key in a table is to prevent duplicate values. To clearly understand this let us take an example.
Example: We have a STUDENT table.

In the above table, we have Phone_no as the Unique Key and Roll_no as the Primary Key. So, we can understand if any student has a Phone_no then it should be unique. Also, there might be some student who doesn't have any Phone_no.
Note: A table can have multiple Unique Key.
Example: In the Student Table, we have only one Primary Key i.e Roll_no but we have two Unique Key i.e Phone_no and Registration_no.

Example: In the Student Table, Unique key Phone_no can have a NULL value whereas the Primary Key cannot have any NULL value.

Example: In the given Student table, the primary keys are sorted according to the key values and hence are they create a clustered index. The unique key like Phone_no and the Registration_no have the data stored in the physical files and the pointers are used to link the indices with the files.

Note: Unique Key + Not Null!= Primary Key. This is beacuse the values in the primary key are stored in the clustered index but the values in the unique key are stored in the non-clustered index.
This is how a Primary key differs from a Unique key. Hope you learned something new today.
Do share this blog with your friends to spread the knowledge. Visit our YouTube channel for more content. You can read more blogs from here.
Keep Learning 🙂
Team AfterAcademy!
AfterAcademy Tech
In this blog, we will learn about the various keys like the super key, candidate key, primary key, alternate key and foreign key. We will discuss how to identify and differentiate between them with examples.

AfterAcademy Tech
In this blog, we will learn the difference between SQL and MySQL. People use these terms intechangeably. But both are having different meanings.

AfterAcademy Tech
In this blog, we will learn what is RDBMS and how it is different from DBMS. People often use these words intechangeably. But there is a difference between these two terms. So, let's learn how.

AfterAcademy Tech
In this blog, we will learn how WHERE and HAVING clause are used and then see what are the main differences between the WHERE clause and HAVING clause.
