Different Types of SQL Keys: A Comprehensive Guide

In this tutorial, we will explore the different types of keys in SQL, their purpose, and how they relate to the overall data model. By the end of this article, you will have a comprehensive understanding of the different keys in SQL and how to use them effectively in your database applications.

Table of contents

  • What Are Keys in DBMS?
  • What Are Super Keys in SQL?
  • What Are Candidate Keys in SQL?
  • What Are Primary Keys in SQL?
  • What Are Alternate Keys or Secondary Keys in SQL?
  • What Are Foreign Keys in SQL?
  • What Are Composite Keys in SQL?
  • Differences Between Various Keys in SQL
  • Conclusion

What Are Keys in DBMS?

Databases are used to store massive amounts of information stored across multiple tables. Each table might be running into thousands of rows. Needless to say, there will be many duplicate rows with redundant information. How do we deal with that? How do we manage records so that we store only unique data? And how do we relate the multiple tables that are present in the database?

SQL keys are the answer to all these queries.

An SQL key, in Database Management Systems, is either a single attribute (or column) or a set of attributes that can uniquely identify rows (or tuples) in a table.

SQL keys ensure that there are no rows with duplicate values. Not only that, but they also help in establishing a relationship between multiple tables in the database. Therefore, it becomes imperative to learn about the different keys in SQL.

What Are Super Keys in SQL?

Super key is a single key or a group of multiple keys that can uniquely identify tuples in a table.

Super Key can contain multiple attributes that might not be able to identify tuples in a table independently, but when grouped with certain keys, they can identify tuples uniquely.

Let me take an example to clarify the above statement. Have a look at the following table with the schema employees(Id, Name, Gender, City, Email, Dep_Id)
SQL Super Key

Consider that the Id attribute here corresponds to the employee id. It is unique to every employee at the table. In that case, we can say that the Id attribute can uniquely identify the tuples of this table. So, Id is a Super key of this table. Note that we can have other Super Keys, too, in this table.

For instance – (Id, Name), (Id, Email), (Id, Name, Email), etc. can all be Super keys as they can all uniquely identify the tuples of the table. This is so because of the presence of the Id attribute, which can identify the tuples uniquely. The other attributes in the keys are unnecessary. Nevertheless, they can still identify tuples.

What Are Candidate Keys in SQL?

Candidate key is a single key or a group of multiple keys that uniquely identify rows in a table.

A candidate key is a column or a combination of columns that uniquely identifies each row in a table. It is used to ensure that there are no duplicate or ambiguous records in a table.

A Candidate key is a subset of Super keys and devoid of unnecessary attributes that are not important for uniquely identifying tuples. For this reason, you can also call the Candidate key a minimal Super key.

The value for the Candidate key is unique and non-null for all tuples. It encapsulates two important constraints – the unique key constraint and the not null constraint. This ensures that values in the Candidate key do not contain any duplicate values. And every table has to have at least one Candidate key. But there can be more than one Candidate Key too.

For example, in the example that we took earlier, both Id and Email can act as a Candidate for the table as they contain unique and non-null values.

SQL Candidate key

On the other hand, we cannot use the attributes like City or Gender to retrieve tuples from the table, as they have no unique values.

Candidate Key DBMS

Whereas querying the table on the Id attribute will help us to retrieve unique tuples.

Candidate Key

What Are Primary Keys in SQL?

Primary key is the Candidate key selected by the database administrator to uniquely identify tuples in a table.

Out of all the Candidate keys that can be possible for a table, only one key will be used to retrieve unique tuples from the table. This Candidate key is called the Primary Key.

There can be only one Primary key for a table. Depending on how the Candidate Key is constructed, the primary key can be a single attribute or a group of attributes. But the important point is that the Primary key should be a unique and non-null attribute(s).

There can be two ways to create a Primary key for the table. The first way is to alter an already created to add the Primary key constraint on an attribute. This is shown below:

SQL Primary Key

Here, I have chosen the Id as the primary key attribute.

Now if I try to add a new row with a duplicate Id value, it gives me an error message.

SQL Insert

The second way of adding a Primary key is during the creation of the table itself. All you have to do is add the Primary Key constraint at the end after defining all the attributes in the table.

Primary key

To define a Primary Key constraint on multiple attributes, you can list all the attributes in the parenthesis, as shown below.

Primary Key DBMS

