My approach to this problem is to create a table, which is a separate column for each configuration variable, as well as for any other data set, and to set the primary key so that the table cannot contain more than one record. I do this by setting the primary key as an enumeration with a single valid value, for example:
CREATE TABLE IF NOT EXISTS `global_config` ( `row_limiter` enum('onlyOneRowAllowed') NOT NULL DEFAULT 'onlyOneRowAllowed',
After you make this setting, any of the values ββcan then be changed using a simple UPDATE statement, viewed using a simple SELECT statement combined with other tables that will be used in more complex queries, etc.
Another advantage of this approach is that it allows you to use the correct data types, foreign keys, and all other things that are relevant to the database design to ensure the integrity of the database. (Just make sure your foreign keys are on DELETE SET NULL or ON DELETE RESTRICT, not ON DELETE CASCADE). For example, let's say that one of your configuration variables is the user identifier of the main site administrator, you can extend the example as follows:
CREATE TABLE IF NOT EXISTS `global_config` ( `row_limiter` enum('onlyOneRowAllowed') NOT NULL DEFAULT 'onlyOneRowAllowed', `someconfigvar` int(10) UNSIGNED NOT NULL DEFAULT 0, `someotherconfigvar` varchar(32) DEFAULT 'whatever', `primary_admin_id` bigint(20) UNSIGNED NOT NULL, PRIMARY KEY(`row_limiter`), FOREIGN KEY(`primary_admin_id`) REFERENCES `users`(`user_id`) ON DELETE RESTRICT ON UPDATE CASCADE ) ENGINE = InnoDB; INSERT IGNORE INTO `global_config` (`primary_admin_id`) VALUES (1);
This ensures that you always have a valid configuration, even if the configuration variable must refer to some other object in the database.
source share