Using a Single Row configuration table in SQL Server database. Bad idea?

后端 未结 12 1920
终归单人心
终归单人心 2020-12-12 09:19

In developing a shopping cart application I\'ve found that I needed to save settings and configurations based on the administrator\'s preferences and requirements. This info

相关标签:
12条回答
  • 2020-12-12 09:55

    A single row will work fine; it will even have strong types:

    show_borders    bit
    admin_name      varchar(50)
    max_users       int
    

    One disadvantage is that it requires a schema change (alter table) to add a new setting. One alternative is normalizing, where you end up with a table like:

    pref_name       varchar(50) primary key
    pref_value      varchar(50) 
    

    This has weak types (everything is a varchar), but adding a new setting is just adding a row, something you can do with just database write access.

    0 讨论(0)
  • 2020-12-12 09:56

    A Key and Value pair is similar to a .Net App.Config which can store configuration settings.

    So when you want to retrieve the value you could do:

    SELECT value FROM configurationTable
    WHERE ApplicationGroup = 'myappgroup'
    AND keyDescription = 'myKey';
    
    0 讨论(0)
  • 2020-12-12 09:56

    Have a key column as varchar and a value column as JSON. 1 is numeric whereas "1" is a string. true and false are both boolean. You can have objects as well.

    0 讨论(0)
  • 2020-12-12 09:59

    Sorry I come like, yeaars later. But anyways, what I do is simple and effective. I simply create a table with three () columns:

    ID - int (11)

    name - varchar (64)

    value - text

    What I do before creating a new config column, updating it or reading is to serialize the "value"! This way I am sure of the type (Well, php is :) )

    For instance:

    b:0; is for BOOLEAN (false)

    b:1; is for BOOLEAN (true)

    i:1988; is for INT

    s:5:"Kader"; is for a STRING of 5 characters length

    I hope this helps :)

    0 讨论(0)
  • 2020-12-12 10:02

    You can do the Key/Value Pair without conversions by adding a column for each major type and one column telling you which column the data is in.

    So your table would look something like:

    id, column_num, property_name, intValue, floatValue, charValue, dateValue
    1, 1, weeks, 51, , ,
    2, 2, pi, , 3.14159, , 
    3, 4, FiscYearEnd, , , , 1/31/2015
    4, 3, CompanyName, , , ACME, 
    

    It uses a little more room but at most you are using a few dozen attributes. You can use a case statement off the column_num value to pull / join the right field.

    0 讨论(0)
  • 2020-12-12 10:06

    You should create a table with a column for the information type and information value (at least). This way you avoid having to create new columns every time a new information is added.

    0 讨论(0)
提交回复
热议问题