how to save marital relationship in a database

后端 未结 6 1568
说谎
说谎 2021-01-18 11:28

I have to save this information in a database

Person -> is married to -> Person

Where should I save that information? What is the proper design pattern shoul

6条回答
  •  被撕碎了的回忆
    2021-01-18 12:00

    You could do it with a "Spouse" column on the "Person" table which can be null (for the case of an unmarried person).

    If married this holds the id of the other person, as is a foreign key.

    A better solution would be a separate "Marriage" table that has at least three columns:

    MarriageId
    Person1Id
    Person2Id
    ...
    

    The person id's are foreign keys into the "Person" table, and you should make the combination of MarriageId, Person1Id and Person2Id unique to avoid adding a row where the people are swapped over.

    Though it should be pointed out that both these models are quite basic and make assumptions about how many people can be in one marriage ;)

提交回复
热议问题