Codechef4u is a community for computer professionals,by computer professionals,just like you; who loves sharing and helping each others,Join them
Share your post

Difference between primary key and foreign key

Difference between foreign key and primary key:

In this post I will share key differences between foreign key and primary key.

Primary Key:

The PRIMARY KEY uniquely identifies each record in a database table; main goal is to prevent duplicate values for columns and provides unique identifier to each column.

Foreign key:

Foreign key is a column or field in current table and primary key of another table, the foreign key points to another table using primary key of that table.

Differences:

Following table shows all possible differences between MS-SQL Server table primary key and foreign key.

Difference

Primary Key

Foreign Key

Limit

We can have only one Primary key in a table

We can have more than one foreign key in a table.

Null values

Primary Key can't accept null values.  

Foreign key can accept multiple null values.

Index

By default, Primary key is clustered index.

You can manually create an index (non-clustered) on foreign key.

 

Auto Increment

Primary key supports Auto Increment value.

Unique Constraint doesn't support Auto Increment value. 

Definition

The PRIMARY KEY uniquely identifies each record in a database table

Foreign key is a field in the table that is primary key in another table.

 

  धन्यवाद मित्रानोआपला प्रत्येक दिवस आनंदी आणि सुखी जावो