How to model a database with many m:n relations on a table

后端 未结 5 1281
孤城傲影
孤城傲影 2021-01-02 06:25

I am currently setting up a database which has a large number of many-to-many relations. Every relationship was modeled via a link table. Example:

A person has a num

5条回答
  •  梦毁少年i
    2021-01-02 07:29

    I do not think the second method is correct because your Person_Attributes table would contain redundant data. For example: say a person likes 10 restaurants and works on 2 jobs, has 3 houses you would have as many as 10*2*3 entries where it should be 10 + 2 + 3(in 3 link tables...as per approach#1). Think of drawbacks having million users and if you had more than 3 attributes in Person_Attributes table to handle... so I would go with approach 1 in your question.

    Say for example your Person_Attributes table has following entry:

    personId | houseId | jobId | restaurantId
    ------------------------------------------
    P1      H1  J1  R1
    

    now if the person likes restaurants R2 and R3...table looks like

    P1      H1      J1      R1
    P2      H1      J1      R2
    P2      H1      J1      R3
    

    table already has redundant data he adds Job J2 at a later point.. your table will look like

    P1      H1      J1      R1
    P2      H1      J1      R2
    P2      H1      J1      R3
    P1      H1      J2      R1
    P2      H1      J2      R2
    P2      H1      J2      R3
    

    Now consider he adds another home H2.. so on and so forth...Do you see my point?

提交回复
热议问题