Difference between Primary key

SQL Server "Primary Key", "Unique Key" and "Foreign Key" [Database]

The PRIMARY KEY and UNIQUE KEY both are similar and it used to apply the uniqueness of the columns and  also used for uniquely identify each row in database tables.

The major difference between Primary key, Unique key and Foreign Key as given below.

            Primary Key

1.       A primary key cannot accept null values.           
2.       A primary key must be unique.
3.       By default,  primary key create only one clustered index in the database .
4.       Each table can have one and only one primary key.
5.       Each table can have one and only one clustered index.
6.       Primary key can be made foreign key into another table.
7.       PK support auto increments.

Unique Key

1.       A unique key can accept only single null value.
2.       A unique key does not have to be the primary key.
3.       By default, unique key create non-clustered index.
4.       Each table can have more than one unique key.
5.       Unique key cannot made foreign key into another table.
6.       UK not support auto increments.

Foreign Key

1.       The foreign key is a field in the table that is primary key in another table.
2.       The foreign key do not create clustered or non clustered index automatically. You can create manually only.
3.       The foreign key can accept multiple null values.
4.       You can have more than one foreign key in a database table.
5.       There are actual advantages to having a foreign key be supported with a clustered index, but you get only one per table.

For example, you can seen in below pic for understanding about the same.



Reference links,

I hope you are enjoying with this post! Please share with you friends. Thank you!!
ANIL SINGH

Anil Singh is an author, tech blogger, and software programmer. Book writing, tech blogging is something do extra and Anil love doing it. For more detail, kindly refer to this link..

My Tech Blog - https://www.code-sample.com/
My Books - Book 1 and Book 2

www.code-sample.com/. Powered by Blogger.
^