0

I have two tables: "Person" and "Details". Person should have one to one relation with Details. I have two options:

  1. Write DetailsId in Details table and make it FK to Details.Id(PK);
  2. Make Details.PersonId as PK and FK at the same time to Person.Id;

In both cases relationship is one to one. Can you explain which one is better and in what cases?

EDIT:

I'm using entity framework and want to get Detail like this: Person.Detail, not Person.Details.First()

karaxuna
  • 26,752
  • 13
  • 82
  • 117

3 Answers3

3

Make two tables "Person" and "Details". In "Details" table, take a field "person_id" which will be your FK that will refer to id field(PK) of the "Person" table. That is the better solution. Because "Details" table will require more operations like update etc. than "Person" table. Therefore make "Person" table as Master & "Details" as slave.

2

For a truly 1:1 relationship, your default solution should be to merge the tables together, unless there are specific reasons against it.

But I suspect you are actually talking about 1:0..1, in which case (2) will model what you need.

I don't understand the (1) - what's the purpose of the self-referencing FK there? Did you actually mean a FK in Person referencing Details?

Community
  • 1
  • 1
Branko Dimitrijevic
  • 50,809
  • 10
  • 93
  • 167
2

Are there specialized subclasses of "persons"? In that case, you may want to have more than two tables, with a different subclass table for each subclass. See the tag "[Class Table Inheritance]" here in SO. You also should look up the tag "[Shared-Primary-Key]". It works better than having a separate FK and PK for the subclass table.

You also need to consider the benefits and the costs of choosing a single table solution.

Walter Mitty
  • 18,205
  • 2
  • 28
  • 58