Understand the Super key, primary key, Candidate key and Foreign key

Primary key:
Primary key is one of the attribute of a relation that uniquely identifies a row on a touple.
Super key:
Super key is a combination of attributes of a relation that uniquely identify a database table.
Candidate key or alternate key:
Candidate key is a subset of super key. A candidate key is a single field on the least or minimum combination of fields that uniquely identifies each record in the table.
Foreign key:
A relation schema may have an attribute that correspond to the primary key of another relation. The attribute is called foreign key.
Example:
To clearly understand the database key I discuss it with example. See the example below
Student
student_id first_name last_name course_id
A1 Kabir Rock L1
A2 Rahim Doc L2
A3 John Bon L3
A4 Bela Din L4

Consider the relation student. In that relation we uniquely identify the student using student_id, so it is primary key of this relation.
The combination of student_id, first_name and last_name is a super key but only first_name is not super key because first_name not uniquely identify the relation.
Candidate key is a minimum set of super key that uniquely identify the relation. So student_id would be a candidate key and (first_name, last_name)  set would be a candidate key. 
course
course_id course_name
L1 S1
L2 S2
L3 S3
L4 S4
Now consider another relation course. In that relation course_id is the primary key but in relation student it is  used as a foreign key.

Comments

Popular posts from this blog

How to show only month and year fields in android Date-picker?

How to construct a B+ tree with example

Conflict Serializability in database