Click here to Skip to main content
15,890,336 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
hi...
i am facing the interview he asking me a one question in sql server
what is the difference between primary key and foreign key in sql serevr

then didnt answer it..
i can not be wants to comit this mistake in my next interview so give me the proper answer
Posted

See here[^], 305,000 results waiting to answer you.

..and in short:
Primary Key:
It will not allow "Null values" and "Duplicate values"

Foreign Key:
It will allow "Null values" and "Duplicte values" and it refers to a primary key in anoter table.
 
Share this answer
 
Comments
Vijay Walunj,Navi mumbai 17-Sep-12 8:02am    
You can only have one primary key, but you can have multiple columns in your primary key.
1.There is only one primary key allowed per table but you may have more than one foreign key per table.
2.Primary key will not allowed "null values" and "Duplicate values" while in foreign key allowed
3.You can't insert foreign key column value into table if primary key value not available.but reverse is possible.
4.You can't delete primary key value if foreign key reference is set into table.but reverse is possible.
 
Share this answer
 
Comments
Vijay Walunj,Navi mumbai 17-Sep-12 7:59am    
You can only have one primary key, but you can have multiple columns in your primary key.
here is simple answer..

Primary Key:
-1 Primary key is used to avoid the duplication of records under same column
-2 Primary key is used to uniquely identify the each record from table
-3 You can use only one primary key at a time on single table
-4 It does not allow any null value or duplicate value


Foreign Key


-1 it is used to give the reference of another table primary key..when i update the child table record then it does not allow me..first i need to update from parent table then it is updated in child table

this is the simple difference that interviewer expected from candidate


thanks
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900