Prepared by
Dr. Arun Kumar Singh
Dr. Arun K Singh arun.k.singh.iiit@gmail.com
Keys_Arun Sir 1
Keys
KEYS:
• 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
Keys_Arun Sir Dr. Arun K Singh arun.k.singh.iiit@gmail.com 2
Type of KEYS:
•1.Primary Key
•2. Candidate Key
•3. Alternate Key
•4. Super Key
•5. Composite Key
•6. Foreign Key
•7. Unique Key
Keys_Arun Sir Dr. Arun K Singh arun.k.singh.iiit@gmail.com 3
PRIMARY KEYS:
Keys_Arun Sir Dr. Arun K Singh arun.k.singh.iiit@gmail.com 4
Condition: Primary Key
Keys_Arun Sir Dr. Arun K Singh arun.k.singh.iiit@gmail.com 5
Candidate Key
Keys_Arun Sir Dr. Arun K Singh arun.k.singh.iiit@gmail.com 6
Alternate Key
Keys_Arun Sir Dr. Arun K Singh arun.k.singh.iiit@gmail.com 7
Super Key
Keys_Arun Sir Dr. Arun K Singh arun.k.singh.iiit@gmail.com 8
Composite Key
Keys_Arun Sir Dr. Arun K Singh arun.k.singh.iiit@gmail.com 9
Foreign Key
Keys_Arun Sir Dr. Arun K Singh arun.k.singh.iiit@gmail.com 10
Example: Students and Courses with Nullable Foreign Key
Keys_Arun Sir Dr. Arun K Singh arun.k.singh.iiit@gmail.com 11
Courses Relation
Relation Name: Courses
Primary Key: Course ID
Students Relation
Relation Name: Students
In this case, we allow the course_id to be nullable, indicating that a
student might not be enrolled in any course.
Primary Key: student_id
Foreign Key: course_id (references Courses(course_id))
A NULL value in the course_id column indicates that the student is not currently enrolled in any course. This flexibility allows the
table to accommodate students who may not have selected a course yet.
Unique Key
Keys_Arun Sir Dr. Arun K Singh arun.k.singh.iiit@gmail.com 12
Example:
Consider the following Student schema-
• Student ( roll , name , sex , age , address , class , section )
Keys_Arun Sir Dr. Arun K Singh arun.k.singh.iiit@gmail.com 13
Given below are the examples of candidate keys-
( class , section , roll )
( name , address )
These are candidate keys because each set consists of minimal attributes required to identify each student
uniquely in the Student table.
Candidate Key Example
Keys_Arun Sir Dr. Arun K Singh arun.k.singh.iiit@gmail.com 14
For example, employee relation has two attributes, Employee_Id and PAN_No, that act
as candidate keys. In this relation, Employee_Id is chosen as the primary key, so the other
candidate key, PAN_No, acts as the Alternate key.
Key Type Definition Uniqueness Null Values Purpose Index Usage Alteration
Primary Key
Uniquely
identifies each
record in a
table.
Required Not Allowed
Data integrity,
record
identification.
Creates Unique
Index
Within same
table
May be
complex,
impact other
tables.
Foreign Key
Establishes a
relationship
between tables.
Not Required Allowed
Data
consistency,
relationship
maintenance.
May create
Index
Between tables
More flexible
for
maintenance.
Candidate Key
Alternative
unique keys that
could be
primary keys.
Required Not Allowed
Backup primary
key options.
May create
Index
Within same
table
May become
primary key.
Super Key
Combination of
attributes that
uniquely
identify.
Required Allowed
Conceptual, not
enforced.
May create
Index
Conceptual use N/A
Composite Key
Combined
attributes used
as a single key.
Required Not Allowed
Specialized
unique
identification.
Creates
Composite
Index
Within same
table
May be
complex,
impact
performance.
Unique Key
Ensures
column(s) have
unique values.
Required Allowed
Enforce
uniqueness, no
primary key.
Creates Unique
Index
Within same
table
May be used as
primary key.
Keys_Arun Sir Dr. Arun K Singh arun.k.singh.iiit@gmail.com 15
Dr. Arun K Singh arun.k.singh.iiit@gmail.com
Keys_Arun Sir 16

