Reputation: 91
I am creating an MVC project and have decided to implement the database using EF code first approach. I have been going through the tutorials here.
In the tutorials, they have a student table and a student address table which has a one-to-one relationship.
The thing I find confusing is that they have a foreign key of StudentID in the student address table, where as in my mind it would make more sense to have a foreign key of StudentAddressID in the Student table. My reasoning behind this is that, if I wanted to see what address was associated with a particular student, I am more likely to go to the student first and look at the address ID rather than the other way around. So my question is why have they done it that way and does it even matter?
Upvotes: 1
Views: 53
Reputation: 133
A student MAY have an address associated to it. so if it was the way you think is more logical, there would be an extra column in the Student table which would not always be used, because in cases the student does not have an address then you are storing nothing in the column allocated to StudentAddressID in Student table, so this way you are wasting storage. Also, it is simpler to have one primary key for both tables, rather than having a different ID for the address table.
Upvotes: 1