Key/Value pairs in a database table

后端 未结 6 1379
轻奢々
轻奢々 2020-12-08 08:38

I need to design a Key/value table in my database and I\'m looking for guidance on the best way to do this. Basically, I need to be able to associate values to a dynamic se

6条回答
  •  伪装坚强ぢ
    2020-12-08 09:05

    It seems to me like you might have a couple design choices.

    Choice 1: A two table design you hinted at in your answer

    Keys (
     id int not null auto_increment
     key string/int
    )
    values (
     id int not null auto_increment
     key_id int
     value string/varchar/int
    )
    

    Choice 2: perhaps as sambo99 pointed out you could modify this:

    keys (
     id int not null auto_increment
     key string/int
     hash_code int -- this would be computed by the inserting code, so that lookups would effectively have the id, and you can look them up directly
    )
    
    values (
     id int not null auto_increment -- this column might be nice since your hash_codes might colide, and this will make deletes/updates easier
     key_id int -- this column becomes optional
     hash_code int
     value string/varchar/int...
    )
    

    --

提交回复
热议问题