Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
MySQL + SQL · Lesson 134

Types of Keys in DBMS

In this lesson: Primary, foreign, candidate, super, alternate, composite and unique keys in DBMS — one table example and chart. In Hindi & English.

Why keys matter

Keys are columns used to uniquely identify rows or to link tables together. They are what keep a relational database correct and connected. There are several types, and they build on each other — from the broad super key down to the specific primary key.

💡 Keys do two jobs: uniquely identify each row, and connect one table to another.

Super key

A super key is any column or set of columns that can uniquely identify a row — even if it has extra, unneeded columns. For a students table, {roll}, {roll, name} and {roll, name, marks} are all super keys, because each uniquely identifies a row.

Candidate key

A candidate key is a minimal super key — it has no extra columns. From the super keys above, only {roll} is a candidate key (assuming roll is unique), because removing any column would break uniqueness. A table can have several candidate keys.

Primary key

The primary key is the one candidate key you choose as the official identifier. It cannot be null and cannot repeat, and there is only one per table.

CREATE TABLE students (
    roll INT PRIMARY KEY,
    name VARCHAR(50)
);

Here roll is the primary key — every student must have a unique, non-null roll number.

Unique key

A unique key also enforces uniqueness, but it can allow one null, and a table can have several. Use it for columns that must be unique but are not the main identifier, like an email.

CREATE TABLE users (
    id INT PRIMARY KEY,
    email VARCHAR(100) UNIQUE
);

Foreign key

A foreign key links tables. It is a column that points to the primary key of another table.

CREATE TABLE orders (
    id INT PRIMARY KEY,
    customer_id INT,
    FOREIGN KEY (customer_id) REFERENCES customers(id)
);

Here orders.customer_id refers to customers.id, connecting each order to a customer and preventing orders for non-existent customers.

Composite key

A composite key uses two or more columns together as the key, when no single column is unique alone.

CREATE TABLE enrollments (
    student_id INT,
    course_id INT,
    PRIMARY KEY (student_id, course_id)
);

Neither student_id nor course_id is unique alone, but together they uniquely identify one enrolment.

Summary

KeyMeaningNull?How many
SuperUniquely identifies (may have extras)—Many
CandidateMinimal super key—Several
PrimaryChosen main identifierNo nullOne
UniqueEnforces uniquenessOne null okSeveral
ForeignLinks to another tableCan be nullSeveral
CompositeKey of multiple columns——

Summary

  • Keys identify rows uniquely and link tables.
  • Super key: identifies a row, possibly with extra columns; candidate: minimal super key.
  • Primary key: the chosen identifier, never null, one per table.
  • Unique key: enforces uniqueness, allows one null, several allowed.
  • Foreign key links tables; composite key uses multiple columns.

Next, compare the two most important in primary key vs foreign key, or learn the table model in RDBMS concepts.

Frequently Asked Questions

What is a key in DBMS?
A key is a column (or set of columns) used to uniquely identify rows in a table or to link tables together. Keys keep data correct and connected. The main types are super, candidate, primary, unique, foreign and composite keys.
What is the difference between a primary key and a candidate key?
A candidate key is any column that can uniquely identify a row. A table may have several candidate keys, but you choose one of them to be the primary key — the official main identifier. The others remain alternate keys.
What is the difference between a primary key and a unique key?
Both enforce uniqueness. A primary key cannot be null and there is only one per table. A unique key can allow one null value, and a table can have several unique keys.
What is a foreign key?
A foreign key is a column in one table that refers to the primary key of another table, linking them. For example, orders.customer_id is a foreign key pointing to customers.id, connecting orders to customers.
What is a composite key?
A composite key is a key made of two or more columns together, used when no single column is unique on its own. For example, (student_id, course_id) can together uniquely identify an enrolment.
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.
Next topicPrimary Key vs Foreign Key Your progress: 134 / 151 (89%) · Lesson 135 / 151 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free