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.

53 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
  33. Howdy, i read your blog occasionally and i own a similar one and i was just curious if you get a lot of spam comments? If so how do you protect against it, any plugin or anything you can recommend? I get so much lately it’s driving me insane so any support is very much appreciated. Cockatiels

    ReplyDelete
  34. I?ve been exploring for a bit for any high quality articles or blog posts in this kind of house . Exploring in Yahoo I finally stumbled upon this site. Reading this info So i am happy to convey that I have a very just right uncanny feeling I came upon just what I needed. I such a lot definitely will make certain to don?t overlook this web site and provides it a glance regularly. sugaring paste

    ReplyDelete
  35. I?ve been exploring for a bit for any high quality articles or blog posts in this kind of house . Exploring in Yahoo I finally stumbled upon this site. Reading this info So i am happy to convey that I have a very just right uncanny feeling I came upon just what I needed. I such a lot definitely will make certain to don?t overlook this web site and provides it a glance regularly. sugaring paste

    ReplyDelete
  36. Good website! I really love how it is easy on my eyes it is. I’m wondering how I might be notified whenever a new post has been made. I have subscribed to your RSS feed which may do the trick? Have a great day! Cockatiels

    ReplyDelete
  37. Hey this is a good post. I’m going to mail this to my friends. I stumbled on this while exploring on yahoo I’ll be sure to come back. thanks for sharing. buy instagram video views

    ReplyDelete
  38. magnificent points altogether, you simply received emblem reader. What would you recommend about your publish that you made a few days ago? Any sure? website

    ReplyDelete
  39. Your blog has the same post as another author but i like your better.*:~;’ british shorthair

    ReplyDelete
  40. There are a few fascinating points in time on this page but I don’t know if these center to heart. There’s some validity but I am going to take hold opinion until I check into it further. Excellent article , thanks and we want much more! Included with FeedBurner likewise Locksmith

    ReplyDelete
  41. It's superior, however , check out material at the street address. CBT Mass Email Sender

    ReplyDelete
  42. 왜 저희 777을 통해서 안전한 토토사이트추천을 받아야 하나요?
    우선 저희 토토777에서는 저희와 제휴를 맺는 업체들에게 꼼꼼한 검증과 보증금을 통한 계약을 통해 이용고객님들께 불미스러운 사고가 생길시 보증금을 통한 보상제도가 마련되어 있으니, 믿고 안전하게 이용하셔도 됩니다. 토토온라인 시장에는 수 많은 총판과 저희와 같은 온라인 홍보영업을 하는 팀들이 있을겁니다. 그러나 저희는 여타 다른 총판들과는 다르게 직접 운영하는 커뮤니티와 고객님들의 안전을 위해 빠른 정보 제공과 더불어 저희가 직접 사용해보고 검증을 통해 제휴진행을 하기 때문에 더욱 안전하다고 말씀드릴 수 있습니다. 일반 이용고객님들께서 100%먹튀없는 사설토토사이트를 찾는 것은 불가능 하다고 말씀드릴 수 있습니다 토토사이트
    .

    ReplyDelete
  43. Some truly wonderful work on behalf of the owner of this internet site , perfectly great articles . Ma&MA Famille

    ReplyDelete
  44. This is one awesome blog. Really thank you! Want more.
    Regards: "satta matka"

    ReplyDelete
  45. Very helpful article and thanks for sharing.
    Regards: direct access barristers

    ReplyDelete
  46. What a nice post! I'm so happy to read this. 안전놀이터모음 What you wrote was very helpful to me. Thank you. Actually, I run a site similar to you. If you have time, could you visit my site? Please leave your comments after reading what I wrote. If you do so, I will actively reflect your opinion. I think it will be a great help to run my site. Have a good day.


    ReplyDelete
  47. Intelligence is a general cognitive ability to acquire and apply knowledge. It also refers to learning, self-awarenesss, creativity and perception. management training

    ReplyDelete
  48. It is ordinarily convoluted to plainly characterize explicitly what the best web facilitating indeed is yet there are a couple of rules it is not difficult to stay with that can assist you with discovering what the best web facilitating is intended for your necessities. https://onohosting.com/

    ReplyDelete
  49. so much wonderful information on here, : D. amcolourher

    ReplyDelete
  50. I invite you to the page where you can read with interesting information on similar topics. from this source

    ReplyDelete
  51. I think this is among the most significant info for me.
    Regards: "Blackpods"

    ReplyDelete
  52. I read that Post and got it fine and informative.
    Regards: "Pinoy Teleserye"

    ReplyDelete
  53. I'm also visiting this site regularly, this web site is really nice and the users are genuinely sharing good thoughts.
    Regards: "Blackpods Pro"

    ReplyDelete

Popular Posts