DBMS_Concept of Keys_Primary_Candidate.pdf

  • 1.
    Prepared by Dr. ArunKumar Singh Dr. Arun K Singh arun.k.singh.iiit@gmail.com Keys_Arun Sir 1 Keys
  • 2.
    KEYS: • A Keyis 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 Keys_Arun Sir Dr. Arun K Singh arun.k.singh.iiit@gmail.com 2
  • 3.
    Type of KEYS: •1.PrimaryKey •2. Candidate Key •3. Alternate Key •4. Super Key •5. Composite Key •6. Foreign Key •7. Unique Key Keys_Arun Sir Dr. Arun K Singh arun.k.singh.iiit@gmail.com 3
  • 4.
    PRIMARY KEYS: Keys_Arun SirDr. Arun K Singh arun.k.singh.iiit@gmail.com 4
  • 5.
    Condition: Primary Key Keys_ArunSir Dr. Arun K Singh arun.k.singh.iiit@gmail.com 5
  • 6.
    Candidate Key Keys_Arun SirDr. Arun K Singh arun.k.singh.iiit@gmail.com 6
  • 7.
    Alternate Key Keys_Arun SirDr. Arun K Singh arun.k.singh.iiit@gmail.com 7
  • 8.
    Super Key Keys_Arun SirDr. Arun K Singh arun.k.singh.iiit@gmail.com 8
  • 9.
    Composite Key Keys_Arun SirDr. Arun K Singh arun.k.singh.iiit@gmail.com 9
  • 10.
    Foreign Key Keys_Arun SirDr. Arun K Singh arun.k.singh.iiit@gmail.com 10
  • 11.
    Example: Students andCourses with Nullable Foreign Key Keys_Arun Sir Dr. Arun K Singh arun.k.singh.iiit@gmail.com 11 Courses Relation Relation Name: Courses Primary Key: Course ID Students Relation Relation Name: Students In this case, we allow the course_id to be nullable, indicating that a student might not be enrolled in any course. Primary Key: student_id Foreign Key: course_id (references Courses(course_id)) A NULL value in the course_id column indicates that the student is not currently enrolled in any course. This flexibility allows the table to accommodate students who may not have selected a course yet.
  • 12.
    Unique Key Keys_Arun SirDr. Arun K Singh arun.k.singh.iiit@gmail.com 12
  • 13.
    Example: Consider the followingStudent schema- • Student ( roll , name , sex , age , address , class , section ) Keys_Arun Sir Dr. Arun K Singh arun.k.singh.iiit@gmail.com 13 Given below are the examples of candidate keys- ( class , section , roll ) ( name , address ) These are candidate keys because each set consists of minimal attributes required to identify each student uniquely in the Student table.
  • 14.
    Candidate Key Example Keys_ArunSir Dr. Arun K Singh arun.k.singh.iiit@gmail.com 14 For example, employee relation has two attributes, Employee_Id and PAN_No, that act as candidate keys. In this relation, Employee_Id is chosen as the primary key, so the other candidate key, PAN_No, acts as the Alternate key.
  • 15.
    Key Type DefinitionUniqueness Null Values Purpose Index Usage Alteration Primary Key Uniquely identifies each record in a table. Required Not Allowed Data integrity, record identification. Creates Unique Index Within same table May be complex, impact other tables. Foreign Key Establishes a relationship between tables. Not Required Allowed Data consistency, relationship maintenance. May create Index Between tables More flexible for maintenance. Candidate Key Alternative unique keys that could be primary keys. Required Not Allowed Backup primary key options. May create Index Within same table May become primary key. Super Key Combination of attributes that uniquely identify. Required Allowed Conceptual, not enforced. May create Index Conceptual use N/A Composite Key Combined attributes used as a single key. Required Not Allowed Specialized unique identification. Creates Composite Index Within same table May be complex, impact performance. Unique Key Ensures column(s) have unique values. Required Allowed Enforce uniqueness, no primary key. Creates Unique Index Within same table May be used as primary key. Keys_Arun Sir Dr. Arun K Singh arun.k.singh.iiit@gmail.com 15
  • 16.
    Dr. Arun KSingh arun.k.singh.iiit@gmail.com Keys_Arun Sir 16