It often seems to me that I want to store data of more than one type (usually specially integer and textual) in the same column in a MySQL database. I know this is terrible, but the reason is that it happens when I keep the answers that people asked to questions in the questionnaire. Some questions require an integer answer, some need a text answer, and some may be an item selected from a list.
The approaches that I used in the past were:
Store everything as text and convert to int (or something else) when needed later.
There are two columns - one for text and one for int. Then you simply fill in one in each line for one answer and leave the other as zero.
There are two tables: one for text answers and one for whole answers.
I don't like it anyway, but I feel that there should be a much better way to deal with this situation.
To make it more specific, here is an example of tables that I may have:
CREATE TABLE question ( id int(11) NOT NULL auto_increment, text VARCHAR(200) NOT NULL default '', PRIMARY KEY ('id') ) CREATE TABLE response ( id int(11) NOT NULL auto_increment, question int (11) NOT NULL, user int (11) NOT NULL, response VARCHAR(200) NOT NULL default '' )
or, if I went using option 2 above:
CREATE TABLE response ( id int(11) NOT NULL auto_increment, question int (11) NOT NULL, user int (11) NOT NULL, text_response VARCHAR(200), numeric_response int(11) )
and if I used parameter 3, there would be a responseInteger table and a responseText table.
Is any of these correct, or am I not seeing an obvious alternative?
source share