0% found this document useful (0 votes)
6 views

Lec 05, Database Keys

The document explains various types of keys in relational databases, including Primary Key, Candidate Key, Super Key, Foreign Key, Alternate Key, and Composite Key. Each key serves a specific purpose in uniquely identifying records and establishing relationships between tables. Real-life analogies are provided to illustrate the concepts effectively.

Uploaded by

msrteam007
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Lec 05, Database Keys

The document explains various types of keys in relational databases, including Primary Key, Candidate Key, Super Key, Foreign Key, Alternate Key, and Composite Key. Each key serves a specific purpose in uniquely identifying records and establishing relationships between tables. Real-life analogies are provided to illustrate the concepts effectively.

Uploaded by

msrteam007
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

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.

Roll No Name City Age

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.

StudentID (PK) Name Email Phone

101 Anna [email protected] 12345

102 Jake [email protected] 54321

StudentID is the Primary Key — it’s unique for each student.


There can’t be two students with the same ID, and the ID can’t be empty.

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.

StudentID (PK) Name Email Phone

101 Anna [email protected] 12345

102 Jake [email protected] 54321

Candidate Keys: StudentID, Email, Phone


You can choose any of these as the Primary Key, but typically, we choose StudentID.
3. Super Key
A Super Key is any set of attributes that can uniquely identify a row. It can include extra,
unnecessary attributes.

StudentID (PK) Email Phone Dept

101 [email protected] 12345 CSE

102 [email protected] 54321 BBA

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.

StudentID (PK) Email (Alt Key) Phone

101 [email protected] 12345

102 [email protected] 54321

Candidate Keys: StudentID, Email, Phone


Primary Key: StudentID
Alternate Key: Email, Phone (because they are unique but 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:

Imagine you are in a university:

●​ Primary Key: Your Student ID — unique and assigned only to you.


●​ Candidate Keys: Your Student ID, Email, and Phone Number — all unique.
●​ Super Key: Your Student ID + Name — still unique, but extra information.
●​ Foreign Key: Your DeptID — links you to a department.
●​ Alternate Key: Your Email — a unique identifier, but not the main key.
●​ Composite Key: Your Student ID + Course ID — uniquely identifies a course
registration.

You might also like