Introduction To Keys: Database Management System

Download as pdf or txt
Download as pdf or txt
You are on page 1of 19

Introduction to Keys

Database Management System


Agenda
Why Keys? Super Key
Why we need a Key? What is super key?

Candiate key , primary


key
Various Types Of Keys What is candiate key ?
Various Keys in Database What is primary key ?
Management System What is Alternate Key ?
What is foreign Key ?
Why we need a key ?
● Keys help you to identify any row of data in a table. In a real-world application, a
table could contain thousands of records. Moreover, the records could be
duplicated. Keys ensure that you can uniquely identify a table record despite these
challenges.

● Allows you to establish a relationship between and identify the relation between
tables

● Help you to enforce identity and integrity in the relationship.


Various Keys in DBMS
● Super Key
● Candidate Key
● Primary Key
● Alternate Key
● Foreign Key
● Compound Key
● Composite Key
● Surrogate Key
What is super Key ?
● A super key is a group of single or multiple keys which identifies rows in a table. A
Super key may have additional attributes that are not needed for unique
identification.
What is super Key ?
Example 1
(STUD_NO, STUD_NAME)

Example 2
(STUD_NO, STUD_NAME,STUD_PHONE)

Example 3
(STUD_NO,STUD_NAME,STUD_PHONE,STUD_STATE)
What is candiate key ?
The minimal set of attribute which can uniquely identify a tuple is known as
candidate key.
● The value of Candidate Key is unique and non-null for every tuple.

● There can be more than one candidate key in a relation. For Example,
STUD_NO is candidate key for relation STUDENT.

● The candidate key can be simple (having only one attribute) or composite
as well. For Example, {STUD_NO, COURSE_NO} is a composite candidate
key for relation STUDENT_COURSE.
What is candidate key ?
Example 1, STUD_NO in
STUDENT

Example 2
{STUD_NO, COURSE_NO}

● Adding zero or more attributes to candidate key generates super key.


● A candidate key is a super key but vice versa is not true
What is Primary Key ?
Primary Key:
There can be more than one candidate key in relation out of which one can be
chosen as the primary key.

● For Example, STUD_NO, as well as STUD_PHONE both, are candidate keys


for relation STUDENT but STUD_NO can be chosen as the primary key
(only one out of many candidate keys).
What is Alternate Key?

● Alternate Key or Secondary Key is the key that has not been selected to be
the primary key, but are candidate keys. However, it is considered a
candidate key for the primary key.
● A candidate key not selected as a primary key is called alternate or
secondary key. Candidate key is an attribute or set of attributes that you
can consider as a Primary key.
Example 1

Above, Student_ID, Student_Enroll and Student_Email are the candidate keys. They are considered
candidate keys since they can uniquely identify the student record. Select any one of the candidate key as the
primary. Rest of the two keys would be Alternate or Secondary Key.
Let’s say you selected Student_ID as primary key, therefore Student_Enroll and Student_Email will be
Alternate Key (candidates of primary key).
Example 2

Employee_ID
Employee_SSN
Employee_Phone

In the above table, the following are the three candidate keys −
Select any one of the above key as Primary Key, for example, EmployeeSSN. The rest of the two keys would
then be called Alternate Keys because they were candidates of Primary Key.
Therefore, Employee_SSN and Employee_Phone are Alternate Keys.
What we have learned
• Super key
• Candidate key
• Primary key
• Alternate key
What is foreign key ?
A FOREIGN KEY is a key used to link two tables together.
A FOREIGN KEY is a field (or collection of fields) in one table that refers
to the PRIMARY KEY in another table.
The table containing the foreign key is called the child table, and the
table containing the candidate key is called the referenced or parent
table.
Example
What is foreign key ?
• Notice that the "PersonID" column in the "Orders" table points to the
"PersonID" column in the "Persons" table.

• The "PersonID" column in the "Persons" table is the PRIMARY KEY in


the "Persons" table.

• The "PersonID" column in the "Orders" table is a FOREIGN KEY in the


"Orders" table.
Thank You !
Team DBMS

You might also like