In this article Primary Key in DBMS we give the information about A Primary Key is a column or set of columns in a table that uniquely identifies each row in that table.
Primary Key in DBMS
Definition:
A Primary Key is a column or set of columns in a table that uniquely identifies each row in that table.
- It cannot contain NULL values.
- It must hold unique values.
- Every table should have only one primary key.
Why Primary Key is Important:
- Ensures data integrity by preventing duplicate rows.
- Used to define relationships between tables (via foreign keys).
- Helps in indexing for faster searching.
Example:
Table: STUDENT
RollNo (PK) | Name | Course |
101 | Rahul | BCA |
102 | Priya | BSc |
103 | Aman | BCA |
- Here, RollNo is the Primary Key because it uniquely identifies each student.
Rules for Primary Key:
- Must be unique.
- Must be NOT NULL.
- One per table only.
- Can be single column or composite key (multiple columns together).
Composite Primary Key Example:
Table: Course_Registration
StudentID | CourseID | Semester |
1 | C101 | 1 |
1 | C102 | 1 |
- Primary Key = (StudentID, CourseID)
⇒ Together they uniquely identify a row.
SQL Syntax Example:
CREATE TABLE Student (
RollNo INT PRIMARY KEY,
Name VARCHAR(50),
Course VARCHAR(20)
);
For composite primary key:
CREATE TABLE Course_Registration (
StudentID INT,
CourseID INT,
Semester INT,
PRIMARY KEY (StudentID, CourseID)
);
Candidate Key in DBMS
Definition:
A Candidate Key is a minimal set of attributes (columns) in a relation (table) that can uniquely identify a tuple (row).
- All Candidate Keys are unique and minimal.
- A table can have multiple candidate keys.
- One of the candidate keys becomes the Primary Key.
Key Points:
- Unique: No two rows can have the same value for a candidate key.
- Minimal: No attribute can be removed from the set without losing uniqueness.
- All Candidate Keys qualify to be the Primary Key.
Example:
Table: STUDENT
RollNo | AadharNumber | Name | |
101 | rahul@gmail.com | 1234-5678-9123 | Rahul |
102 | priya@gmail.com | 2345-6789-1234 | Priya |
Candidate Keys:
- RollNo
- AadharNumber
All of these uniquely identify a student, so they are Candidate Keys.
You can choose one as the Primary Key (say, RollNo), and the others remain as Alternate Keys.
Difference Between Candidate Key & Primary Key
Feature | Candidate Key | Primary Key |
Uniqueness | Yes | Yes |
NULL allowed | No | No |
Number per table | Multiple | Only one |
Purpose | Can be chosen as PK | Main unique identifier |
SQL Candidate Key Concept
While SQL does not directly define “CANDIDATE KEY” as a keyword, you enforce it manually using UNIQUE constraints.
CREATE TABLE STUDENT (
RollNo INT PRIMARY KEY,
Email VARCHAR(100) UNIQUE,
AadharNumber CHAR(12) UNIQUE,
Name VARCHAR(50)
);
In this case:
- RollNo is the Primary Key
- Email and AadharNumber are Candidate Keys with UNIQUE constraints
Super Key in DBMS
Definition:
A Super Key is any combination of attributes in a table that uniquely identifies each record (row) in that table.
Every Primary Key is a Super Key, but not every Super Key is a Primary Key.
Key Characteristics:
- It can contain extra attributes — not minimal.
- A table can have many super keys.
- Among all super keys, the minimal ones are called Candidate Keys.
Example:
Table: STUDENT
RollNo | Name | Department | |
101 | rahul@gmail.com | Rahul | BCA |
102 | priya@gmail.com | Priya | BSc |
Possible Super Keys:
- {RollNo}
- {Email}
- {RollNo, Name}
- {RollNo, Email}
- {RollNo, Name, Department}
All these combinations uniquely identify a student, so they are Super Keys.
Super Key vs. Candidate Key
Property | Super Key | Candidate Key |
Uniquely identifies | Yes | Yes |
Minimal | Not necessarily | Always minimal |
Redundant fields | May contain extra attributes | No extra attributes allowed |
Count per table | Many | Few (subset of Super Keys) |
Candidate Keys ⊂ Super Keys
Summary
- Super Key = Any unique identifier (may include unnecessary fields)
- Candidate Key = Minimal Super Key
- Primary Key = One selected Candidate Key
Visual Example
If STUDENT table has:
- RollNo (unique)
- Email (unique)
Then:
Type | Examples |
Super Keys | {RollNo}, {Email}, {RollNo, Name} |
Candidate Keys | {RollNo}, {Email} |
Primary Key | {RollNo} (chosen from candidate keys) |
Foreign Key in DBMS
Definition:
A Foreign Key is a column (or a set of columns) in one table that refers to the Primary Key in another table.
It is used to establish a relationship between two tables.
Key Characteristics:
- Ensures referential integrity.
- The value of a foreign key must match an existing value in the referenced primary key or be NULL.
- Foreign keys can have duplicate values (unlike primary keys).
Example:
Table 1: Department
DeptID (PK) | DeptName |
D01 | Computer |
D02 | Mathematics |
Table 2: Student
RollNo (PK) | Name | DeptID (FK) |
101 | Rahul | D01 |
102 | Priya | D02 |
103 | Aman | D01 |
Here:
- Student.DeptID is a Foreign Key
- It refers to Department.DeptID (the Primary Key)
This ensures that each student is assigned to a valid department only.
SQL Syntax:
CREATE TABLE Department (
DeptID VARCHAR(5) PRIMARY KEY,
DeptName VARCHAR(100)
);
CREATE TABLE Student (
RollNo INT PRIMARY KEY,
Name VARCHAR(100),
DeptID VARCHAR(5),
FOREIGN KEY (DeptID) REFERENCES Department(DeptID)
);
Foreign Key vs Primary Key
Feature | Primary Key | Foreign Key |
Uniqueness | Must be unique | Can be duplicate |
Null Allowed | Not allowed | Allowed (optional) |
Purpose | Uniquely identifies rows | Establishes relationship between tables |
In which table? | Main (parent) table | Referring (child) table |
Referential Integrity
- DBMS ensures that foreign key values must exist in the parent table.
- If you try to insert a value in the child table that doesn’t exist in the parent, it will cause an error.
What if the parent record is deleted?
Use these ON DELETE actions:
- ON DELETE CASCADE: Delete child records too
- ON DELETE SET NULL: Set foreign key to NULL in child
- ON DELETE RESTRICT: Prevent deletion of parent if children exist
Unique Key in DBMS
Definition:
A Unique Key is a constraint in a database table that ensures all values in a column (or combination of columns) are unique across the table.
Unlike a Primary Key, a Unique Key can accept a NULL value, but only one NULL (in most RDBMS like MySQL).
Key Characteristics:
Feature | Unique Key |
Uniqueness | Must be unique |
NULL allowed | Yes (one NULL allowed) |
Number per table | Multiple allowed |
Used for | Enforcing data uniqueness |
Default Index | Yes (automatically) |
Example:
Table: STUDENT
RollNo (PK) | Email (Unique) | AadharNumber (Unique) | Name |
101 | rahul@gmail.com | 1234-5678-9123 | Rahul |
102 | priya@gmail.com | 2345-6789-1234 | Priya |
Here:
- RollNo is the Primary Key
- Email and AadharNumber are Unique Keys
Both are unique across the table but are not chosen as the Primary Key.
SQL Syntax:
CREATE TABLE STUDENT (
RollNo INT PRIMARY KEY,
Name VARCHAR(50),
Email VARCHAR(100) UNIQUE,
AadharNumber CHAR(12) UNIQUE
);
You can also name a unique constraint:
sql
CopyEdit
CREATE TABLE STUDENT (
RollNo INT PRIMARY KEY,
Email VARCHAR(100),
CONSTRAINT UC_Email UNIQUE (Email)
);
Primary Key vs Unique Key
Feature | Primary Key | Unique Key |
Uniqueness | Yes | Yes |
NULL Allowed | No | Yes (1 NULL allowed) |
Number per table | Only 1 | Multiple allowed |
Used for | Main identity | Alternate unique fields |
Use Case Examples:
- Email address in user table
- Username in login table
- PAN/Aadhar in citizen database
Some More:
POP- Introduction to Programming Using ‘C’
OOP – Object Oriented Programming
DBMS – Database Management System
RDBMS – Relational Database Management System
Join Now: Data Warehousing and Data Mining