![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
[I realise that this isn't necessarily a question specific to MySQL, but it's the database system that I'm using..] I'm about to create a table to extend a database to allow users to store (via a webform) a particular preference choice setting in the database. At present, the options available to users will be that they can choose: * this xor * that (ie, only 1 of 2 possible values). But I suppose that at some point in the future, we might decide to let users choose 1 from an increased range of choices (1 from n). I was wondering what best practice thinking was on how best to deal with this? With only 1 of 2 choices, effectively 1 boolean choice, I could define a single field 'choice' (obviously not the real name) and store the values as 0 or 1 (as long as I clearly define for my application how these map to one choice versus the other). Were extra choice possibilities to become available, this would obviously not work, but instead I could redefine my 'choice' field with acceptable values of 0, 1, 2, ..n (or perhaps as strings 'this', 'that', 'the other', ..'the last choice', which although perhaps more self-explanatory when a human admin needs to read the database would probably make for messier application coding). Or instead, I could define 'n' number of pseudo-boolean fields (with 0 or 1 values): * this_set * that_set * the_other_set .. * the_last_choice_set This way, it should be clear from the field names what they mean, and easy to determine whether they are set or not. The last suggestion seems to me to perhaps be the most tidily-extensible way to do it (and would perhaps most easily allow users to choose any combination of 'this', 'that', ..'the last choice' rather than just *one*, were the rules to change), but I was wondering if anybody else had any thoughts about this? Thanks, David. |
#3
| |||
| |||
|
|
David wrote: [I realise that this isn't necessarily a question specific to MySQL, but it's the database system that I'm using..] I'm about to create a table to extend a database to allow users to store (via a webform) a particular preference choice setting in the database. At present, the options available to users will be that they can choose: * this xor * that |
|
(ie, only 1 of 2 possible values). But I suppose that at some point in the future, we might decide to let users choose 1 from an increased range of choices (1 from n). |
| Have a look at enumerated variables. I believe they are an efficient and extensible way to store multiple exclusive choices. |
#4
| |||
| |||
|
|
[I realise that this isn't necessarily a question specific to MySQL, but it's the database system that I'm using..] I'm about to create a table to extend a database to allow users to store (via a webform) a particular preference choice setting in the database. At present, the options available to users will be that they can choose: * this xor * that (ie, only 1 of 2 possible values). But I suppose that at some point in the future, we might decide to let users choose 1 from an increased range of choices (1 from n). I was wondering what best practice thinking was on how best to deal with this? With only 1 of 2 choices, effectively 1 boolean choice, I could define a single field 'choice' (obviously not the real name) and store the values as 0 or 1 (as long as I clearly define for my application how these map to one choice versus the other). Were extra choice possibilities to become available, this would obviously not work, but instead I could redefine my 'choice' field with acceptable values of 0, 1, 2, ..n (or perhaps as strings 'this', 'that', 'the other', ..'the last choice', which although perhaps more self-explanatory when a human admin needs to read the database would probably make for messier application coding). Or instead, I could define 'n' number of pseudo-boolean fields (with 0 or 1 values): * this_set * that_set * the_other_set .. * the_last_choice_set This way, it should be clear from the field names what they mean, and easy to determine whether they are set or not. The last suggestion seems to me to perhaps be the most tidily-extensible way to do it (and would perhaps most easily allow users to choose any combination of 'this', 'that', ..'the last choice' rather than just *one*, were the rules to change), but I was wondering if anybody else had any thoughts about this? Thanks, David. |
#5
| |||
| |||
|
|
On 2011-06-16 16:09, The Natural Philosopher wrote: David wrote: I'm about to create a table to extend a database to allow users to store (via a webform) a particular preference choice setting in the database. At present, the options available to users will be that they can choose: * this xor * that some food for thought create table possible_choices ( choice char(4) not null primary key ) engine = innodb; insert into possible_choices (choice) values ('this'),('that'); create table answers ( user ... choice char(4) not null references possible_choices (choice), primary key (user, choice) ) engine = innodb; |
|
(ie, only 1 of 2 possible values). But I suppose that at some point in the future, we might decide to let users choose 1 from an increased range of choices (1 from n). insert into possible_choices (choice) values ('bang'),('buck'); Typically a check constraint is used to define such small domain, but that is not supported in mysql so I've used a foreign key against a domain table instead. |
|
Have a look at enumerated variables. I believe they are an efficient and extensible way to store multiple exclusive choices. Enums are dangerous and confusing creatures at the database layer and one should think twice before summing them (IMHO). |
#6
| |||
| |||
|
|
None of the above. Use the relational aspects of the database. You have one table with the user information and an user_id field. A second table with a choice_id and choice information (at least a name, maybe also a description - two or 3 columns total). A third table has two columns - user_id and choice_id for the selected options. |
|
And don't worry about the database being "human-readable" - databases are not made to be human-readable - they are made to store data. If you want human-readable, you execute the appropriate SELECT statement to get the information in the format you want. |
#7
| |||||||||||
| |||||||||||
|
|
I'm about to create a table to extend a database to allow users to store Why _create a table_ ? An obvious thing to do is _add a column to a table_, with an additional table (or two) maybe needed or maybe not. |
|
(via a webform) a particular preference choice setting in the database. At present, the options available to users will be that they can choose: * this xor * that (ie, only 1 of 2 possible values). But I suppose that at some point in the future, we might decide to let users choose 1 from an increased range of choices (1 from n). Are you sure the choice will never be "choose 7 of 9" (and no, I don't mean the Star Trek character)? |
|
I was wondering what best practice thinking was on how best to deal with this? With only 1 of 2 choices, effectively 1 boolean choice, I could define a single field 'choice' (obviously not the real name) and store the values as 0 or 1 (as long as I clearly define for my application how these map to one choice versus the other). This works. You need to add this to an existing table, or to also have a user id column in a new table, otherwise you end up with a single choice for the entire website, not one choice for each user. |
|
I really hope you're not in a situation of not being able to modify the existing table because adding a column (even at the end) breaks existing code. |
|
Were extra choice possibilities to become available, this would obviously not work, but instead I could redefine my 'choice' field with acceptable values of 0, 1, 2, ..n (or perhaps as strings 'this', 'that', 'the other', ..'the last choice', which although perhaps more self-explanatory when a human admin needs to read the database would probably make for messier application coding). You could make then "choice" field into a foreign key for a new table (e.g. "Payment_methods") that lists all the choice codes and their descriptions. [..] |
|
Or instead, I could define 'n' number of pseudo-boolean fields (with 0 or 1 values): This creates a problem that you might end up with more than one of them set to 1 (or none of them). |
|
It also has the problem that you have to modify the application to use the new columns. |
|
If every application is checking that the choices are set properly, you may have to modify all parts of the application *ALL AT ONCE*, |
|
since any user who has taken the new choice will appear to be a user who has taken *NO* choice to a part of the application that hasn't been updated yet to know about the new field. |
|
* this_set * that_set * the_other_set .. * the_last_choice_set This way, it should be clear from the field names what they mean, and easy to determine whether they are set or not. But you have to add the new field name to every query involving them. |
|
The last suggestion seems to me to perhaps be the most tidily-extensible way to do it (and would perhaps most easily allow users to choose any combination of 'this', 'that', ..'the last choice' rather than just *one*, were the rules to change), but I was wondering if anybody else had any thoughts about this? There's tidily-extensible and there's dangerously-extensible. What would be the consequences of somehow accidentally choosing all the choices (or none of them)? |
#8
| |||
| |||
|
|
On 2011-06-17, Jerry Stuckle<jstucklex (AT) attglobal (DOT) net> wrote: None of the above. Use the relational aspects of the database. You have one table with the user information and an user_id field. A second table with a choice_id and choice information (at least a name, maybe also a description - two or 3 columns total). A third table has two columns - user_id and choice_id for the selected options. Thanks, Jerry. This is essentially the same as Lennart suggested. And don't worry about the database being "human-readable" - databases are not made to be human-readable - they are made to store data. If you want human-readable, you execute the appropriate SELECT statement to get the information in the format you want. I take your point, and I am sure you are probably right, but often it certainly _seems_ that it can be easier to to have fields that are "human readable" to some degree. I "inherited" this database with minimal documentation, and had to spend quite some time working out how various tables, mostly with numeric id data (references to ids in other tables) all actually fitted together, before I could even begin to re-implement the existing system (moving to a new web platform), let alone add desired new features! David. |
#9
| |||
| |||
|
|
There are a number of tools out there which can create a diagram from an existing database. |
#10
| |||
| |||
|
|
On 7/6/2011 10:28 AM, Jerry Stuckle wrote: There are a number of tools out there which can create a diagram from an existing database. Any recommendations, Jerry? I have looked for the likes of this but have only found apps that are bloated and kludgy. |
![]() |
| Thread Tools | |
| Display Modes | |
| |