Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

In the past I have stored all generic settings and configuration items such as: custom application title, home address, version, debug, etc.. all in a "settings.php" file that I would include into all of my scripts.

I would assume that it is better practice to have a database table specifically setup for application settings such as these, in that way you dont need to edit a php file to adjust or make changes. Yet I am a bit cautious about how I would set this up.

There are two options I have been considering:

  1. I have a table with one row, many columns, making each column a unique variable value.
  2. I have two columns with the first being the variable name, and the second being the variable's value.

Any suggestions or things I should be aware of from a database design perspective?

share|improve this question
add comment

1 Answer

up vote 5 down vote accepted

Under Option 1, you are guaranteed a single row (if you remember to insert the one row firsthand).

  • If you want to introduce a new setting, you would have to do ALTER TABLE to add the needed column.
  • If you wanted to remove an old setting, you would have to do ALTER TABLE to remove the unneeded column.
  • You would have to assume to the presence of the column in the code. Otherwise, you may have to query information_schema.columns to make sure the column representing the setting needed exists in the table or not. Querying the table information_schema.columns is extremely bad in the presence of dozens (or even hundreds) of InnoDB tables in a heavy write environment. That could potentially lock information_schema.columns intermittently until it is free to read.
  • You should make sure the table count is always one(1).

Under Option 2, things are a little different

  • If you want to introduce a new setting, you would have to do an INSERT to add the needed setting as a row.
  • If you wanted to remove an old setting, you would have to do a DELETE to remove the needed setting as a row.
  • You have to query the table by variable name to see if the option is present.
  • No virtual limit to adding to or deleting a setting from a table

What do these Options have in common ?

  • If you leave old settings alone, this can allow for old versions of app code to use the old settings, but that just introduces clutter into the table.
  • Storage Engine should be InnoDB to allow multiple people to read from it.

Personally, I would choose Option 2 because creating new settings, optionally removing old settings, and checking for existing settings would all occur at one table. The method of retrieving settings would all be the same. Under Option 1, you would have to query information_schema.columns plus make certain assumptions about the settings table. The sequence of events for retrieving settings may change from code version to code version.

share|improve this answer
    
ahh yes I did not consider the fact that it would need to access information_schema.columns as well. You make a very strong argument for Option 2. Thank you much! –  CenterOrbit Aug 23 '11 at 22:18
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.