How to store data with dynamic number of attributes in a database

前端 未结 8 2317
挽巷
挽巷 2020-12-04 13:14

I have a number of different objects with a varying number of attributes. Until now I have saved the data in XML files which easily allow for an ever changing number of attr

8条回答
  •  温柔的废话
    2020-12-04 13:22

    Let me give some concreteness to what DVK was saying.

    Assuming values are of same type the table would look like (good luck, I feel you're going to need it):

    dynamic_attribute_table
    ------------------------
    id         NUMBER
    key        VARCHAR
    value      SOMETYPE?
    

    example (cars):

    |id|    key   |   value   |
    ---------------------------
    | 1|'Make'    |'Ford'     |
    | 1|'Model'   |'Edge'     |
    | 1|'Color'   |'Blue'     |
    | 2|'Make'    |'Chevrolet'|
    | 2|'Model'   |'Malibu'   |
    | 2|'MaxSpeed'|'110mph'   |
    

    Thus,
    entity 1 = { ('Make', 'Ford'), ('Model', 'Edge'), ('Color', 'Blue') }
    and,
    entity 2 = { ('Make', 'Chevrolet'), ('Model', 'Malibu'), ('MaxSpeed', '110mph') }.

提交回复
热议问题