1条记录如何存储亲属关系信息
How to store family relationship information with 1 record
假设我们有一个人 A。人F是他爸爸,人M是他妈妈,人B是他弟弟,人S是他的儿子。
每个人可能有很多关系。这就是为什么,我们必须像下面这样创建新关系 table:
人
+----+------+
| id | name |
+----+------+
| 1 | A |
| 2 | F |
| 3 | M |
| 4 | B |
| 5 | S |
+----+------+
关系类型
+----+---------+
| id | value |
+----+---------+
| 1 | Father |
| 2 | Mother |
| 3 | Brother |
| 4 | Son |
| 5 | Wife |
| 6 | Husband |
+----+---------+
关系
+----+----------+------------+------+
| id | PersonID | RelativeID | Type |
+----+----------+------------+------+
| 1 | 1 | 2 | 1 |
| 2 | 1 | 3 | 2 |
| 3 | 1 | 4 | 3 |
| 4 | 1 | 5 | 4 |
| 5 | 2 | 1 | 4 |
| 6 | 2 | 3 | 5 |
| 7 | 3 | 1 | 4 |
| ...... |
+----+----------+------------+------+
在这种情况下,第 1 行表示 2 is father of 1
(对于 ID),第 5 行表示 1 is son of 2
。在现实世界中,这两行是等价的,但如果我不插入其中一行,我就无法得到缺少的行,这意味着使用现有的行。
问题是:
1行包含这2个意思的结构怎么弄?
事实上2个亲戚之间我们有2个关系:
Person A is person B`s wife <=> Person B is person A`s husband
Person C is person D`s sister <=>Person D is person C`s brother (D is male)
Etc...
因此,如果您为第三个 table(关系)提供一个 Reverse_Relation_Type 列,那么您的问题就会得到解决,您将不会 冗余 数据所有,你将有:
+----+----------+------------+---------------+-----------------------+
| id | PersonID | RelativeID | Relation_Type | Reverse_Relation_Type |
+----+----------+------------+---------------+-----------------------+
| 1 | 1 | 2 | 1 |4 |
| 2 | 1 | 3 | 2 |4 |
| 3 | 1 | 4 | 3 |3 |
| 4 | 1 | 5 | 4 |6 |
| 6 | 2 | 3 | 5 |1 |
| ...... |
+----+----------+------------+---------------+-----------------------+
假设我们有一个人 A。人F是他爸爸,人M是他妈妈,人B是他弟弟,人S是他的儿子。 每个人可能有很多关系。这就是为什么,我们必须像下面这样创建新关系 table:
人
+----+------+
| id | name |
+----+------+
| 1 | A |
| 2 | F |
| 3 | M |
| 4 | B |
| 5 | S |
+----+------+
关系类型
+----+---------+
| id | value |
+----+---------+
| 1 | Father |
| 2 | Mother |
| 3 | Brother |
| 4 | Son |
| 5 | Wife |
| 6 | Husband |
+----+---------+
关系
+----+----------+------------+------+
| id | PersonID | RelativeID | Type |
+----+----------+------------+------+
| 1 | 1 | 2 | 1 |
| 2 | 1 | 3 | 2 |
| 3 | 1 | 4 | 3 |
| 4 | 1 | 5 | 4 |
| 5 | 2 | 1 | 4 |
| 6 | 2 | 3 | 5 |
| 7 | 3 | 1 | 4 |
| ...... |
+----+----------+------------+------+
在这种情况下,第 1 行表示 2 is father of 1
(对于 ID),第 5 行表示 1 is son of 2
。在现实世界中,这两行是等价的,但如果我不插入其中一行,我就无法得到缺少的行,这意味着使用现有的行。
问题是: 1行包含这2个意思的结构怎么弄?
事实上2个亲戚之间我们有2个关系:
Person A is person B`s wife <=> Person B is person A`s husband
Person C is person D`s sister <=>Person D is person C`s brother (D is male)
Etc...
因此,如果您为第三个 table(关系)提供一个 Reverse_Relation_Type 列,那么您的问题就会得到解决,您将不会 冗余 数据所有,你将有:
+----+----------+------------+---------------+-----------------------+
| id | PersonID | RelativeID | Relation_Type | Reverse_Relation_Type |
+----+----------+------------+---------------+-----------------------+
| 1 | 1 | 2 | 1 |4 |
| 2 | 1 | 3 | 2 |4 |
| 3 | 1 | 4 | 3 |3 |
| 4 | 1 | 5 | 4 |6 |
| 6 | 2 | 3 | 5 |1 |
| ...... |
+----+----------+------------+---------------+-----------------------+