DBMS keys

Margi patel
Analytics Vidhya
Published in
4 min readJan 20, 2021
Image source

In the beginning study of the DBMS keys, we are confused about the DBMS keys concepts because there are many different types of DBMS keys, and almost all are related to each other with a slight difference, which makes it very difficult to get a clear understanding of the time. In this blog, you will learn the following concepts.

  1. What are DBMS keys?
  2. Why we need DBMS keys?
  3. What the different types of DBMS keys?

What are DBMS keys?

Keys in DBMS are an attribute or set of attributes in a database table to uniquely identify a row in the table.

Why we need DBMS keys?

  • Without a DBMS key, we can’t retrieve the records uniquely from the table. Because without a key, we will either retrieve multiple records or incorrect records.
  • To maintain the integrity of data and identity of data. By making an attribute as a key and making it mandatory, we can enforce that anything inserted in the table is identifiable.
  • To generate relationships among different database tables and identify a relationship between tables.

What the different types of DBMS keys?

The database supports the following types of keys.

  1. Super key

2. Candidate key

3. Primary key

4. Alternate key

5. Foreign key

6. Composite key

7. Compound key

8. Unique key

9. Natural key

10. Surrogate key

Customer_Details
Order_Details

Super key

The set of attributes that can uniquely identify a tuple is known as Super Key. Also, it is a combination of Candidate keys that uniquely identify a record. For example, {Customer_Id}, {Email}, {Phone_No}, {SSN}, {Customer_Id, SSN}, {Email, SSN}, {Email, Phone_No} and many more combination we have as a super key from the Customer_Details table.

Candidate key

Candidate keys are the subset of super keys, which have a minimal number of columns to define a row uniquely. A candidate key is like a unique key to identify the row. A candidate key is a super key but vice versa not possible.

It allows NULL values.

For example, we have so many options for super keys but some of the forms are an unnecessary pairing of keys such as {Customer_Id, SSN}, {Email, SSN}, {Email, Phone_No}, {SSN, Phone_No} but {Customer_Id}, {Email}, {Phone_No} and {SSN} can individually uniquely identify a row of data so this four data are the minimal set of candidate keys.

Primary key

There can be more than one candidate key in relation. From those keys, you will make one key a primary key. Here we have four candidate keys so we can make anyone a primary key.

The user and system both can insert the records with some conditions. A primary key can be a surrogate key, but the reverse is not possible.

It doesn’t allow duplicate and NULL values.

Alternate key

All the remaining candidate keys which are not selected as a primary key are called alternate keys.

It allows duplicate and any number of NULL values.

Foreign key

A foreign key helps to define the relationship with another table. It shows the relationship between two tables. It also helps to maintain data integrity for tables in the relationship. For example, {Customer_id} is a foreign key because it shows which customer has placed the order.

Composite key

Any key with more than one attribute is called a composite key. When we don’t have a primary key at that time we can choose multiple columns to find unique records. For example, {Customer_FirstName, Customer_LastName, Customer_city} is a composite key. We can take any number of columns, but a minimum of two columns are required to make a composite key.

It allows null and duplicate values.

Compound key

If a foreign key is present in a composite key, then it is called a Compound key.

Unique key

It shouldn’t have a duplicate value, but it allows only one NULL value.

Natural key

Sometimes the primary key is made up of real data is referred to as a natural key. For example, {SSN} or {Email} we can take as a natural key.

Surrogate key

When a primary key is generated at runtime, it is called a surrogate key. It doesn’t allow a duplicate value, but it might have a NULL value. It is typically a numeric value. When a primary key is not present, we can use a surrogate key. For example, you place one order to buy a laptop then in your purchase bill you can see one system generated invoice number, it is known as a surrogate key. For example, we can take {Customer_id} as a surrogate key if it is generated by the system.

I hope after reading this blog, you have a better understanding of DBMS keys.

If you like the blog or feel it helpful, please leave a clap!

Thank you.

--

--