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?
There is no way to organize tables hierarchically that is meaningful to MySQL. As others have noted, you can maintain separate schemas (in MySQL, databases), or you can develop and use a table naming structure that organizes your tables in a way that is meaningful to users (but is still not meaningful to MySQL).
To remove a table in MySQL, use the DROP TABLE statement. The basic syntax of the command is as follows: DROP [TEMPORARY] TABLE [IF EXISTS] table_name [, table_name] [RESTRICT | CASCADE];
Use a CREATE TABLE statement to specify the layout of your table: mysql> CREATE TABLE pet (name VARCHAR(20), owner VARCHAR(20), species VARCHAR(20), sex CHAR(1), birth DATE, death DATE); VARCHAR is a good choice for the name , owner , and species columns because the column values vary in length.
MySQL (and MariaDB) allows you to change the structure of tables with the ALTER TABLE SQL command.
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');
:-)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With