What is the difference between Primary and Super Key?
Whenever we need data, we can easily get it from the tables in our database.A table has rows and columns.The data of any column can be accessed directly by the name of the column.How do we get the data of a particular row?We have the name of every column so we can easily find it, but rows don't have a name.How can we pick a row?We need to know any property or hint which will allow us to pick any row from many rows.The hint which will help us in doing so is the key if we are able to do this.We will learn about the different types of keys used in the database management system.Let's start.
A key is an attribute or set of attributes that help us identify the rows of a table.Establishing relationship among tables is helped by it.With the help of examples, we will see how this is done.
We don't know which Andrew to call because there are two students with the same name.If we know the age of student 'Andrew', we can't distinguish between him and the other student.We can distinguish and identify the students through some value.The Roll_no will help us identify the rows in the table.The key is Roll_no.There will be no confusion if we know the Roll_no of the student.
A super key is a combination of all possible attributes which can uniquely identify the rows in a table.It is not necessary for a superkey to uniquely identify the rows in the table.
Each row is uniquely identified by the above keys.Each key is a super key.By using Roll_no only, we can uniquely identify the rows, but if you are making a super key, you will try to find all the possible cases of keys that can be used to identify data uniquely.
A minimal super key is a candidate key.The minimum attribute required to uniquely identify the table is what is called a minimal superkey.All candidate keys are super keys because it was selected from the set of the super key.Candidate Keys can't have any values.
If the subset of the candidate key is a super key, it's not a valid key.
All of the 6 super keys can't become a candidate key.Only those keys with no redundant attributes would become a candidate key.
We conclude from the above discussion that we can only have 2 out of 6 super keys as the candidate key.I.e.Roll and registration no.
Any row of a table is uniquely identified by the minimal set of attributes.It was selected from a set of keys.A candidate key can become a primary key.It is done by the Database Administrator.The primary key can't have a value.It can't have a duplicate value.
In the above example, we saw that we have two candidate keys.We can choose any key as the primary key for our table from this set.It depends on what we need.The primary key is more logical if we are talking about class.
Since we have made 'Roll_no' as the primary key, our alternate key would be 'REGISTRATION_NO'.
The foreign key of a table is the attribute.The foreign key points to the primary key of the table.
A foreign key can be used to establish a relationship between two tables.The foreign key in the Student table is the link between the table and the course table.We can use the foreign key to find the information about the course if we need to.