Ulp*_*hat 6 sql database table-structure
让我们考虑,我们有人称一.人˚F是他的父亲,人中号是他的母亲,人乙是他的哥哥和人的小号是他的儿子.每个人可能有很多关系.这就是为什么,我们必须创建如下的新关系表:
人
+----+------+
| id | name |
+----+------+
| 1 | A |
| 2 | F |
| 3 | M |
| 4 | B |
| 5 | S |
+----+------+
Run Code Online (Sandbox Code Playgroud)
关系类型
+----+---------+
| id | value |
+----+---------+
| 1 | Father |
| 2 | Mother |
| 3 | Brother |
| 4 | Son |
| 5 | Wife |
| 6 | Husband |
+----+---------+
Run Code Online (Sandbox Code Playgroud)
关系
+----+----------+------------+------+
| 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 |
| ...... |
+----+----------+------------+------+
Run Code Online (Sandbox Code Playgroud)
在这种情况下,第一行表示2 is father of 1(对于ID)和第五行表示1 is son of 2.在现实世界中,这两行是等价的,但是如果我不插入其中一行,我就不能错过使用现有行的行.
问题是:如何制作结构,其中包含1行中的这两个含义?
事实上,在 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...
Run Code Online (Sandbox Code Playgroud)
因此,如果您为第三个表(关系)提供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 |
| ...... |
+----+----------+------------+---------------+-----------------------+
Run Code Online (Sandbox Code Playgroud)