Monday 20 February 2017

Different Keys in SQL

Types of Keys
Primary Key:

The values in the column should Uniquely define the Record/Tuple in that Table.

ID
STUDENT_NAME
CLASS
MARKS
PHONE


Consider the above Table.Here the column ID can Uniquely Define each student in the system.Hence ID is the Primary Key.

Foreign Keys:

When the values in the column of the Referencing table contains a value of the Primary Key of the Reference Table, that column is known as Foreign Key of the table.

eg.
Consider that the Reference Table is:
Supplier_ID
Name
Phone
        1
      ABC
7846
        2
      DEF
7585
        3
      GHI
7886

And Referencing Table is:
Item_id
Item_name
Supplier
      45
  pencil
     1
       76
  eraser
      1
       89
  scale
      2

Then the Supplier in the Referencing Table is the Foreign Key as it refers to the Supplier_ID in the Reference Table.

Note: Foreign Key can have repeated values as it refers to Primary Key of  reference table.


Candidate Key: It is defined as the minimal number of attributes required to identify a Record/Tuple.

eg.Consider the following table where the Primary key is Reg_no. and exam_seat_no.

Reg_no
Student_name
 class
Exam_seat_no
DOB

Now, the Reg_no , Student_name and Eaxm_seat_no can uniquely define a student.
Similarly,  Reg_no and Exam_seat_no can uniquely define a student but it's not minimal .
Only  Reg_no or Exam_seat_No can Uniquely define a student hence Reg_no  and Exam_seat_no separately are Candidate keys.

Super Keys:
In above example, Reg_no and Student_name together or Reg_no and Exam_seat_no together or all the three together are called Super key.
So, Super Key  is basically the  group of columns  which can Uniquely define a Record/Tuple ,and not necessarily be minimal.


Note:
           EVERY CANDIDATE KEY IS A SUPER KEY  BUT A SUPER KEY MAY BE / MAY NOT             BE   A  CANDIDATE KEY.






No comments:

Post a Comment