Lec 05, Database Keys
Lec 05, Database Keys
A key is an attribute or set of attributes that uniquely identifies any record from the table.
Keys play an important role in the relational database. It is used to uniquely identify any
record or row of data from the table. It is also used to establish and identify relationships
between tables.
1 Alice Berlin 25
2 Jake Toronto 28
3 Alice Berlin 25
1. Primary key
A Primary Key is a column (or a combination of columns) that uniquely identifies each
row in a table. It cannot have duplicate or NULL values. PK will never be changed, no
update is possible.
2. Candidate key
A Candidate Key is a column (or set of columns) that could be a Primary Key because it is
unique and not null. A table can have multiple candidate keys, but only one is chosen as
the Primary Key.
Super Keys:
● {StudentID}
● {StudentID, Email}
● {StudentID, Phone, Department}
A Candidate Key is the smallest Super Key — the one with no unnecessary attributes.
4. Foreign key
A Foreign Key is a column that links two tables. It references the Primary Key of another
table, creating a relationship between them.
Department Table:
DeptID DeptName
1 CSE
2 BBA
Student Table:
StudentID (PK) Name DeptID
101 anna 1
102 jake 2
DeptID in the Student Table is a Foreign Key that references DeptID in the Department
Table.
It links students to their respective departments.
5. Alternate key
An Alternate Key is a Candidate Key that was not chosen as the Primary Key.
6. Composite key
A Composite Key is a combination of two or more columns that uniquely identifies each
row.
Course Enrollment Table:
StudentID (PK) CourseID (PK) Grade
101 CSE101 A
101 CSE102 B
102 CSE103 A
Primary Key: (StudentID, CourseID) → Together, these two columns uniquely identify
each record.
StudentID alone or CourseID alone is not unique, but together, they form a Composite
Key.
Real-Life Analogy: