Introduction to DBMS Keys
The DBMS keys or the Database Management System Keys represent one or more attributes (depending on the types of the DBMS Keys used) from any table in the Database system that brings about to distinctively categorize a row and /or a combination of more than one column, to identify the relationship between the tuple (row) in the table, or to determine the relationship between the two tables, which applies to the tables that are identified and queried for analysis or reporting purposes
Different Types of Key in DBMS
There are many keys in DBMS. Let us take a look at the important keys and their functionality.
- Super Key
- Candidate Key
- Primary Key
- Alternate Key
- Foreign Key
- Compound Key
- Surrogate Key
1) Super Key
Super key is either a single key or a set of keys which helps in identifying distinct rows in a particular table. A Super key can have extra attributes that are redundant for distinct identification.
Let us look at an example where the EmpId and the Mobile number can be considered as the Super Keys.
2) Candidate Key
If a Super Key does not have any duplicate attribute, it is known as a Candidate Key. The Primary Key is carefully chosen after consideration, from the given Candidate keys. All tables are required to have one candidate key at least. There are a few rules that we need to follow regarding the selection of a Candidate Key. They are:
- A Candidate Key should comprise of distinctive values.
- A Candidate Key can have various attributes.
- A Candidate Key cannot comprise of null values.
- A Candidate Key must uniquely identify each row in the table.
Let us look at an example of a table where the Emp Id, Mobile No, and Email are the Candidate keys. These keys help us in distinctly identifying any Employee row in the table.
3) Primary Key
Primary Key is a column or a combination of columns in a relationship that helps us in uniquely identifying a row in that particular table. There can be no duplicates in a Primary Key meaning that there can be no two same values in the table. We have a few rules for choosing a key as the Primary Key. They are:
- Primary Key field cannot be left NULL and it is necessary for the Primary Key column to hold a value.
- Any two rows in the table cannot have identical values for that column.
- In case a foreign key refers to the primary key, then no value in this primary key column can be altered or modified.
Let us look at an example of a table where the Emp Id is the Primary Key.
4) Alternate Key
A table may have more than one option for a key being selected as the Primary Key. Any key that is capable of being the Primary Key, but at the moment is not the Primary Key, is known as an Alternate Key. It is a candidate key that has not been selected as the Primary Key.
Let us look at an example, where the EmpId, Email and the Mobile No. are candidate keys and are capable of being the Primary key. But because Emp Id is the Primary Key, so Email and Mobile No. become the Alternate Key.
5) Foreign Key
Foreign Keys help us in establishing relationships with other tables. It is is also called Referential Integrity. A Foreign Key column can be added to a table to establish this relationship. They help us in maintaining data integrity and allow easy navigation between any instances of two entities.
Let us look at an example comprising of two tables, Employee and Department table.
Table: Department
Table: Employee
Currently, we do not have any idea about the departments in which the employees are working. By adding the DeptId to the Employee table, we can establish a relationship between the Employee table and the Department table. Here, the DeptId of the Employee table becomes the Foreign Key and the DeptId of the Department Table becomes the Primary Key for that table.
Table: Employee with DeptId as Foreign Key
6) Compound Key
A Compound Key is a primary key that does not consist of a single column but two or more columns that allow us to distinctly identify a particular row. For a compound key, we do not have any column that is unique in itself; therefore we need to combine two or more columns to make them unique.
Let us look at an example of a table consisting of product and product details. In this table, we can see that a product can be ordered by more than one customer, and more than one product can be present in order. Therefore we need to combine both the OrderId and the ProductId to create a unique way of identifying the row.
7) Surrogate Key
A situation may arise where a particular table does not have a Primary Key. In this case, we use a Surrogate Key, which is an artificial key that can distinctly identify every row in the table. Surrogate Keys are used specifically when we do not have a natural primary key. They do not provide any relation to the table data and are usually serially ordered integers.
In this example, we have the data of Employees and their Shift timings. Therefore, we use a Surrogate Key to uniquely identify each row.
In this article, we have seen a few of the most important DBMS Keys, how they are different and when they are used.
Recommended Articles
This is a guide to DBMS Keys. Here we discuss the keys of the Database Management System which includes a super key, primary key, foreign key, etc along with examples. You may also have a look at the following articles to learn more –
- Data Models in DBMS
- RDBMS Interview Questions
- Data Integration Tool
- Guide to Specialization in DBMS
- Complete Guide to Aggregation in DBMS
7 Online Courses | 8 Hands-on Projects | 73+ Hours | Verifiable Certificate of Completion
4.5
View Course
Related Courses