开发者

Database design with recursion

开发者 https://www.devze.com 2023-04-05 06:34 出处:网络
I have table Person. One person can have many siblings from the same table. I have a hard time understanding on how to do this design, because If I do recursive relationship I can only add on开发者_JA

I have table Person. One person can have many siblings from the same table. I have a hard time understanding on how to do this design, because If I do recursive relationship I can only add on开发者_JAVA技巧e sibling.

Table: PersonId, LastName,FirstName.

help please!


Because the sibling relationship is many to many, you need a second table called SIBLING, with two columns, the person id of each sibling in the relationship.

PERSON
+----------+--------------+--------------+
| PersonId | Last Name    | First Name   |
+----------+--------------+--------------+
| 1        | Abc          | Def          |
| 2        | Ghi          | Def          |
| 3        | Jkl          | Stu          |
| 4        | Mno          | Def          |
| 5        | Pqr          | vwx          |
+----------+--------------+--------------+

SIBLING
+-----+-----+
| Id1 | Id2 |
+-----+-----+
| 1   | 2   |
| 1   | 4   |
| 2   | 4   |
| 3   | 5   |
+-----+-----+

Here SIBLING is sometimes called a "join table" or "association table." Its PK is the whole table (a composite PK) and each column is a FK to PERSON.PersonId.

0

精彩评论

暂无评论...
验证码 换一张
取 消