Primary, Candidate and Alternate key

I am a developer from Nepal who loves playing with tech.
Search for a command to run...

I am a developer from Nepal who loves playing with tech.
No comments yet. Be the first to comment.
Have you ever translated something, only to find out later that the original text changed while you were working? Or worse — that a whole new page existed and nobody told you? For that, I built stub f
The p5.js Web Editor is an intuitive, browser-based platform for creative coding with JavaScript. It allows artists, designers, and educators to dive into coding visually, without needing to set up any development environment. But what if you're a de...

Before starting out, let me quickly share what we're building today. Open codesandbox.io or visit gsap-svg-path-animation.netlify.app. Seems interesting, let's start. Introduction You might have visited some websites where when you scroll, things mov...

Build a countdown social media og:image using nextjs and vercel/og

In the fast-paced world of web development, creating an engaging and seamless user experience is important. One effective way to achieve this is by implementing a loading animation on your website. In this tutorial, we'll explore the step-by-step pro...

Keys in DBMS are quite confusing things. So, let’s break it down into simple.
If you don’t like reading the things out then check this video, I got you covered.
A key is nothing but an attribute or set of attributes that are able to uniquely identify each record in table. (I hope here you know about database and table already.)
Let’s understand more with some examples
Student table
Here, we have this electronic table that basically has different rows and columns. So if we want to get information about any of the students then we need an attribute or a property by the help of which, we’re gonna get the information. We can use here, name as a property to get that information. If we want to get the details of Andy then we write a SQL query like this:
SELECT FROM students WHERE name = ‘Andy’;*
which returns the details of Andy. But what if we have to deal with larger records?
Students in a classroom
Here, we have a table of a classroom that has 17 students. If we observe carefully, we can find that there are students with the same name. If we try to get information about Andy, we’re gonna get information about both students who live in CA and TX. But we want to get information about Andy, who lives in CA, not in TX. For that a simple solution would be, we write the full name of a student.
Students in a classroom with full name
SELECT * FROM students WHERE First_Name = ‘Andy’ AND Last_Name = ‘Thomas’;
Now, the above query is using both First_Name and Last_Name (fullname) to identify each student in a classroom so it will return only one Andy who lives in CA.
Ok, we solved the issue with Andy. But what if we want the information about Henry who lives in NY. Again the same problem, now we have to search for the new property or so called attribute which is capable of identifying each student uniquely.

If we take Address as an unique identifier, is that going to help us? Obviously not because many students can live in the same place.
Now if we observe here, Roll no or Phone no. number can uniquely identify each student in a table. So, these attributes are the unique identifiers. It is not mandatory that we have to choose a single attribute as an unique identifier, we can choose a group of attributes to identify each record uniquely. For example, we can group First_Name and Last_Name, and call them as an unique identifier. (As we did earlier)
By the definition of candidate key, we have three candidate keys ( Roll no, {First_Name, Last_Name} & Phone no.) in this table. And we have to choose one candidate key as a primary key. But you need to look at a few things before setting any candidate key as a primary key.
Primary key cannot have a null value but candidate key can.
In this example, the Phone no. can be null since a student may or may not have a phone. But neither Roll no nor the name of the student can be null. So, Roll no and fullname ( {First_Name, Last_Name} )are the winners here.
Also if the number of student increases, then there is a possibility that some of the students can have the same First_Name and Last_Name, so the name as a whole can’t even be considered as a candidate key.
So, what we are now left off ? Clearly, Roll no can be considered as a primary key in the above example.
Our keys in this example
If candidate keys are the attributes which uniquely identify each record in a table. Like in this case, we have Roll no and Phone no.. And a capable candidate key is the primary key. Then what the heck are these alternate keys? Well it turns out that the candidate keys which weren’t able to become a primary key are considered as alternate keys. In the above example, Phone no. is an alternate key.
The attribute or set of attributes which uniquely identify each record (row) in a table are called candidate keys. In other words, the minimal super key is a candidate key.
The attribute or set of attributes which uniquely identify a single row in a table is called primary key. A table(relation) can have only one primary key and it cannot be null. Most of the time, update operation isn’t performed in primary key.
Candidate keys that has not been selected to be the primary key are called alternate keys. Alternate keys are also known as secondary keys.