how to save marital relationship in a database

后端 未结 6 1570
说谎
说谎 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:09

    If you can only be maried to one person: 1:1

    -------------
    - Person    -
    -------------
    id (key)
    maried_to_id (foreign key)
    

    If you can be maried to more than one person or want to keep track of previous mariages, n:n

    -------------
    - Person    -
    -------------
    person_id (key)
    
    -------------
    - Mariage   -
    -------------
    first_person_id (foreign key)
    second_person_id (foreign key)
    start_date
    end_date
    

    (also first_person_id + second_person_id + date form a unique key for mariage. You could leave out the date, but then remariages wouldnt be tracked)

提交回复
热议问题