在UML中表示一对一关系(引用)的正确方法

时间:2012-04-17 18:38:48

标签: database-design uml

我有两张表,它们之间有一对一的关系:

Person
 - Id
 - HouseFk

House
 - Id

人拥有House的外键。请注意,HouseFk也可以为NULL。在UML中表示这个的正确方法是什么?我可以用符号表示两种变体:

1对0..1:

      1     0..1    
Person----------House

0..1到1:

      0..1     1    
Person----------House

它是哪种方式,为什么?我真的很困惑,因为我听说一对多的关系使得键的位置颠倒了,这意味着B指的是A,从中可以推断出一对一(尽管在我的情况下,它是一对多或一个方案,A是指B,因此0..1在B侧(第一个)?还是一边(第二个)?

谢谢!

1 个答案:

答案 0 :(得分:5)

你们关系的规则是什么?你需要先定义它。人'拥有'房子?人'住在'众议院?让我们假设它现在'拥有'。然后你有几个选择:

选项1:无选择性(强制性1:1):

- Each Person must own exactly one House
- Each House must be owned by exactly one Person

这将代表

       1           1
Person ------------- House

选项2a:可选单向:

 - Each Person may own at most one House
 - A Person need not own any Houses
 - Each House must be owned by exactly one Person

这将代表

       1        0..1
Person ------------- House

选项2b:另一种方式可选:

 - Each Person must own exactly one House
 - Each House may be owned by at most one Person
 - A House need not be owned by any Person

这将代表

       0..1        1
Person ------------- House

选项3:双向可选:

 - Each Person may own at most one House
 - A Person need not own any Houses
 - Each House may be owned by at most one Person
 - A House need not be owned by any Person

这将代表

       0..1     0..1
Person ------------- House

修改(*)

您的数据库架构不是以上所述。它说:

 - Each Person may own at most one House
 - A Person need not own any Houses
 - Each House may be owned by many People
 - A House need not be owned by any Person

不确定这是不是你想要的......? [如果你不明白为什么会喊出来。]

第h

*感谢ypercube在下面的评论中指出了更正。