Types Of Keys in SQL
SQL Key
A key is SQL is an attribute or a set of attribute which helps you uniquely identify a record or a row of data in a relation/ table.
Primary Key
A primary Key is a table attribute or column that identify every record present in a table uniquely.
There can be only one primary key attribute in a table and primary key column elements can’t be the same or null values.
Roll No. Name Surname
1 Alia Roy
2 Raj Kapoor
3 Alex Yadav
4 Sachin Singh
Roll No is the primary Key in the above table.
Super Key
It is the set of all columns which help to identify rows in a table uniquely.
Super Key 1 Super Key 2
Roll No. Name Mobile number Age
1 Alia 981xxxxxxx 34
2 Raj 982xxxxxxx 19
3 Alex 983xxxxxxx 20
4 Sachin 984xxxxxxx 16
Candidate Key
Candidate key is a column or a set of columns that can qualify as a primary key in the database.
There can be multiple candidate keys in a table and only one candidate key can qualify as the primary key.
Candidate Key
Roll No. Name Mobile number Age
1 Alia 981xxxxxxx 34
2 Raj 982xxxxxxx 19
3 Alex 983xxxxxxx 20
4 Sachin 984xxxxxxx 16
Composite Key
It is a set of two or more attributes that helps to identify each tuple in a table uniquely.
Any key such as super, primary or candidate key etc. can be called composite key if it has more than one attribute.
Composite Key
Roll No. Name Address Age
1 Alia Noida 34
2 Raj Delhi 19
3 Alex Agra 20
4 Sachin Patna 16
Foreign Key
Foreign key is a key used to link two tables together. This sometimes also called as referencing.
Table 1 Table 2
Service ld Service Name Service ld Charge Tax
1008 Security I008 2500 10%
1010 Accounting 1010 3000 7%
1009 Maintenance 1009 5000 12%
Foreign Key