Sunday, May 10, 2020

Primary key Vs Foreign Key in SQL

As we know that relationship between two or more tables in SQL Server is the basic concept of any relational database. For an example. a family always starts with Parent and Children relationship, same as is a database always starts with product-item or customers-regions relationship. 
So, we will try to understand that how can we differentiate a relationship between two or more data tables in a database. You can suppose that Parent equals to Primary key and Children equal to Foreign key in a database.


In the above diagram, you can see the Primary Key and Foreign Key relationship between Students, Enrollments and Classes data tables. 
In Students data table, Student ID is the primary key and it is establishing a relationship with Enrollments tables and acts as a Foreign key. 

In Classes data table, Class ID is the primary key and it is establishing a relationship with Enrollments tables and acts as a Foreign key.

Primary Key - In a database, a table can have only one primary key which cannot have a NULL value. It always represents a clustered index in a database table and helps to organize the sequence of clustered index. 
Primary key can be related to another tables as a Foreign Key and you can apply Auto Increment value for a Primary key but auto increment is not mandatory.  
We can define Primary key constraint on temporary table and table variable.
Note: you can't delete primary key value from the parent table which is used as a foreign key in child table. To delete the primary key in the main table, you have to delete that primary key value from the child tables.

Foreign Key - A foreign key is just a referential constraint between two or more tables. If a Primary key is used in the another table then it would be know in the another table as Foreign key which can accept multiple null value. Foreign key always generates after Primary key. Thus they do not automatically increments. It supports  clustered or non-clustered indexes and you can have more than one foreign key in a table.
If you want to create some indexes on Foreign key then you must manually create an index on foreign keys. 
Please keep in your mind, you can’t create foreign key constraint on temporary table or table variable as compared to Primary key constraint.
You can delete the foreign key value from the child table even though that refers to the primary key of the parent table.

Foreign keys are almost always "Allow Duplicates," which would make them unsuitable as Primary Keys.
It is perfectly fine to use a foreign key as the primary key if the table is connected by a one-to-one relationship, not a one-to-many relationship.

