Please disable your adblock and script blockers to view this page
 

Difference between Primary Key and Foreign Key in Sql Server


Introduction: In this article I am going to share one of the mostly asked Sql interview question i.e. Primary Key vs Foreign Key. 


I have tried to list all the major differences between Primary Key and Foreign Key. Hope it will help you in preparing for your interview.

Primary Key
Foreign Key
A primary key is a column or a set of columns that can be used to uniquely identify a row in a table. It enforces the implicit NOT NULL constraint.

A foreign key is a column(s) in one table that references a unique column usually the primary key of another table. The purpose of the foreign key is to ensure referential integrity of the data. The referenced table is called the parent table while the table with the foreign key is called the child table.



Syntax to create Primary Key:
CREATE TABLE tbDepartment
 (
--define primary key
  DeptId INT IDENTITY(1,1) PRIMARY KEY
  DeptName varchar (50) NOT NULL
 )

Syntax to create foreign key:
CREATE  TABLE tbEmployee
 (
--define primary key
    EmpId INT IDENTITY(1,1) PRIMARY KEY
    FirstName VARCHAR (50) NOT NULL,
    LastName VARCHAR (50) NOT NULL,         
--define foreign key
    DeptId INT FOREIGN KEY REFERENCES tbDepartment(DeptId) 
)

Primary Key doesn’t allow null values.

Foreign key can have multiple null values.

A table can have a single primary key.
We can have multiple foreign keys in a table that can reference different tables.

Primary Key can’t be duplicate. It means same value cannot be entered in the primary key column.
Foreign key can be duplicated. It means same value can be entered in foreign key column.
By default primary key implicitly creates a clustered index on the column and data in the table is physically organized in the sequence of clustered index.

Foreign key do not automatically create an index (clustered or non-clustered) but we can explicitly create an index on foreign key column.

We can define primary key constraint on temporary table
We can’t define foreign key constraint on temporary table
We can define primary key constraint on table variable
We can't define foreign key constraint on table variable
We can insert a value in primary key column that may or may not be present in child table containing the foreign key.

We can't insert a value in foreign key column that is not present in the primary key column in the referenced parent table. 
We can't delete primary key value from the parent table which is used as a foreign key in child table. To delete we first need to delete that primary key value from the child table.
We can delete the foreign key value from the child table even though that refers to the primary key of the parent table.
 
Now over to you:
"A blog is nothing without reader's feedback and comments. So please provide your valuable feedback so that i can make this blog better and If you like my work; you can appreciate by leaving your comments, hitting Facebook like button, following on Google+, Twitter, Linked in and Pinterest, stumbling my posts on stumble upon and subscribing for receiving free updates directly to your inbox . Stay tuned and stay connected for more technical updates."
Previous
Next Post »

2 comments

Click here for comments
February 12, 2015 ×

Hello nice article. Please correct the last point.
To delete we first need to delete that primary key value from the parent table. It should be replaced by
To delete we first need to delete that primary key value from the child table.
Thanks

Reply
avatar
February 12, 2015 ×

Thanks Malik Azhar Awan for notifying the mistake..i have updated that in article..

Reply
avatar

If you have any question about any post, Feel free to ask.You can simply drop a comment below post or contact via Contact Us form. Your feedback and suggestions will be highly appreciated. Also try to leave comments from your account not from the anonymous account so that i can respond to you easily.. ConversionConversion EmoticonEmoticon