Difference between Primary Key and Candidate KeyKeys in MySQL are an attribute or a set of attributes used to access records from tables. They are also used to construct a relationship between two tables. Both Primary and Candidate Key identifies each record uniquely in a table or relation. The most popular difference among them is that a table can have only one primary key but can have more than one candidate key. In this article, we are going to compare essential differences between Primary and Candidate Keys based on various parameters. Before making a comparison, we will discuss in brief about these keys. What is Primary Key?The primary key is a unique or not-null key that uniquely identifies each record in a table or relation. It is a minimal super key; that's why it can contain only one primary key in any relationship. For example, suppose we have a table named students with ID, Name, Age, and Address columns. Here we can make only the ID column a primary key because all other column values can be the same, but the ID column can't be the same. What is Candidate Key?A candidate key is an attribute or a set of attributes that identify each record in a table or relation uniquely but noted that a table could contain multiple candidate keys. This key can store a NULL value that opposes in a primary key. For example, suppose we have a table named students with ID, Name, DOB, Age, and Address columns. Here we can figure out two candidate keys that are {ID} and {Name, DOB}. Hence, it clarifies that more than one candidate key is available to identify the table or relation uniquely. Since a table or relation can have more than one candidate key, one candidate key can qualify to become a primary key out of all candidate keys. Note that each candidate key can be a primary key, but only one should be chosen as the primary key. The rule to become a primary key among candidate keys is that the key's attribute values must be unique and can never be Null for any domain. Key differences between Primary and Candidate KeyThe following points explain the main differences between primary and candidate keys:
Primary Key vs. Candidate Key Comparison ChartThe following comparison chart explains their main differences in a quick manner:
ConclusionIn this article, we have made a comparison between primary and candidate key. Here we conclude that it is optional to specify a primary key for a relation. But if we are declaring a relationship, candidate keys must be present in that relation. Next TopicMySQL Interview Questions |