But remember that these attributes should be defined as non-null values; otherwise, the whole purpose of using the Primary key to identify tuples uniquely gets defeated.

 Note: Knowing the difference between the Candidate key and the Primary key is extremely important as it is a popular interview question!

What Are Alternate Keys or Secondary Keys in SQL?

Alternate keys are those candidate keys which are not the Primary key.

There can be only one Primary key for a table. Therefore all the remaining Candidate keys are known as Alternate or Secondary keys. They can also uniquely identify tuples in a table, but the database administrator chose a different key as the Primary key.

If we look at the Employee table once again, since I have chosen Id as the Primary key, the other Candidate Key (Email) becomes the Alternate key for the table.

Alternate Key

What Are Foreign Keys in SQL?

Foreign key is an attribute which is a Primary key in its parent table, but is included as an attribute in another host table.

A Foreign key generates a relationship between the parent and host tables. For example, in addition to the Employee table containing the employees’ personal details, we might have another table, Department containing information related to the department of the employee with the following schema Department(Id, Name, Location).
Foreign key

The Primary key in this table is the Department Id. We can add this attribute to the Employee by making it the Foreign key in the table. We can either do this when we are creating the table or we can alter the table later to add the Foreign Key constraint. Here I have altered the table, but creating Foreign Key during table creation is similar to that for Primary Key.

Alter Foreing key

Here, Dep_Id is now the Foreign Key in table, Employee while it is a Primary Key in the Department table.

The Foreign key allows you to create a relationship between two tables in the database, thereby ensuring normalization in relational databases. Each of these tables describes data related to a particular field (employee and department here). Using the Foreign key, we can easily retrieve data from both tables.

Foreign key query
Using Foreign keys makes it easier to update the database when required. This is so because we only have to make the necessary changes in limited rows. For example, if the Marketing department shifts from Kolkata to Pune, instead of updating it for all the relevant rows in the Employee table, we can simply update the location in the Department table. This ensures that there are only a few places to update and less risk of having different data in different places.

The concept of Foreign key is fundamental to understanding the referential integrity constraint in RDBMS or relational databases. Referential integrity ensures that any column which is declared as a foreign key in a table can contain only null values or the values which are present in the primary key of the base table. For example, the Dep_Id foreign key in the Employees table can contain only the department ids present in the Department table or the null value. Anything beyond that would break the referential integrity in a relational model.

What Are Composite Keys in SQL?

A Composite key is a Candidate key or Primary key that consists of more than one attribute.

Sometimes it is possible that no single attribute will have the property to identify tuples in a table uniquely. In such cases, we can use a group of attributes to guarantee uniqueness. Combining these attributes will uniquely identify tuples in the table.

Consider the following table:

SQL Composite key

Here, neither of the attributes contains unique values to identify the tuples. Therefore, we can combine two or more attributes to create a key uniquely identifying the tuples. For example, we can group Transaction_Id and Product_Id to create a key that can uniquely identify the tuples. These are called composite keys.

Differences Between Various Keys in SQL

  • SQL keys are used to identify rows in a table uniquely.
  • SQL keys can either be a single column or a group of columns.
  • A Super key is a single key or a group of multiple keys that can uniquely identify tuples in a table.
  • Super keys can contain redundant attributes that might not be important for identifying tuples.
  • Super keys are a superset of Candidate keys.
  • Candidate keys are a subset of Super keys. They contain only those attributes which are required to identify tuples uniquely.
  • All Candidate keys are Super keys. But the vice-versa is not true.
  • A Primary key is a Candidate key chosen to uniquely identify tuples in the table.
  • Primary key values should be unique and non-null.
  • There can be multiple Super keys and Candidate keys in a table, but there can be only one Primary key in a table.
  • Alternate keys are those Candidate keys that were not chosen to be the Primary key of the table.
  • A Composite key is a Candidate key with more than one attribute.
  • A Foreign key is an attribute that is a Primary key in its parent table but is included as an attribute in the host table.
  • Foreign keys may accept non-unique and null values.

Conclusion

In this article, we understood the importance of keys in databases. We covered the most common and widely used SQL keys that any professional looking to work with databases should know about. We also saw how to implement each of those keys in SQL and understood the differences between each type of key.

I hope this gave you a good background on keys in SQL, whether you are from computer science, data science, data analysis, or even data engineering background.

#sql #database 

Different Types of SQL Keys: A Comprehensive Guide
19.75 GEEK