How should I structure my settings table with MySQL?

孤者浪人 提交于 2019-12-14 03:39:52

问题


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

Like this?

Setting _|_ Value
setting1 |   a
setting2 |   b
setting3 |   c
setting4 |   d
setting5 |   e

Or like this?

|--------|_setting1_|_setting2_|_setting3_|_setting4_|_setting5_|
Settings |    a     |    b     |    c     |    d     |    e     |

Or maybe some other way?


回答1:


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');

:-)




回答2:


Consider the first option (Setting, Value) as columns. But also consider adding additional, meta columns as well, such as Description (would come in handy if you have alot of ambiguous settings), PreviousValue, LastUpdated, UpdatedBy, etc.




回答3:


Your first example, name-value pairs or EAV, allows for a good deal more flexibility.

Check out the wiki page about EAV modelling in databases.




回答4:


As usual, it depends. Solution 1 is simpler. Sol #2 easily integrates with ORMs, but may hit DB row size limitations. Google for OTLT (as in One True Lookup Table problem) How much settings do you have(few? dozens? hundreds?) How often will you need them?



来源:https://stackoverflow.com/questions/2469492/how-should-i-structure-my-settings-table-with-mysql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!