MySQL Normalization Question
April 27, 2006 1:41 PM
Subscribe
DBAs: Is this a case where I *should not* normalize my database? (PHP/MySQL 4.0.24 question). Long explanation of the situation inside.
So here's the deal -- I'm (re)writing an app that needs to allow users to build questions which can be later used in forms presented by that application use in forms. Questions that are straightforward ("Fill out this field", "Pick from these choices") are very simple to do. However, it gets a little more complicated when you have questions like "Pick from these choices, if other, please fill out the blank" or "If the answer to question 2 is yes, please answer question 3?" -- ie, dependent questions.
I'm currently storing questions as a single record in the database. When I have multiple choices for each question, I newline-separate them and stick them into a field called "question_data". When I present this question to a user, I simply grab the question (via ID number) and explode/implode the "question_data" field into the proper HTML markup. I use an arbitrary symbol when I need to do something like the "Other" field above (although I don't yet have a way of doing the "If the answer to question two is yes, please answer question 3" but I'm sure i could write one. When I need to perform calculations using this data (ie, how many people answered question 2 with answer B?), I get all the users answers and tally up the occurence of each -- "other" responses are prefixed with an "Other: " string that is trivial to find with PHP.
Now, this solution has worked well so far because 1) doing database dumps of user demographics is pretty straightforward (I can just dump their question field directly into the report) and 2) everything is just in one field, *even* if it is an "Other" answer -- ie, if the user gives a "non-official" answer, then I don't have to go look for it.
I've been trying to read more about database design in my spare time (I am by no means a DBA) and it seems like what I'm doing with the question_data field is a big no-no. I can think of some advantages to making another table and having individual records for each question option available, but it seems like calling everything up to display the question (which is the more important thing for this app) would take longer than the way I'm currently doing things (ie, it'd be a join AND I'd have like, 25 rows to deal with).
So what should I do here? Am I looking at the problem totally wrong? Should I force normalization onto these question options? Is the way I'm using question_options already normalized?
or is this a case where I should just comma-separate the "question_data" field and upgrade to mysql 5 so I can do FIND_IN_SETs?
i realize this is a confusing configuration to explain without pictures -- I'll do my best to fill in any important holes I might have missed.
posted by fishfucker to computers & internet (25 comments total)
posted by felix at 1:47 PM on April 27, 2006