0% found this document useful (0 votes)
14 views4 pages

Keys

Uploaded by

moazmizan666
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views4 pages

Keys

Uploaded by

moazmizan666
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

In a Database Management System (DBMS), keys play an essential role in organizing and

uniquely identifying records in a table. Let's explore the different types of keys with examples to
clarify their roles.

1. Primary Key

 Definition: A Primary Key is a column (or set of columns) that uniquely identifies each
record in a table. It cannot contain NULL values, and its values must be unique.
 Example: Consider a Students table:

Students
+-----------+------------+--------+
| StudentID | Name | Email |
+-----------+------------+--------+
| 101 | Alice | [email protected] |
| 102 | Bob | [email protected] |
| 103 | Charlie | [email protected] |
+-----------+------------+--------+

 Here, StudentID is the Primary Key because it uniquely identifies each student
in the table.
 No two students can have the same StudentID, and it cannot be NULL.

2. Candidate Key

 Definition: A Candidate Key is any column or set of columns that can uniquely identify
a record. A table can have multiple candidate keys.
 Example: In the same Students table:

+-----------+------------+------------+
| StudentID | Name | Email |
+-----------+------------+------------+
| 101 | Alice | [email protected] |
| 102 | Bob | [email protected] |
| 103 | Charlie | [email protected] |
+-----------+------------+------------+

 Both StudentID and Email can be Candidate Keys because they can each
uniquely identify a student.
 While only one can be chosen as the Primary Key (e.g., StudentID), the other
(e.g., Email) can still be a candidate key.

3. Super Key

 Definition: A Super Key is any combination of columns that can uniquely identify a row
in the table. It can consist of a single column or multiple columns.
 Example: In the same Students table:
+-----------+------------+------------+
| StudentID | Name | Email |
+-----------+------------+------------+
| 101 | Alice | [email protected] |
| 102 | Bob | [email protected] |
| 103 | Charlie | [email protected] |
+-----------+------------+------------+

 StudentID is a Super Key because it can uniquely identify each row.


 A combination of StudentID and Name (StudentID, Name) is also a Super Key,
but it contains redundant data since StudentID alone can uniquely identify rows.

4. Alternate Key

 Definition: An Alternate Key is any candidate key that is not chosen as the primary key.
It’s another potential unique identifier.
 Example: If we have StudentID as the Primary Key, Email becomes an Alternate Key
since it can also uniquely identify a student.

+-----------+------------+------------+
| StudentID | Name | Email |
+-----------+------------+------------+
| 101 | Alice | [email protected] |
| 102 | Bob | [email protected] |
| 103 | Charlie | [email protected] |
+-----------+------------+------------+

5. Foreign Key

 Definition: A Foreign Key is a column (or set of columns) in one table that refers to the
Primary Key in another table. It establishes a relationship between two tables.
 Example: Suppose we have two tables: Students and Courses. The Students table has
StudentID as the Primary Key, and the Enrollments table has StudentID as a Foreign
Key.
Students Table:
+-----------+------------+
| StudentID | Name |
+-----------+------------+
| 101 | Alice |
| 102 | Bob |
+-----------+------------+

Enrollments Table:
+-----------+------------+------------+
| StudentID | CourseID | Grade |
+-----------+------------+------------+
| 101 | CSE101 | A |
| 102 | MAT102 | B |
+-----------+------------+------------+

 In this example, StudentID in the Enrollments table is the Foreign Key that
references the StudentID in the Students table.

6. Composite Key

 Definition: A Composite Key is a key that consists of two or more columns that together
uniquely identify a record.
 Example: In an Enrollments table, the combination of StudentID and CourseID can be
a Composite Key because the same StudentID may enroll in multiple courses, but
together with CourseID, it will uniquely identify each enrollment record.

Enrollments Table:
+-----------+------------+------------+
| StudentID | CourseID | Grade |
+-----------+------------+------------+
| 101 | CSE101 | A |
| 101 | MAT102 | B |
| 102 | CSE101 | B+ |
+-----------+------------+------------+

7. Unique Key

 Definition: A Unique Key is a column (or set of columns) that ensures all values in the
column are unique. It can accept a single NULL value, unlike a primary key.
 Example: In a Users table:

Users Table:
+-----------+------------+------------+
| UserID | Username | Email |
+-----------+------------+------------+
| 1 | user1 | [email protected] |
| 2 | user2 | [email protected] |
+-----------+------------+------------+
 The Email column can be a Unique Key, ensuring that no two users have the
same email address.

You might also like