banner_ad

What are Primary Keys, Unique Keys and Foreign Keys?

Others 674 views 1 replies

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.
 


CCI Pro

Leave a Reply

Your are not logged in . Please login to post replies

Click here to Login / Register  

Company
ARTICLESHIP 31 May 2026
Article Assistant

KPRS And Associates

New Delhi

CA Inter

View Details
Company
Featured 26 May 2026
Account Executive

SMJ global advisors pvt ltd

New Delhi

B.Com

View Details
Company
22 May 2026
U.S. Financial Reporting & Consolidation Manager

Karia Overseas

Ahmedabad

CA

View Details
Company
07 June 2026
Tax Associate

Rajkumar Jain & Co.

Ahmedabad

Graduate (Any)

View Details
Company
24 May 2026
Accounts & Tax Executive

PARAS KHURANA AND CO

New Delhi

B.Com

View Details
Company
09 June 2026
Accounts Associate

S Madan and CO

New Delhi

Graduate (Any)

View Details
Company
14 May 2026
ICSI Trainees for 21 Months and Semi-Qualified CS

CMNITY HIRE

New Delhi

Others

View Details
Company
ARTICLESHIP 04 June 2026
Article

Rakhecha & Co.

New Delhi

CA Inter

View Details