How should I structure my settings table with MySQL?

后端 未结 4 1350
我在风中等你
我在风中等你 2021-02-18 12:35

What\'s the best way to structure a MySQL table for storing admin settings?

Like this?

Setting _|_ Value
setting1 |   a
setting2 |   b
setting3 |   c
set         


        
4条回答
  •  眼角桃花
    2021-02-18 13:35

    Table name = 'settings'

    name  | varchar <-- primary key
    value | varchar
    

    Then you can query like this:

    SELECT * FROM settings WHERE name = 'default_printer';
    

    This option is nice and easy and it will work well with 10, or 10,000 settings. With the other option you'll have to add a new column, which would be a completely pointless waste of time.

    Edit

    After your 1st comment you could choose multiple values like this:

    SELECT * FROM settings WHERE name IN ('default_printer','default_page_size');
    

    :-)

提交回复
热议问题