Blog/
Types of Keys in DBMS: Everything You Need to Know

Types of Keys in DBMS: Everything You Need to Know

By Rincy John, Technical Writer at Webandcrafts with 13 years of experience in Software Development and Technical Writing. She has a diverse background in cross-platform full-stack development, Software Testing and Writing. She is passionate about acquiring new skills and is adaptable to changing technology.
  • Published in Blog on June 11, 2025
  • Last Updated on June 17, 2025
  • 10 mins read
Types of Keys in DBMS

Think about a global e-commerce platform with millions of customers, orders, and products. One wrong click can vanish a customer’s order. Here comes the DBMS to organise data for businesses.

Every DBMS has keys that work behind the scenes to ensure the data is accurate, accessible, and secure. In his book Database Design and Relational Theory, C.J Date discusses using keys to safeguard data integrity and shape data.

DBMS is like a vast library; the keys are the catalogue system to help you find the right book. Understanding of the types of DBMS keys is required to manage your data in an orderly fashion.

Let's use some examples to help us understand the various types of keys in DBMS and their roles. We'll encounter a range of keys, from primary keys to foreign keys. Let's get started!

What Are the Keys in DBMS?

Keys in a Database Management System (DBMS) are attributes, or a set of attributes, to uniquely identify the records in a table and the relationships between tables. Keys are necessary for data integrity, as they prevent duplication and assure accurate and rapid retrieval of data, as well as maintaining the relationship through entity and referential integrity.

For instance, a Customer ID, which serves as the primary key in a customer database, distinguishes individuals who may have the same name. The primary key ensures that each record is uniquely identifiable. Different forms of keys act as safeguards to maintain the uniqueness of records. To create, read, update, or delete (CRUD) a record, it must be uniquely identified so the system knows exactly which one to work with. Uniqueness helps ensure that changes are applied to the correct record. Overall, keys in a DBMS preserve order and reliability, enabling organisations to trust their data and make better decisions, leading to improved operational efficiency.

Ready to Build Better Databases?

Let's talk

Loading...

8 Types of Keys in DBMS at a Glance

Type of Key

Description

Role in DBMS

Primary Key

A unique attribute (or set of attributes) that identifies each record in a table. Cannot contain a null value.Ensures every record is uniquely identifiable, preventing duplicates.

Candidate Key

A minimal set of attributes that can uniquely identify a record.Acts as a potential primary key; multiple candidate keys may exist in a table.

Super Key

A set of attributes that can uniquely identify a record, including extra attributes.Serves as a superset of candidate keys, capable of uniquely identifying records, but not necessarily minimal.

Foreign Key

A set of attributes in one table that links to the primary key of another table.Establishes and enforces relationships between tables for data consistency.

Alternate Key

A candidate key that is not selected as the primary key.Serves as a secondary unique identifier for records.

Composite Key

A key consisting of two or more attributes to uniquely identify a record.Used when a single attribute isn’t enough to ensure uniqueness.

Unique Key

An attribute that ensures all values are unique, but can allow one null value.Enforces uniqueness across records, often used for fields like email addresses.

Secondary Key

An attribute used for searching and indexing but not for unique identification.Enhances query performance by enabling faster data retrieval.

What Are the Different Types of Keys in DBMS?

1. Candidate Key

A candidate key is a minimal group of attributes that uniquely identifies each row in the table, and there can be many candidate keys. A candidate key has to be unique and non-null; no two rows can have the same value. In a university database, a StudentID and Email can be candidate keys for students, as both can uniquely identify students in the university system.

StudentIDEmailNameDepartment
S1john@ins.comJohn DanComputer Science
S2smith@ins.comSmith JoeMathematics

SQL Query

create_students_table.sqlSQL
1CREATE TABLE Students (2   StudentID VARCHAR(20) NOT NULL, 3   Email VARCHAR(100) NOT NULL,4   Name VARCHAR(100),5   Department VARCHAR(100),6   PRIMARY KEY (StudentID),7   UNIQUE (Email)8);

StudentID can be a candidate key
Email can also be a candidate key (Unique constraint)
PRIMARY KEY: StudentID is the primary key
UNIQUE: Email is a candidate key (but not selected as Primary Key)

2. Primary Key

A primary key is a unique identifier that identifies each record in a table. Primary keys are critical to data integrity since they cannot have null values and contain unique values to show that their records can be distinct. For example, a resident management system might have UserID as a primary key, where UserID will uniquely identify a user and their credentials and activity without duplicating records or confusion.

UserIDUsernameEmail
101annann@email.com
102benben@email.com

SQL Query

create_users_table.sqlSQL
1CREATE TABLE Users ( 2UserID INT NOT NULL, 3Username VARCHAR(50), 4Email VARCHAR(100), 5PRIMARY KEY (UserID) 6);

UserID column, which is the Primary Key
PRIMARY KEY: UserID uniquely identifies each user

3. Foreign Key

