How do I get around this relational database design smell?

前端 未结 2 1571
抹茶落季
抹茶落季 2021-01-26 17:18

I have a really simple mediaTypes table which contains the following columns:

id string
name string

Each mediaType record can have many \"place

2条回答
  •  遥遥无期
    2021-01-26 17:27

    maybe this is a subjective solution. If the Placements table have no much columns, ej: (detail_col_1, detail_col_2, detail_col_3.. detail_col_6) the table design is not that bad, I mean, it doesn`t depend of how many null columns you got, maybe it looks ugly but it should work. Now, if you want a complex method I'd suggest some of these:

    1. Simple Placements table with json in it:
    MediaTypes
    + id
    + name
    
    Placements
    + id
    + mediaTypeId
    + name
    + detail
    

    In detail I can define my attributes as json, and set the correct values for each type:

    row 1: {'attr1': valx, 'attr2': valy} row 2: {'attr4': valz, 'attr1': valw}

    Now, the problem here is the query filter (you cannot). This should work if you want to save extra info.

    1. An elegant way:
    MediaTypes
    + id
    + name
    
    Placements
    + id
    + mediaTypeId
    + name
    
    DetailAttributes //table of attributes for any type 
    + id
    + name
    + mediaTypeId
    
    PlacementDetailAttributes //many to many rel between DetailAttributes&Placements
    + placementId
    + detailAttributeId
    + value
    

    With this approach you can add many attributes as you want. Query filter by attributes should work too!!

提交回复
热议问题