Powered by Blogger.

Saturday, February 15, 2014

Difference between primary key and foreign key in SQL server

In this post we will discuss about what is difference between  primary key and foreign key in SQL server 2008.

You can see my previous posts on:

- Advantages of WCF in Asp.Net

- Read and write from Text file in Asp.Net

- Difference between Dataset and Datareader asp.net

Though primary key and foreign key looks similar but there are some difference between them.

Primary Key:
- Primary key uniquely identify a record in the table.

- Primary Key can't accept null values.

- By default, Primary key is clustered index and data in the database table is physically organized
in the sequence of clustered index.

- We can have only one Primary key in a table.

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

- Foreign key can accept multiple null value.

- Foreign key do not automatically create an index, clustered on non-clustered. You can manually create an index on foreign key.

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