A foreign key is defined as a set of attributes in one table that refers to the primary key and unique key of another table, creating a relationship and ensuring referential integrity. This ensures consistent data relationships between tables. For example, refer to the following tables. The Purchases table uses ClientID as a foreign key to link to the Clients table to ensure that orders are being made by valid customers.

Clients:

ClientIDName
1Emy Jack
2Egan Jose

Purchases:

PurchaseIDClientIDPurchaseDate
100112025-01-01
100222025-02-02

SQL Query

create_clients_table.sqlSQL
1CREATE TABLE Clients ( 2ClientID INT NOT NULL, 3Name VARCHAR(100), 4PRIMARY KEY (ClientID) 5); 

Primary Key: ClientID (uniquely identifies each customer)

create_purchases_table.sqlSQL
1CREATE TABLE Purchases (2    PurchaseID INT NOT NULL,3    ClientID INT NOT NULL,4    PurchaseDate DATE,5    PRIMARY KEY (PurchaseID),6    FOREIGN KEY (ClientID) REFERENCES Customers(ClientID)7);

ClientID column (Foreign Key references the Clients table)
Primary Key: PurchaseID (uniquely identifies each purchase)
Foreign Key: ClientID (references ClientID in the Clients table)

4. Super Key

A super key is a set of attributes that can uniquely identify a record in a table, including both minimal and non-minimal combinations of attributes. Every candidate key is a super key, but not all super keys are candidate keys, as they may include extra attributes.

For example, in a library database, BookID, Title, and Author together can have the possibility to be a super key, but BookID alone would be a candidate key.

BookIDTitleAuthor
B001DatabaseJoseph
B002BasicsRuth

SQL Query

create_books_table.sqlSQL
1CREATE TABLE Books (2BookID VARCHAR(20) NOT NULL,3Title VARCHAR(100),4Author VARCHAR(100),5PRIMARY KEY (BookID)6);

BookID, Title, Author: Super key (can uniquely identify records, but not minimal)

5. Alternate Key

An alternate key is a candidate key that is not selected for the primary key but can still be used to identify records uniquely. Once a primary key is determined, the rest of the candidate keys are called alternate keys and can be used as alternate unique identifiers.

For example, in an employee database, if EmployeeID is the primary key, then Email is an alternate key (must not be null) and can be used for lookups because it can be used to identify employees uniquely.

EmployeeIDEmailName
E001anna@company.comAnna Lawrance
E002diana@company.comDiana Martin

SQL Query

create_employees_table.sqlSQL
1CREATE TABLE Employees (2    EmployeeID VARCHAR(20) NOT NULL,3    Email VARCHAR(100) NOT NULL,4    Name VARCHAR(100),5    PRIMARY KEY (EmployeeID), 6    UNIQUE (Email)             7);

EmployeeID as the Primary Key. Email as an Alternate Key

6. Composite Key

A composite key consists of two or more attributes that together uniquely identify a record in a table. It is required when a single attribute cannot ensure uniqueness, often in many-to-many relationships.
E.g., if we had a course enrollment system, we could have a composite key that consists of both StudentID and CourseID. Now we can uniquely track each student's enrollment in each specific course, including duplication of Student and Course values across the table.

StudentIDCourseIDEnrollmentDate
S001C0012025-01-05
S002C0012025-02-19

SQL Query

create_enrollment_table.sqlSQL
1CREATE TABLE Enrollment ( 2StudentID INT NOT NULL, 3CourseID INT NOT NULL, 4EnrollmentDate DATE, 5PRIMARY KEY (StudentID, CourseID) 6);

Composite Key consisting of StudentID and CourseID

7. Unique Key

A unique key has a single but slightly different function from a primary key. A unique key guarantees that all values in a particular column are unique from one another and may accept null values depending on the DBMS (a primary key does not). Unique keys are commonly used to implement uniqueness for non-primary attributes, like email addresses or phone numbers, where one null entry may be permissible.

CustomerIDEmailName
1meera@unity.comMeera Jish
2NULLAlexa Alex

SQL Query

create_customers_table.sqlSQL
1CREATE TABLE Customers ( 2CustomerID INT NOT NULL, 3Email VARCHAR(100) UNIQUE, 4Name VARCHAR(100), 5PRIMARY KEY (CustomerID)6 );

CustomerID as a unique identifier and the Primary Key
Email is a Unique Key

8. Secondary Key

A secondary key is an attribute for searching or indexing, but it does not uniquely identify the records. The purpose of a secondary key is for improving query performance, not for maintaining uniqueness. A possible real-world scenario is a ProductName in a product inventory system. The ProductName is an indexed Secondary key to speed up product searches, for improved usability in an app for retail.

ProductIDProductNamePrice
P001Laptop1999.88
P002Smartphone589.77

SQL Query

create_products_table.sqlSQL
1CREATE TABLE Products ( 2ProductID INT NOT NULL, 3ProductName VARCHAR(100), 4Price DECIMAL(10,2), 5PRIMARY KEY (ProductID), 6INDEX idx_product_name (ProductName) 7);

