When I should use one to one relationship?

后端 未结 13 1629
面向向阳花
面向向阳花 2020-11-27 11:32

Sorry for that noob question but is there any real needs to use one-to-one relationship with tables in your database? You can implement all necessary fields inside one table

13条回答
  •  隐瞒了意图╮
    2020-11-27 11:55

    In my time of programming i encountered this only in one situation. Which is when there is a 1-to-many and an 1-to-1 relationship between the same 2 entities ("Entity A" and "Entity B").

    When "Entity A" has multiple "Entity B" and "Entity B" has only 1 "Entity A" and "Entity A" has only 1 current "Entity B" and "Entity B" has only 1 "Entity A".

    For example, a Car can only have one current Driver, and the Driver can only drive one car at a time - so the relationship between the concepts of Car and Driver would be 1 to 1. - I borrowed this example from @Steve Fenton's answer

    Where a Driver can drive multiple Cars, just not at the same time. So the Car and Driver entities are 1-to-many or many-to-many. But if we need to know who the current driver is, then we also need the 1-to-1 relation.

提交回复
热议问题