Database
Keys
By: Khalid Asad
Associate Professor
GSSC, Peshawar
Database Keys
• In a relational database, keys play an important role in
ensuring data integrity, maintaining relationships between
tables, and enforcing uniqueness.
• Understanding different types of database keys is crucial for
efficient database design, normalization, and query
performance.
2
Database Keys
• Database keys can be categorized into different types
based on their functionality.
3
Primary Key
• A Primary Key is a column (or a combination of
columns) that uniquely identifies each row in a table.
4
Primary Key
• In a relational database, the Students and Courses
tables typically have Primary Keys (PKs) to uniquely
identify each record. Below is a structured
representation of both tables with their Primary Keys:
5
Primary Key
6
Primary Key
7
Primary Key
8
Primary Key
9
Primary Key
10
Candidate Key
• A Candidate Key is a set of attributes that can uniquely
identify a row.
• One of the Candidate Keys is chosen as the Primary Key,
while others are Alternate Keys.
11
Candidate Key
12
Candidate Key
13
Alternate Key
• An Alternate Key is a Candidate Key that was not
selected as the Primary Key.
14
Alternate Key
15
Super Key
• A Super Key is a set of one or more attributes that can
uniquely identify a row.
• Super Keys include all Candidate Keys and Primary Keys
but may have extra attributes.
16
Super Key
17
Super Key
18
Foreign Key (FK)
• A Foreign Key is an attribute in one table that refers to
the Primary Key of another table, establishing a
relationship.
19
Foreign Key (FK)
20
Foreign Key (FK)
21
Composite Key
• A Composite Key is a Primary Key made up of two or
more columns.
22
Composite Key
23
Unique Key
• A Unique Key ensures that a column's values remain
unique, but unlike the Primary Key, it can contain NULL
values.
24
Unique Key
25
Secondary Key
• A Secondary Key is an attribute (or a set of attributes)
that is not a Primary Key but can be used for data
retrieval because it holds unique or important
information.
26
Secondary Key
27
Secondary Key
28
Diff. b/w Primary & Secondary Key
29
Secondary Key
30
Surrogate Key
• A Surrogate Key is an artificial key, usually an auto-
incremented number, used as a Primary Key when no
natural key exists.
31
Surrogate Key
32
Surrogate Key
33
Summary Table of
Database Keys
34