Are Foreign Keys in SQL Server 2008 are automatically indexed with a value? For Example. if I add a value in my Primary key (or auto incremetend) in may parent table will the table that has a foreign key referenced to that key will automatically have the same value? or I Have to do it explicitly?
Share
No, if you create a foreign key in a child table, it will not automatically get populated when a parent row gets inserted. If you think about this it makes sense. Let’s say you have a table like:
What you are suggesting is that when you add a row to
Students, the system should automatically add a row toStudentLoans– but what if that student doesn’t have a loan? If the student does have a loan, what should the amount be? Should the system pick a random number?Typically what will happen in this scenario is that you’ll be adding a student and their loan at the same time. So if you know the loan amount and the student’s name, you can say: