DBMS Topics
Keys in DBMS
Last Updated : 21 May, 2026
A key is an attribute or a set of attributes that is used to uniquely identify tuples rows in a relation table. Keys are fundamental to the relational model — they enforc
What is a Key?
A key is an attribute or a set of attributes that is used to uniquely identify tuples (rows) in a relation (table). Keys are fundamental to the relational model — they enforce entity integrity and enable relationships between tables.
Types of Keys
1. Super Key
A super key is any set of one or more attributes that can uniquely identify a tuple in a relation.
- Every super key can identify rows uniquely
- May contain extra (unnecessary) attributes
- A relation can have many super keys
2. Candidate Key
A candidate key is a minimal super key — a super key from which no attribute can be removed while still preserving uniqueness.
- Every attribute in a candidate key is necessary
- A table can have multiple candidate keys
- All candidate keys are potential choices for primary key
3. Primary Key
The primary key is the candidate key that is selected by the database designer to uniquely identify tuples in the table.
Rules:
- Must be unique across all rows
- Cannot contain NULL values (Entity Integrity Rule)
- Only ONE primary key per table
- Immutable — should not change over time ideally
CREATE TABLE Student (
student_id INT PRIMARY KEY, -- Simple primary key
name VARCHAR(100) NOT NULL,
email VARCHAR(100) UNIQUE
);
-- Composite Primary Key
CREATE TABLE Enrollment (
student_id INT,
course_id INT,
grade CHAR(2),
PRIMARY KEY (student_id, course_id) -- Composite PK
);4. Alternate Key
An alternate key is a candidate key that was NOT chosen as the primary key. Also called a secondary key.
| Candidate Keys | {StudentID}, {Email}, {AadhaarNo} |
| Primary Key | {StudentID} |
| Alternate Keys | {Email}, {AadhaarNo} |
5. Foreign Key
A foreign key is an attribute (or set of attributes) in one table that references the primary key of another table. It enforces referential integrity.
| 1 | Computer Science |
|---|---|
| 2 | Mathematics |
| 102 | Bob | 2 |
|---|---|---|
| 103 | Carol | 1 |
Department Table (Referenced)
Employee Table (Referencing)
OR be NULL (if the column is nullable)
Referential Actions:
ON DELETE CASCADE— Delete child rows when parent is deletedON DELETE SET NULL— Set FK to NULL when parent is deletedON DELETE RESTRICT— Prevent deletion if child rows existON UPDATE CASCADE— Update FK value when PK value changes
6. Composite Key
A composite key is a primary key consisting of two or more attributes. Neither attribute alone is sufficient to uniquely identify a tuple.
OrderItem Table
| OrderID | ProductID | Quantity | Price |
|---|---|---|---|
| 1001 | 501 | 2 | 500 |
7. Natural Key vs. Surrogate Key
| Type | Description | Example |
|---|---|---|
| Natural Key | Key derived from actual data domain | Aadhaar number, ISBN, PAN card |
| Surrogate Key | System-generated, has no business meaning | Auto-increment integer ID, UUID |
When to use surrogate keys:
- Natural key is too long (URL, email)
- Natural key can change over time
- No obvious natural key exists
- For performance (integer joins are faster than string joins)
Key Hierarchy Summary
1 chosen
rest
Quick Comparison Table
| Key Type | Uniqueness | NULL Allowed | Count per Table |
|---|---|---|---|
| Super Key | Yes | Sometimes | Many |
| Candidate Key | Yes | No | Multiple |
| Primary Key | Yes | No (enforced) | Exactly 1 |
| Alternate Key | Yes | Usually No | Multiple |
| Foreign Key | No (can repeat) | Sometimes | Multiple |
| Composite Key | Yes (combined) | No | 1 (when it's the PK) |
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Keys in DBMS.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this DBMS topic.
Search Terms
dbms, database management system, database notes, sql, unit, keys, keys in dbms
Related DBMS Topics