ProductID as a unique identifier and the Primary Key
ProductName is indexed, acting as a Secondary Key

Importance of Keys in Database Management

1. Uniqueness and Identification
The primary and unique keys in a DBMS ensure uniqueness since they prevent duplication and confusion of data. For example, the ProductID that identifies products in a retail database.

2. Relationships Between Tables
In a relational database, foreign keys link tables by connecting a column in one table (like CustomerID in Orders) to a primary key in another (Customer). This keeps data accurate and consistent. In e-commerce, it helps prevent errors, speeds up queries, and supports systems like CRM, supply chain, and finance.

3. Data Integrity
Data integrity in a DBMS is maintained using keys like primary, foreign, and unique keys. Primary keys ensure each record is unique, while foreign keys prevent invalid references. For example, in healthcare, a PatientID uniquely identifies patients, and foreign keys ensure only registered doctors are linked to them. This is crucial for compliance with laws like GDPR and HIPAA. Good key management helps prevent costly mistakes and protects a business's reputation.

4. Efficient Data Retrieval
Keys in a DBMS help quickly find records by indexing primary and secondary keys, which boosts speed and accuracy. For example, indexing DeliveryDate in logistics lets users quickly track shipments. In real-time systems like online banking or e-commerce, this can improve data retrieval by enhancing user experience and operational efficiency.

5. Optimised Query Execution
Keys in a DBMS are essential for fast query execution and efficient database performance. Indexing primary, unique, or secondary keys speeds up complex queries and reduces computing load, saving time and money. For instance, a composite key like AccountID and TransactionDate can help financial services filter transactions quickly with minimal processing impact. This leads to lower infrastructure costs, better performance, and scalable growth without losing speed or reliability.

Common Misconceptions About Keys in DBMS

1. Candidate Key vs. Super Key: They Are Not the Same

Misconception: Candidate keys are often confused with super keys as interchangeable terms.

Clarification: A super key may possess redundant attributes, whereas a candidate key is a ‘minimal super key’, meaning that its attributes will never have a subset that can uniquely identify records.

Example: In the context of a library database, {BookID, Title, Author} is a super key, however, BookID alone is a candidate key as it gives the ability to uniquely identify the book independently of the other fields, provided it exists.

Importance: Recognising this distinction is beneficial in accurately modelling a database schema with properties that add to efficiency, and minimise storage expense as well as query performance penalties.

2. Foreign Key Doesn’t Have to Be Unique

Misconception: A foreign key has to be unique in its table.

Clarification: A foreign key references a primary key or unique key from another table and does not need to be unique. It may (and often will) have duplicate values.

Importance of Not Having to Be Unique: This feature allows for the possibility of one-to-many or many-to-many relationships in the database.

Example From Life: In an e-commerce database, the Orders table will have a CustomerID foreign key. The Orders table will allow multiple orders to share the same foreign key as a way to indicate that a customer can place multiple orders.

Customers:

CustomerIDName
C001Sona Thomas
C002Neena Susan

Orders:

OrderIDCustomerIDOrderDate
1001C0012025-01-01
1002C0012025-02-02
1003C0022025-03-03

Here, CustomerID in the Orders table is a foreign key and is not unique, as C001 appears multiple times.

3. Primary Key Can Be Composite

Misconception: A primary key must be a single attribute. 

Clarification: A primary key can refer to either a single attribute or a composite key (combination of attributes). A primary key is an attribute or a group of attributes that is used to uniquely identify records.
 
Real World Example: In an Enrollment table, the StudentID and CourseID attributes form a composite key that will identify a unique enrollment. 

Importance: Composite keys let you use multiple columns as one key. This helps avoid single-column limits, cuts down data repetition, and improves design, especially in many-to-many setups like students and courses.

SQL Query

create_enrollment_table.sqlSQL
1CREATE TABLE Enrollment ( 2StudentID INT NOT NULL, 3CourseID INT NOT NULL, 4EnrollmentDate DATE, 5PRIMARY KEY (StudentID, CourseID) 6);
StudentIDCourseIDEnrollmentDate
S001C1012025-01-05
S001C1022025-02-16
S002C1012025-03-11

Here, {StudentID, CourseID} is a composite key, ensuring each enrollment is uniquely identified.

Conclusion

Keys in Database Management Systems (DBMS) are vital to organising a database and establishing integrity within the database. The basic types of keys include primary keys, foreign keys, candidate keys, super keys, alternate keys, composite keys, unique keys, and secondary keys. Each key has a specific function for data relationships and data integrity. When designing a schema, DBMS keys should be carefully chosen as they can impact data retrieval, data integrity, and a business's performance. Poor choices of DBMS keys can result in problems, including data duplication, slow queries, etc.

Ready to take your database performance to the next level? Reach out to Webandcrafts for high-level databases and optimise your keys, boost speed, and ensure long-term database efficiency.

Want to Optimize Your DBMS?

Let's talk

Loading...