Types of Keys in Database
Management System
Presented By :- Mrs.
Surkhab Shelly
Why we have Keys in DB?
• A Key is an attribute or a set of attributes
in a relation that identifies a tuple
(record) in a relation.
• The keys are defined in a table to
access or sequence the stored data
quickly and smoothly.
• They are also used to create
relationship between different tables.
Types of Keys in Database
• 1. Primary Key
• 2. Candidate Key
• 3. Alternate Key
• 4. Super Key
• 5. Composite Key
• 6. Foreign Key
• 7. Unique Key
Primary Key
Employee
EmployeeID
EmployeeName
SSN
DeptID
DOB
• Which is Unique & Can’t be have
NULL Value
• The minimal set of attributes (or
attribute) which can uniquely identify the
rows (tuples) in a table is known as a
primary key.Key Points about Primary
Key
• Primary key is use to identify each row
uniquely in a table.
• Primary key cannot have a NULL value.
• Each table can have only one primary
key.
Candidate Key
Employee
EmployeeID
EmployeeName
SSN
DeptID
DOB
• Are individual columns in a table that
qualifies for uniqueness of each
row/tuple.
• A candidate key is an attribute or set of an
attribute which can uniquely identify a
tuple.
• Here in Employee table EmployeeID&
SSN areeligible for a
Primary Key and thus are
Candidate keys.
Dr. Kamal
Gulati
Alternate Key
Employee
EmployeeID
EmployeeName
SSN
DeptID
DOB
• Candidate column other the
Primary column,
like if EmployeeID is set for a
PK then SSN would be the
Alternate key.
Super Key
Employee
EmployeeID
EmployeeNam
e
SSN
DeptID
DOB
• If you add any other Column / Attribute to a
Primary Key then it become a Super Key,
like EmployeeID + EmployeeName is a
Super Key.
• Super key stands for superset of a key.
• The name of two employees can be the same, but
their EMPLYEE_ID can't be the same. Hence, this
combination can also be a key.
The super key would be EMPLOYEE-ID, (EMPLOYEE_ID,
EMPLOYEE-NAME), etc.
• A super key is a set of one of more columns
(attributes) to uniquely identify rows in a table.
Dr. Kamal
Gulati
Composite Key
Employee
EmployeeID
EmployeeName
SSN
DeptID
DOB
• If a table do have a single column that
qualifies for a Candidate key, then you
have to select 2 or more columns to
make a row unique.
• Like if there is no EmployeeID or
SSN columns, then you can make
EmployeeName +
DateOfBirth (DOB) as Composite
Primary Key. But still there can be a
narrow chance of duplicate rows.
Foreign Key
Employee
EmployeeID
EmployeeName
SSN
DeptID
DOB
Department
DeptID
DeptName
•A foreign key is a field in table which is generally a primary key from
another table.
•In other words, if we had a table A with a primary key X that linked to a
table B where X was a field in B, then X would be a foreign key in B.
•A foreign key is those keys which are used to define a relationship
between two or more tables.
•When we want to implement a relationship between two tables then we use
the concept of foreign key.
It is also known as referential integrity.
We can create more than one foreign key per table
Unique Key
• Unique key is same as
primary with the difference
being the existence of null.
• Unique key field allows one
value as NULL value.
Employee
EmployeeID
EmployeeName
SSN
EmailID
DOB
Practical Example
•
• Table R1. Let A,B,C,D,E are the attributes of this
relation.
A→BCDE (This means the attribute 'A' uniquely
determines the other attributes B,C,D,E.)
BC→ADE (This means the attributes 'BC'
jointly determines all the other attributes A,D,E in
the relation.)
• Find the following:
– Primary Key
– Candidate Key
– Super Key
– Composite Key
Table – R1
A
B
C
D
E
Dr. Kamal
Gulati
Answers:
•Primary Key: A
•Candidate Key: A & BC
•Super Key:A, BC, AE, AD &
ABC
•Composite Key: BC

