![](images/colors.jpg)
![](../images/bg1.jpg)
Introduction of Keys in DBMS
![](profiles/dav.jpg)
Data is undeniably a critical asset for companies, driving decisions, strategies, and operations. In the early stages of a business or project, managing a small quantity of data can be straightforward, often relying on simple methods or basic tools. However, as the volume of data grows significantly over time, challenges emerge—especially regarding application performance. Frequent searches, complex processing, and real-time data management demand a robust solution. This is where databases become indispensable. Databases provide structured, scalable, and efficient mechanisms to store, retrieve, and process data while maintaining integrity and security. By optimizing data management, databases ensure that applications continue to perform seamlessly, even under the pressure of high data loads, enabling businesses to scale and thrive in a data-driven world.
Today, we are going to discuss the critical strategy and concept of database keys, a foundational aspect of database management that significantly enhances performance and resolves common challenges efficiently
What are the Keys in DBMS?
In simple words, Keys are used to uniquely identifyany record or row (or tuple) of data from the table (or in a relation). It is also used to establish and identify relationships between tables.
Keys are important for maintaining data integrity and efficient retrieval.
Keys in DBMS can be single or a group of attributes that uniquely identify the records. Using all attributes as a key is less efficient than choosing the minimum combination of attributes that provide the same result.
Example
Aadhar card number is a unique identification number is used to identify citizens in India.These can also help find all the available details maintained on the server about the candidate, such as their address, passport number, or phone number are keys unique to each citizen.
Why are the Keys required?
In today’s world, an extensive quantity of data is accessible. We are using different cloud platforms to save our data.There are numerous tables to accommodate this data. These tables may contain many duplicate records, both sorted and unsorted. Consequently, retrieving a distinct or particular record from these tables without constraints or limitations poses a formidable challenge.
To address these challenges we have a concept called “Keys”. Keys serve the purpose of guaranteeing the absence of duplicate records within rows or tuples.
Let us take a real-life example
to understand why keys are required in database. Let’s consider Indian government databases of each citizen living in India. What attribute of the citizen do you think will uniquely identify each of them? You could refer to a citizen by using their name, birth place, dob and current place. Or, you can mention only the Aadhar card number of the citizen, and you can get all the other details from that.
A key could either be a combination of more than one attribute (or columns)or just a single attribute. The main motive of this is to give each record a unique identity.
Different types of Keys in DBMS?
- Primary Key
- Candidate Key
- Super Key
- Foreign Key
- Composite Key
- Alternate Key
- Unique Key
Let’s understand each of them separately
1) Primary Key
PK (Primary Key) is a field or a set of fields with values that are unique throughout a table Or, in other words Primary Key refers to a column that uniquely identifies all the records within that table.
A table can have only one primary key constraint.
All columns defined within PrimaryKey constraint must be defined as NOT NULL.
Primary key must not contain repeated or duplicated values across its rows. Each value within the primary key must be unique, with no repetitions allowed.Applying the PK constraint to a column or set of columns ensures that they cannot have null values or duplicates. Furthermore, any foreign key that references the primary key is unable to modify the values present in the primary key.
Example: Aadhar Card number is a PK in below Citizen table.
Citizen Table Structure
Column Name | Data Type | Constraint | Description |
AadharCardNumber |
|
| Unique 12-digit Aadhar Card number |
FullName |
|
| Full name of the citizen |
DateOfBirth |
|
| Citizen's date of birth |
Address |
| Address of the citizen | |
PhoneNumber |
| 10-digit phone number (optional) | |
| Email ID of the citizen (optional) |
Now Its your turn
Tell me what can be Primary key in Student table?
Column Name | Data Type | Constraint | Description |
RegistrationNumber | INT | PRIMARY KEY | Unique registration number for each student |
FullName | VARCHAR(100) | NOT NULL | Full name of the student |
DateOfBirth | DATE | NOT NULL | Student's date of birth |
Branch | VARCHAR(50) | NOT NULL | Engineering branch (e.g., Computer Science) |
Year | INT | NOT NULL | Year of study (1st, 2nd, etc.) |
ContactNumber | CHAR(10) | Contact number of the student (optional) | |
VARCHAR(100) | Email ID of the student (optional) |
Yeah You got it Correct
It will be Registration Number because this number will me every student unique across all students even from other departments / section and historically as well.
2) Super Key
In simple words, two or more attributes in a table can together identify a table record uniquely, so the combination of such attributes are Super Key.
we can define super key as a set of those keys that identify a row or a tuple uniquely.
The word super denotes the superiority of a key. Thus, a super key is the superset of a key known as a Candidate key (we will discuss it in next section). It means a candidate key is obtained from a super key only.
Example: Set of Super Key in above Citizen table
AadharCardNumber
AadharCardNumber , FullName
AadharCardNumber , FullName , Email
Now, How DBA chooses Primary Key ?
the answer is Primary Key is identified from Super Key which is super set. Primary Key is the single attribute from super key which can uniquely identify the record or row in a table. It is a responsibility of a DBA to choose most suitable attribute for Primary Key out of Super key or Candidate key.
Example : In our above example AadharCardNumber is most suitable and appropriate PK.
Now Its your turn
Tell me what can be Super key in Student table?
3) Candidate Key
Candidate Key is a subset of Super Key. Also, like other keys it uniquely identifies record or tuple and, value of Candidate Key cannot be NULL.
Let’s understand Candidate Key using our above Citizen table.
Out of above Super Keys, subset which can uniquely identify a record in a table are
AadharCardNumber
so these are our Candidate Keys "Minimal representation of a tuple"
Now, the question comes how Primary Key is different from Candidate Key ?
The selection of a Candidate Key often leads to the designation of a Primary Key.
The purpose of both Candidate Key and Primary Key are same but, still both are different. We can have one or more candidate key in a table but only one Primary Key is created for a table. We can select most appropriate Key from Candidate Key for Primary Key ie. AadharCardNumber in our example.
If a table has one Candidate Key then it can be considered for both.
Now Its your turn
Tell me what can be Candidate key in Student table?
4) Alternate Key
Candidate Key which is not chosen for Primary Key is Alternate Key. It serves as alternative unique identifier for a record in a table.
Example: Email is a alternate key or secondary key.
Thus, AadharCardNumber is the Primary Key and the remaining identified keys will be the candidate keys, hence the alternate key.
5) Composite Key
known as a Compound Key as well
A Composite key is the two or more attributes that together can uniquely identify a tuple in a table. Also, where each attribute creating a key is a foreign key in its own right.
Now question comes how PK is different from Composite key ?
a primary key is a single column that uniquely identifies a record in a database table, while a composite key is a combination of columns.
Database designers use composite keys to ensure data integrity and improve data retrieval speed.They are particularly useful when a single column is not enough to guarantee a row's uniqueness.
Let’s understand composite key using an example.Suppose In an e-commerce platform like Amazon, when a customer places multiple orders, their details are saved in a CustomerOrder table. Since CustomerOrderID
alone cannot ensure uniqueness (as customers may place multiple orders with the same ID across different products), a composite key is required.
Structure of Customer Order Table
Column Name | Data Type | Constraint | Description |
CustomerID | INT | NOT NULL | Unique ID for each customer |
CustomerName | VARCHAR(100) | Full name of the customer | |
CustomerNumber | CHAR(10) | Contact number of the customer | |
CustomerAddress | VARCHAR(255) | Address of the customer | |
CustomerOrderID | INT | NOT NULL | Unique ID for each order |
CustomerOrderDetails | VARCHAR(255) | Details of the order | |
ProductID | INT | NOT NULL | Unique ID of the product ordered |
Now, to identify a unique record in such table we need combination of attributes I.e., CustomerID and CustomerOrderID as under one customer there can be multiple order placed.
6) Foreign Key
A foreign key is a field (or a collection of fields) in one table that establishes a link between the data in two tables. It is a constraint that enforces a relationship between the primary key of one table and a column in another table.
Foreign Key (FK) creates connectivity between two tables.
FK columns of one table points to the primary key attribute of the other table.
PK column set in one table will act as FK column in another table. Also,
FK column (or column in referenced table) can accept Null Values.
Foreign keys help define the relationships between tables and ensure data integrity.
A foreign key constraint is a database rule that enforces referential integrity.This means that if a value in the foreign key column is updated or added, the same value must already exist in the referenced column.
Let’s under FK constraint using above e-commerce table, CustomerOrder. Say we have another table ProductDetails which contains all product details Like
Product Details Table
Column Name | Data Type | Constraint | Description |
ProductID | INT | PRIMARY KEY | Unique ID for each product |
ProductName | VARCHAR(100) | NOT NULL | Name of the product |
ProductSubName | VARCHAR(100) | Sub-category or additional details |
Now, ProductID in CustomerOrder is Foreign Key constraint and also, PK in ProductDetails table.
In the ProductDetail table, the field ProductID is a Primary Key because it is uniquely identifying all other fields of the ProductDetail table. On the other hand, ProductID is a Foreign key attribute for the CustomerOrder table because it is acting as a referencing attribute. It means that both the ProductDetail and CustomerOrderDetail table are linked with one another because of the ProductID attribute.
So now when we insert a new row in the CustomerOrder table, the foreign key constraint ensures data integrity by validating that the ProductID
exists in the referenced ProductDetails table. This mechanism prevents invalid rows from being added to the database.
7) Unique Key
Unique key constraint is a column or set of columns that ensures that each row in a table has unique information.Unique keys are used to prevent duplicate values in a column and can store NULL values.
Unique keys are used to monitor the reliability of data by prohibiting copy or null entries in the designated columns.
How Unique key is different from Primary Key?
Primary key field can never have a Null value as its value, whereas a unique key can have one of its field values as Null.
a table can have more than one Unique key column but cannot have more than one Primary Key.
Let’s understand Unique Key using an example, in this our CustomerOrderDetail table
1.CustomerID is unique, but it’s not the unique key because it’s specifically generated for each customer and might not be used externally.
2.CustomerEmail is chosen as the unique key because it uniquely identifies each customer.No two customers can have the same email address in this scenario.
Conclusion
Keys play a very vital role in designing a database. These concepts also help to find the difference between good and bad database design. Keys characteristics of establishing integrity and identifying the relationship between two tables are their most vital feature, allowing users to edit data with precision while maintaining uniqueness. From alternate keys to composite, the goal is to separate the identity of each record through unique keys defining separate qualities.
Finally, we have prepared an exercise for you to test your understanding of database keys.
Exercise
for your practice purpose, We have given 3 tables ie. Player, Game and PlayerGameStats. Understand table structure and provide all Primary Key, Candidate Key, Super Key, Foreign Key, Composite Key, Alternate Key, and Unique Key in each table if present . If you have any doubt, you can ask me in comments.
Player Table
Column Name | Data Type |
PlayerID | INT |
VARCHAR(100) | |
Username | VARCHAR(50) |
PhoneNumber | VARCHAR(15) |
PlayerName | VARCHAR(100) |
Game Table
Column Name | Data Type |
|
|
|
|
|
|
PlayerGameStats
Column Name | Data Type |
|
|
|
|
|
|
|
|