What are Primary Keys, Unique Keys and Foreign Keys?

Primary key plays a very important role in a table. A Primary Key is the one which uniquely identifies a row of a table. A primary key is used to create a relationship between two tables. This key does not allow duplicate values and also does not allow null values.

For example:

     UserId   FirstName      LastName
        1      John               Smith
2      Simon             Douglas
3      Bob                Franklin

It will not the values as follows:

     UserId    FirstName      LastName
        1       John               Smith
1       Simon             Douglas
2       Bob                Franklin 

Unique key is similar to Primary Key. Unique key is the one which uniquely identifies a row of a table. It will not allow duplicate values but it allows null values. So, it’s advisable to declare a primary key.

A foreign key is a field that points to the primary key of another table.

For example:

PersonalDetails_table                     RegistrationDetails_table

UserId                                         ID
FirstName                                           UserId
LastName                                           RegistrationDate

In the above relation, UserId field is common in both the tables. The field UserId is a primary key of PersonalDetails _table and a foreign key of RegistrationDetails_table.

Replies (1)

Nice example of foreign key because, this question is frequently asked during the interviews.
 

Leave a Reply

Your are not logged in . Please login to post replies

Click here to Login / Register  

Company
ARTICLESHIP 20 November 2026
Article clerk

Jain Sanjay Kumar & Co

Jaipur

CA Inter

View Details
Company
Featured 07 November 2026
Paid Assisstant

DPV & Associates

Chennai

CA Inter

View Details
Company
Featured ARTICLESHIP 31 October 2026
CA Articleship

apricus india

Navi Mumbai

CA Inter

View Details
Company
20 November 2026
Article

S & N CO

Mumbai

CA Inter

View Details
Company
28 October 2026
Assistant Director/ Associate Director-Finance & Accounting

Krea University

Chennai

CA

View Details
Company
16 November 2026
Assistant Article

ZAK & Co. Chartered Accountants

Bengaluru

CA Inter

View Details
Company
30 October 2026
Asst Manager Accounts

Goodwin Pumps India Pvt Ltd

Chengalpattu

CA Inter

View Details
Company
Featured ARTICLESHIP 17 November 2026
Associate/ Article Trainee

M/s. A V C & Co.

New Delhi

CA Inter

View Details