Types of keys dbms

  • 1.
    Types of Keysin Database Management System Presented By :- Mrs. Surkhab Shelly
  • 2.
    Why we haveKeys in DB? • A Key is an attribute or a set of attributes in a relation that identifies a tuple (record) in a relation. • The keys are defined in a table to access or sequence the stored data quickly and smoothly. • They are also used to create relationship between different tables.
  • 3.
    Types of Keysin Database • 1. Primary Key • 2. Candidate Key • 3. Alternate Key • 4. Super Key • 5. Composite Key • 6. Foreign Key • 7. Unique Key
  • 4.
    Primary Key Employee EmployeeID EmployeeName SSN DeptID DOB • Whichis Unique & Can’t be have NULL Value • The minimal set of attributes (or attribute) which can uniquely identify the rows (tuples) in a table is known as a primary key.Key Points about Primary Key • Primary key is use to identify each row uniquely in a table. • Primary key cannot have a NULL value. • Each table can have only one primary key.
  • 5.
    Candidate Key Employee EmployeeID EmployeeName SSN DeptID DOB • Areindividual columns in a table that qualifies for uniqueness of each row/tuple. • A candidate key is an attribute or set of an attribute which can uniquely identify a tuple. • Here in Employee table EmployeeID& SSN areeligible for a Primary Key and thus are Candidate keys.
  • 6.
    Dr. Kamal Gulati Alternate Key Employee EmployeeID EmployeeName SSN DeptID DOB •Candidate column other the Primary column, like if EmployeeID is set for a PK then SSN would be the Alternate key.
  • 7.
    Super Key Employee EmployeeID EmployeeNam e SSN DeptID DOB • Ifyou add any other Column / Attribute to a Primary Key then it become a Super Key, like EmployeeID + EmployeeName is a Super Key. • Super key stands for superset of a key. • The name of two employees can be the same, but their EMPLYEE_ID can't be the same. Hence, this combination can also be a key. The super key would be EMPLOYEE-ID, (EMPLOYEE_ID, EMPLOYEE-NAME), etc. • A super key is a set of one of more columns (attributes) to uniquely identify rows in a table.
  • 8.
    Dr. Kamal Gulati Composite Key Employee EmployeeID EmployeeName SSN DeptID DOB •If a table do have a single column that qualifies for a Candidate key, then you have to select 2 or more columns to make a row unique. • Like if there is no EmployeeID or SSN columns, then you can make EmployeeName + DateOfBirth (DOB) as Composite Primary Key. But still there can be a narrow chance of duplicate rows.
  • 9.
    Foreign Key Employee EmployeeID EmployeeName SSN DeptID DOB Department DeptID DeptName •A foreignkey is a field in table which is generally a primary key from another table. •In other words, if we had a table A with a primary key X that linked to a table B where X was a field in B, then X would be a foreign key in B. •A foreign key is those keys which are used to define a relationship between two or more tables. •When we want to implement a relationship between two tables then we use the concept of foreign key. It is also known as referential integrity. We can create more than one foreign key per table
  • 11.
    Unique Key • Uniquekey is same as primary with the difference being the existence of null. • Unique key field allows one value as NULL value. Employee EmployeeID EmployeeName SSN EmailID DOB
  • 12.
    Practical Example • • TableR1. Let A,B,C,D,E are the attributes of this relation. A→BCDE (This means the attribute 'A' uniquely determines the other attributes B,C,D,E.) BC→ADE (This means the attributes 'BC' jointly determines all the other attributes A,D,E in the relation.) • Find the following: – Primary Key – Candidate Key – Super Key – Composite Key Table – R1 A B C D E
  • 13.
    Dr. Kamal Gulati Answers: •Primary Key:A •Candidate Key: A & BC •Super Key:A, BC, AE, AD & ABC •Composite Key: BC