32 comments:

  1. Normally I don’t read article on blogs, but I would like to say that this write-up very forced me to try and do it! Your writing style has been surprised me. Thanks, quite nice article. 먹튀

    ReplyDelete
  2. There are very lots of details that adheres to that to think about. That’s a fantastic specify retrieve. I provide you with the thoughts above as general inspiration but clearly you will find questions such as the one you raise up where biggest factor are going to be getting work done in honest great faith. I don?t know if guidelines have emerged around such things as that, but More than likely that the job is clearly defined as an affordable game. Both youngsters feel the impact of merely a moment’s pleasure, throughout their lives. 토토먹튀

    ReplyDelete
  3. Hello there! I know this is somewhat off topic but I was wondering which blog platform are you using for this site? I’m getting tired of WordPress because I’ve had problems with hackers and I’m looking at options for another platform. I would be fantastic if you could point me in the direction of a good platform. 먹튀검증

    ReplyDelete
  4. Hello there! I know this is somewhat off topic but I was wondering which blog platform are you using for this site? I’m getting tired of WordPress because I’ve had problems with hackers and I’m looking at options for another platform. I would be fantastic if you could point me in the direction of a good platform. Mega888 game client download

    ReplyDelete
  5. marketing jobs can really earn you lots of dollars but most of the time, it is a difficult job* 먹튀검증

    ReplyDelete
  6. Nice post. I discover some thing harder on diverse blogs everyday. Most commonly it is stimulating to study content off their writers and use a specific thing from their store. I’d want to apply certain while using the content in my small blog no matter whether you don’t mind. Natually I’ll give you a link in your web blog. Appreciate your sharing. 토토사이트

    ReplyDelete
  7. I have recently started a blog, and the info you offer on this website has helped me greatly. Thanx for all of your time & work. 파워볼사이트

    ReplyDelete
  8. Oh my goodness! an incredible article dude. Thank you Nonetheless I’m experiencing challenge with ur rss . Don know why Unable to subscribe to it. Is there anyone getting similar rss drawback? Anybody who knows kindly respond. Thnkx 918kiss

    ReplyDelete
  9. Hi, have you ever before asked yourself to write about Nintendo or PSP? 먹튀검증

    ReplyDelete
  10. I used to be recommended this website through my cousin. I am now not certain whether this put up is written by him as nobody else recognize such special about my difficulty. You are incredible! Thank you! 먹튀검증

    ReplyDelete
  11. I used to be recommended this website through my cousin. I am now not certain whether this put up is written by him as nobody else recognize such special about my difficulty. You are incredible! Thank you! 먹튀검증

    ReplyDelete
  12. This can be just the simplest and best advice I have ever come across about this topic. Thank you for this very useful blog post of yours. Additionally, I enjoy writing articles which has a personal tone incorporated. I feel it makes your reader feel more important and inclined to believe me more. Plus it sounds more real and never coming from a robot. I enjoy making my visitors feel important and special. I want to let them have the best tips on how to cope with important issues such as this. DreamHost Coupon

    ReplyDelete
  13. I am so happy to read this. This is the kind of manual that needs to be given and not the random misinformation that’s at the other blogs. Appreciate your sharing this greatest doc. la salsa

    ReplyDelete
  14. This can be just the simplest and best advice I have ever come across about this topic. Thank you for this very useful blog post of yours. Additionally, I enjoy writing articles which has a personal tone incorporated. I feel it makes your reader feel more important and inclined to believe me more. Plus it sounds more real and never coming from a robot. I enjoy making my visitors feel important and special. I want to let them have the best tips on how to cope with important issues such as this. superslot เครดิตฟรี50

    ReplyDelete
  15. This is so good as warzyd. You should to check it out. He is amazing and can show what things like nobody else. Give him a chance a check. But except i really like your blog. Good job! App for kiss918 and 918kiss

    ReplyDelete
  16. I tried to submit a comment earlier, although it has not shown up. I think your spam filter may possibly be broken? 24h schlüsseldienst

    ReplyDelete
  17. Thanks for sharing superb informations. Your web site is so cool. I’m impressed by the details that you have on this website. It reveals how nicely you understand this subject. Bookmarked this website page, will come back for extra articles. You, my pal, ROCK! I found simply the information I already searched all over the place and just couldn’t come across. What a great web site. http://healthexpertstips.com/

    ReplyDelete
  18. I am curious to find out what blog system you are utilizing? I’m having some small security issues with my latest blog and I would like to find something more secure. Do you have any recommendations? greengeeks wordpress hosting review

    ReplyDelete
  19. You produced some decent points there. I looked on the net to the issue and discovered most people goes together with along with your website. HostGator Promo Code

    ReplyDelete
  20. Gaming desktops are not as problematic as you might assume, and earning your personal gaming personal pc is not as tough as a lot of folks would make you imagine. Given that you by now have a precise software in thoughts when setting up your personal gaming device, there are truly only three most important elements you need to have to be concerned about, and almost everything else is genuinely secondary: the processor, movie card, and RAM. 영주출장

    ReplyDelete
  21. Have you already setup a fan page on Facebook ?~-.:. 메이저사이트

    ReplyDelete
  22. When I originally commented I clicked the -Notify me when new surveys are added- checkbox and already when a comment is added I get four emails with the exact same comment. Will there be in any manner you can remove me from that service? Thanks! 영주출장

    ReplyDelete
  23. Needed to draft you the bit of note to be able to thank you again just for the breathtaking pointers you’ve shared at this time. It is certainly incredibly open-handed with people like you to supply easily precisely what a lot of folks would’ve offered as an e book to help make some bucks on their own, notably now that you could have done it in case you decided. The tricks additionally served to become good way to recognize that some people have a similar interest the same as my own to learn many more concerning this problem. I am sure there are a lot more enjoyable periods ahead for many who scan through your blog. 토토사이트

    ReplyDelete
  24. Really interesting and one of a kind post. I like such things as making more research, developing writing skills, as well as related things. These kinds of secrets help in being a specialist on this topic. This post is very helpful to my family because people like you devoted time to learning. Regularity is the key. But it is not that easy, as has been made out to be. I am not an expert like you and lots of times I feel like giving it up. Looking at a person like me, your posting is of good assistance and booster. Thank you for sharing. macaws

    ReplyDelete
  25. Aw, this was a really nice post. In thought I would like to put in writing like this additionally ?taking time and precise effort to make an excellent article?however what can I say?I procrastinate alot and on no account appear to get something done. free backlink

    ReplyDelete
  26. You created some decent points there. I looked online for your issue and located most people go along with together with your web site. Daniel Gordon GLD Partners

    ReplyDelete
  27. I am glad to be one of the visitors on this great site (:, appreciate it for posting. gaming pc deal

    ReplyDelete
  28. It is perfect time to make some plans for the future and it is time to be happy. 토토사이트 I've read this post and if I could I desire to suggest you some interesting things or suggestions. Perhaps you could write next articles referring to this article. I want to read more things about it!

    ReplyDelete
  29. I simply found your internet website many days ago and i have been reading through it frequently. you have got a good range of useful data on the site and bitstarz review

    ReplyDelete
  30. About 918kiss| SCR888 | KISS918. If in the past, shooting ... Earn a variety of details from this 918 Kiss Fish Shooting Game.

    ReplyDelete
  31. This web site is my intake, very good style and Perfect articles. تطبيق توصيل

    ReplyDelete
  32. I was surfing net and fortunately came across this site and found very interesting stuff here. Its really fun to read. I enjoyed a lot. 토토사이트 Thanks for sharing this wonderful information.

    ReplyDelete

Popular Posts