What Are The Types of Keys in DBMS? Examples, Usage, and Benefits
By Rohan Vats
Updated on Apr 01, 2025 | 19 min read | 339.6k views
Share:
For working professionals
For fresh graduates
More
By Rohan Vats
Updated on Apr 01, 2025 | 19 min read | 339.6k views
Share:
Table of Contents
How many types of keys in DBMS are there? If you are not aware of the answer to this query, don't fret! You are at the right place. In a DBMS, there are eight main types of keys: super key, primary key, candidate key, alternate key, foreign key, composite key, unique key, and surrogate key. These keys make every record in a table stand out by assigning a unique identifier and maintaining orderly relationships when multiple tables interact.
You might wonder why they hold so much importance. Different types of Keys prevent confusion when you deal with large datasets. They also define how different tables should connect, which supports more efficient searches.
This article will walk you through the different types of keys in a DBMS. By the end, you will understand how each key type fits into the bigger picture of reliable database design.
Relational databases, also known as SQL databases, rely on different key types to keep each record distinct and to connect data across multiple tables. Each type handles its own task, such as avoiding duplicates or linking related tables.
Below is a quick view of eight fundamental types of keys in DBMS and what they do.
Keys in DBMS |
Description |
Super Key | One or more columns that can single out every row, even if they include extra attributes not strictly needed for uniqueness. |
Primary Key | The minimal set of attributes needed to identify a row without any duplicates or null values. |
Candidate Key | Any key that qualifies to become a primary key because it’s both unique and minimal. |
Alternate Key | A candidate key not used as the primary key. It still uniquely identifies rows, but it isn’t the main choice for the table. |
Foreign Key | A column in one table that matches a primary key in another table to keep their data aligned. |
Composite Key | Two or more columns combined to form a unique identifier. Helpful when no single column can do the job on its own. |
Unique Key | Demands that all values differ within a column (or set of columns), though it typically allows one null. It prevents repeated entries. |
Surrogate Key | A system-generated key with no direct real-world meaning, often auto-incremented or assigned by the database itself. |
Now that you know the main keys in SQL, let’s explore them in detail.
A super key is a collection of one or more columns that can identify every row in a table without confusion. It may include extra columns that are not strictly necessary for uniqueness, but any row that matches all the columns in the super key is guaranteed to be one of a kind.
This differs from a candidate key, which is also unique but contains no redundant attributes.
Let’s understand this with an example:
Below is a sample table that stores basic student information:
StudentID |
StudentName |
ContactNumber |
|
1 | Arun | 98765XXXXX | arun@example.com |
2 | Meera | 99887XXXXX | meera@example.com |
3 | Kirti | 98767XXXXX | kirti@example.com |
Examples of Super Key in Relational Database
Importance of Super Key
A primary key is the minimal set of columns that pinpoint a row in a table. It does not allow duplicates or empty values, which makes it the firmest boundary, preventing confusion between records. This key is the preferred choice for queries that count on accurate and immediate results.
Let’s understand this with an example:
Below is a sample table for your reference:
BookID |
Title |
Author |
Price |
101 | A Tale of Two Cities | Charles Dickens | INR 300 |
102 | Pride and Prejudice | Jane Austen | INR 280 |
103 | The Great Gatsby | F. Scott Fitzgerald | INR 350 |
Examples of a Primary Key in Relational Database
Rules for Primary Keys in DBMS
Primary Key Operations
Importance of Primary Key
Did you know? Any column already unique enough to be a primary key automatically qualifies as a super key. However, the reverse might not always be true because a super key can hold extra columns that aren’t strictly needed for identifying a row.
A candidate key is a minimal group of columns that guarantee a row’s uniqueness in a table. Unlike a super key, which can hold extra columns that aren’t required for uniqueness, a candidate key includes only what’s strictly necessary.
You may find several candidate keys in a single table, but each one meets the same standard: no duplicates and no extra fields that could be removed without losing uniqueness.
Let’s understand this with an example:
Below is a sample table that highlights these columns:
FlightID |
FlightNumber |
DepartureTime |
ArrivalTime |
GateNumber |
1 | AI-203 | 8:30 AM | 10:45 AM | B5 |
2 | AI-204 | 9:00 PM | 11:15 PM | B7 |
3 | AI-205 | 10:00 AM | 12:20 AM | C1 |
Examples of Candidate Key in Relational Database
Rules for Candidate Keys in DBMS
Candidate Key Operations
Importance of Candidate Key
An alternate key is any candidate key that did not become the primary key. It still uniquely identifies a row in a table but isn’t chosen as the main identifier. That means a table can have multiple candidate keys (like Email and PhoneNumber), but only one of them serves as the primary key. The remaining candidate keys are known as alternate keys.
Let’s understand this with an example:
Below is a sample table where PatientID is primary, and you can treat HealthCardID or PhoneNumber as alternate keys.
PatientID |
PatientName |
PhoneNumber |
HealthCardID |
1 | Rakesh | 98765XXXXX | HCI-654321 |
2 | Kavya | 99000XXXXX | HCI-987654 |
3 | Rohit | 98877XXXXX | HCI-112233 |
Examples of an Alternate Key in Relational Database
Rules for Alternate Keys in DBMS
Importance of Alternate Key
A foreign key is a column in one table that points to a primary key in another table. Its main purpose is to link two tables so that they share information in a consistent way. When you reference a foreign key, you ensure that the data in both tables stays synchronized, and entries only appear if there’s a valid match in the primary key column.
Let’s understand this with an example:
Below is a table that references a foreign key from the ‘Customers table’:
OrderID |
CustomerID |
ProductName |
OrderDate |
5001 | 101 | Wireless Mouse | 2025-01-12 |
5002 | 102 | Keyboard | 2025-01-13 |
5003 | 101 | Laptop Stand | 2025-01-14 |
Examples of a Foreign Key in Relational Database
Rules for Foreign Keys in DBMS
Foreign Key Operations
Importance of Foreign Key
You can also check out upGrad’s free tutorial, Difference Between Primary Key and Foreign Key.
A composite key is a combination of two or more columns that together make each row in a table unique. None of those columns alone can serve as the complete identifier, but merging them guarantees one-of-a-kind rows.
Composite keys often appear in many-to-many relationships, where individual columns can repeat themselves, but their pairing remains distinct.
Let’s understand this with an example:
Below is a sample table to highlight how two columns can work together:
StudentID |
CourseID |
EnrollmentDate |
101 | C001 | 2025-02-08 |
101 | C002 | 2025-02-09 |
102 | C001 | 2025-02-10 |
Examples of a Composite Key in Relational Database
Importance of Composite Key
A unique key is a constraint that ensures no two rows in a table share the same value for a specified column. It functions similarly to a primary key but can allow a null value. You can mark a column or group of columns as unique if you need each record to stand out without necessarily making it the primary identifier of your table.
Let’s understand this with an example:
Below is a table showing unique values in the Email column:
MemberID |
PhoneNumber |
|
201 | user1@example.com | 98765XXXXX |
202 | user2@example.com | 99000XXXXX |
203 | (null) | 98767XXXXX |
Examples of a Unique Key in Relational Database
Importance of Unique Key
A surrogate key is a system-generated column that identifies each row in a table but has no direct connection to real-world data. Many databases assign an automatically incrementing number as the surrogate key, and this approach offers a stable way to keep records distinct. It won't change over time and doesn't depend on user-provided values or business rules.
Let’s understand this with an example:
Below is a sample table, marking UserID as the surrogate key:
UserID |
Username |
JoinDate |
|
1 | coder123 | coder123@example.com | 2025-01-09 |
2 | devPro | devPro@example.com | 2025-01-10 |
3 | designGeek | designGeek@example.com | 2025-01-11 |
Examples of a Surrogate Key in DBMS
Importance of Surrogate Key
Want to refresh your basics regarding data structures? Join upGrad's free Certificate Course on Data Structures & Algorithms and get your revision done in a structured way!
upGrad’s Exclusive Software and Tech Webinar for you –
SAAS Business – What is So Different?
Data in a DBMS can turn disorganized if every record looks alike or repeats. You might have tried tracking one person’s details among hundreds of rows, only to discover entries that seem identical. DBMS Keys prevent that confusion and help you draw clear boundaries between records.
Here are some main reasons they matter:
Also Read: DBMS Tutorial For Beginners: Everything You Need To Know
Primary keys and unique keys both keep duplicate data out of your table. They do so in slightly different ways, especially when you consider their rules on null values and how they fit into the bigger design. Understanding these differences can make your database structure more efficient and flexible.
Take a look at the table below to see how each key type stands apart:
Aspect |
Primary Key |
Unique Key |
Definition | Identifies rows by disallowing duplicates and nulls. | Demands distinct values in a column but can allow one null (depending on the database system). |
Number Allowed | Only one per table | You can create multiple unique keys in the same table. |
Handling Nulls | Never permits null values | Typically allows one null, but disallows duplicates among non-null entries. |
Role | Serves as the main reference for relationships in other tables. | Provides an extra layer of uniqueness without being the table’s primary reference. |
Enforcement Method | Declared with the PRIMARY KEY constraint | Enforced via the UNIQUE constraint |
Indexing Behavior | Automatically indexed by most database systems for quick lookups. | Generally indexed, but implementation may vary by system. |
Impact on Table Design | Central for a table’s identity; no two rows can share the same primary key. | Improves data quality by blocking accidental repeats, while still offering some flexibility through a null field if allowed. |
To build in-depth knowledge, you can also check out upGrad's free tutorial, Primary Key vs Unique Key.
Keys in SQL relational databases go beyond simply ensuring no two rows look alike. They also help you maintain order, handle large volumes of data, and preserve vital relationships between tables. Each key must meet specific standards so that your database doesn’t run into conflicts or inconsistencies.
Here are the main properties you should keep in mind:
There is a definite advantage in defining more than one type of key. Each one focuses on a particular data issue, such as removing duplication or linking related records in separate tables. This arrangement keeps a database flexible and well-prepared to handle both routine data and complex scenarios.
Here are some additional benefits you gain by using these different key types:
upGrad offers industry-relevant courses designed in partnership with leading universities and industry experts. With flexible learning, mentorship, and hands-on projects, upGrad helps you gain practical skills and stay ahead in fields like DBMS.
You can start with the following courses:
So, if you're ready to enhance your DBMS skills, take the first step towards your dream career—book a free career counseling session today for tailored expert advice. You can also visit your nearest upGrad offline career center and start shaping your future now!
Related Blogs:
Boost your career with our popular Software Engineering courses, offering hands-on training and expert guidance to turn you into a skilled software developer.
Master in-demand Software Development skills like coding, system design, DevOps, and agile methodologies to excel in today’s competitive tech industry.
Stay informed with our widely-read Software Development articles, covering everything from coding techniques to the latest advancements in software engineering.
Get Free Consultation
By submitting, I accept the T&C and
Privacy Policy
India’s #1 Tech University
Executive PG Certification in AI-Powered Full Stack Development
77%
seats filled
Top Resources