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.

70 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. 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
  29. About 918kiss| SCR888 | KISS918. If in the past, shooting ... Earn a variety of details from this 918 Kiss Fish Shooting Game.

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

    ReplyDelete
  31. 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
  32. 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
  33. 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
  34. 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
  35. 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
  36. 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
  37. Your blog has the same post as another author but i like your better.*:~;’ british shorthair

    ReplyDelete
  38. 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
  39. It's superior, however , check out material at the street address. CBT Mass Email Sender

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

    ReplyDelete
  41. 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
  42. I invite you to the page where you can read with interesting information on similar topics. from this source

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

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

    ReplyDelete
  45. This comment has been removed by the author.

    ReplyDelete
  46. Start with a chief outline, which is a significant level portrayal of what the business will do.
    brainbi

    ReplyDelete
  47. It’s very informative and you are obviously very knowledgeable in this area. You have opened my eyes to varying views on this topic with interesting and solid content. business insider

    ReplyDelete
  48. Succeed! It could be one of the most useful blogs we have ever come across on the subject. Excellent info! I’m also an expert in this topic so I can understand your effort very well. Thanks for the huge help. brand name ideas

    ReplyDelete
  49. TopWin-Movie-Maker.com ofrece las descargas de la última versión y la clásica de Windows Movie Maker. Han sido probados compatibles con estos sistemas operativos: Windows XP, Vista, 7, 8, 10. Todos los softwares disponibles para descargas han sido escaneado por los softwares de seguridad más utilizados como Avast! y AVG. Consejo: No tenemos ninguna relación con Microsoft. También puede descargar el software desde Microsoft, pero podrían faltar los efectos personalizados de terceros, animaciones, música, etc. creador de películas de Windows

    ReplyDelete
  50. This blog was extremely helpful. I really appreciate your kindness in sharing this with me and everyone else! https://givetime.io/

    ReplyDelete
  51. "I was impressed by your writing. Your writing is impressive. I want to write like you.우리카지노 I hope you can read my post and let me know what to modify. My writing is in I would like you to visit my blog.

    ReplyDelete
  52. It has a good meaning. If you always live positively, someday good things will happen. 메이저사이트Let's believe in the power of positivity. Have a nice day.


    ReplyDelete
  53. Yes, I am entirely agreed with this article, and I just want say that this article is very helpful and enlightening. I also have some precious piece of concerned info !!!!!!Thanks. Click here

    ReplyDelete
  54. Oftentimes, startup and small business owners are scared of taking loans because they believe returning the same loan with interest on it will hinder their growth. click for more

    ReplyDelete
  55. Game judi slot online ini merupakan bagian yang tidak bisa dipisahkan dengan berbagai macam jenis taruhan online yang tergolong cukup mudah buat bisa dimainkan lewat apkikasi slot online. Pasar taruhan judi slot online itu sendiri dengan faktornya keberuntungan cukup mudah memperoleh kemenangan Anda sendiri dengan taruhan secara online, YAP! Seperti halnya kita ketahui sebelumnya yang tidak perlu membuat para pemainnya harus keluar rumah. Melainkan dengan kehadiran judi slot online dengan software Android dan IOS di mobile ini bisa membuat Anda lebih mudah lagi dalam memainkannya. Anda bisa didownload di playstore dan appstore terlebih dahulu kemudian baru bisa melakukan pertaruhan dimana saja sesuka hati. Oleh karena itu, unduh langsung aplikasi slot online dari perangkat ponsel Anda. Pilihlah bandar judi slot online 389Sports yang sudah terpercaya sebagai solusi bermain judi slot online. 389Sports selalu memberikan kualitas permainan terbaik untuk Anda dengan dukungan Customer Service (CS) 24 jam dan sistem tercanggih memudahkan Anda dalam mencapai kemenangan bermain judi slot online. situs slot online

    ReplyDelete
  56. I have recently started a blog, the info you provide on this site has helped me greatly. Thanks for all of your time & work. Power Plant UAE

    ReplyDelete
  57. purchase views for youtube YouTube is a fantastic tool for internet marketing - if used correctly. Consumers are watching over 2 billion videos per day, and this figure is increasing at an exponential rate. For an Internet Marketer, where better can you get your message across? A huge audience coming to you, looking for information and products to buy. And the beauty of YouTube is it is completely free advertising! But for you to be able to sell products from your YouTube video, it firstly must be seen. To do this, you must aim on getting your product on page 1 of Google.

    ReplyDelete
  58. https://twitchviral.com/ A whole subculture has developed fans who keep track of sites providing cool, funny games online. Providers of flash games as entertainment have taken notice. These sites gained fantastic worldwide presence that has sought to deliver a humongous platform for arcade and related gaming to satisfy the most rapacious player.

    ReplyDelete
  59. I have seen some great stuff here. Worth bookmarking for revisiting. I surprise how much effort you put to create such a great informative website. Your work is truly appreciated around the clock and the globe. Abu Dhabi Business Registration

    ReplyDelete
  60. TTG adalah keliru satu developer yg membangun permainan slot online paling menarik & seru buat mengikuti ekspresi dominan yg tertera. Hal tersebutlah yg tak jarang berada pada pada visi & misi dalam provider judi slot online terpercaya menurut TTG ini, TTG slot online ini merupakan top trending gaming yg sebagai pionir masuknya trending top gaming & dapatkan insentif berlimpah pada top trending gaming. slot gacor

    ReplyDelete
  61. The weakness is available when client input is either erroneously separated for string exacting break characters inserted in SQL articulations or client input isn't specifically and subsequently out of the blue executed. https://onohosting.com/

    ReplyDelete
  62. What should I and shouldn't do when buying a property in Panama?warehouse space for rent

    ReplyDelete
  63. Di dalam situs slot joker123 yang bisa diakses melalui link alternatif 389sports, Anda bisa menjumpai berbagai macam permainan slot online, live casino hingga judi tembak ikan online dengan beragam jackpot yang berlimpah menanti Anda. Tidak hanya itu, jika Anda bergabung bersama kami maka Anda juga bisa mendapatkan bonus new member antara 10%-50%, bonus cashback 10% dan bahkan bonus deposit pulsa yang bisa diklaim ketika Anda melakukan deposit dengan besar 10%. Cukup menarik bukan? slot joker123

    ReplyDelete
  64. Justru taruhan tersebut memberikan peluang menang besar kepada para penjudi. Bukan hanya satu keuntungan saja, melainkan ada beragam jenis profit yang bisa anda miliki. Di gambling bola, terdapat pasaran terbaik. Pasaran gambling bola memberikan dukungan pengalaman berjudi yang lebih mumpuni. Anda sebagai pemula yang belum memiliki pemahaman terhadap dunia gambling bola, dapat mengasah kemampuan yang jauh lebih meningkat ketika berhasil mencoba satu per satu pasarannya. Sudah pasti target anda dalam berjudi adalah memenangkan taruhan, bukan? situs judi bola

    ReplyDelete
  65. I precisely had to thank you so much yet again. I do not know the things I might have implemented in the absence of the actual creative ideas shared by you on such a area of interest. It absolutely was an absolute frightful crisis in my opinion, but finding out your professional approach you solved it took me to leap with delight. I am happy for this guidance as well as wish you comprehend what an amazing job you were undertaking teaching many others through a blog. I know that you’ve never encountered all of us. why is Snapchat not working

    ReplyDelete
  66. This is an excellent post I seen thanks to share it. It is really what I wanted to see hope in future you will continue for sharing such a excellent post. check this site

    ReplyDelete
  67. Many small business fear expansion at this time, they fear hiring additional employees, plus they are completely focused on the future.  have a peek at these guys

    ReplyDelete
  68. Documentaries have gained in popularity in the modern world as society has become more aware of current affairs, which is an indication that society has become more ‘woke.’ With the rise of big stream websites such as Netflix, it is easier to watch documentaries than ever before because each episode is only 10-15 minutes long. click here for more information

    ReplyDelete
  69. daftar dan main sekarang juga di situs slot terbaik dan terpercaya sc88slot

    ReplyDelete