Difference between Primary key and Foreign key in SQL Database


In the relationaltonal database key is the most important element to maintain the relationship between two tables or to uniquely identify data from the table. Primary key is used to identify data uniquely therefore two rows can’t have the same primary key. It can’t be null.

On the other hand, foreign key is used to maintain relationship between two tables. Primary of a table act as forgein key in the other table. Foreign key in a table enforce Referential Integrity constraint. It can be more than one in the table.

Sr. No.KeyPrimary KeyForeign Key

1

Basic 

It is used to uniquely identify data in the table 

It is used to maintain relationship between tables 

2

        Null

It can’t be null 

It can accept the null values 

3

Duplicate 

Two or more rows can’t have same primary key 

It can carry duplicate value for a foreign key attribute

4

Index

Primary has clustered index 

By default, It is not clustered index 

5

Tables 

Primary key constraint can be defined on temporary tables 

It can’t be defined on temporary tables 


Updated on: 21-Jan-2020

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements