I want to create web site with tests. Test contains different questions. It could be textbox, checkbox or radiobutton. I want to store in db questions, answers and users.
Here is my thought about tables structure.
Users
{
Id
Name
}
Questions
{
Id
Description
Type // textbox, checkbox, etc
}
Answers
{
Id
UserId
QuestionId
AnswerValue
}
But how i can store information about DIFFERENT answers? I can write in AnswerValue something like "Carrot" (textbox),"1011" (checkbox),"3" (radiobutton) etc Unfortunally it's not good solution i think. What is best way to design DB structure in my case?
Maybe the design isn't too bad...
Since you know the QuestionId of the Answer, you can lookup which type of question it was.
For example:
SELECT
Q.Type, A.* FROM Question Q, Answer A
WHERE
Q.Id = A.QuestionId
Then in your code, when you're displaying answers, you'd have both the answer value and its type, so your code could process based on Q.Type.
To address the issue of how to store answers to questions with multiple values (checkboxes or radiobuttons) you should consider storing those in another table with a one-to-many relationship.
For example, a Checkbox question can have many Items (check-able things), and a Checkbox answer can have many items (the items which were checked). In your current schema, there doesn't seem to be any way to store the descriptions of each item. So, I suggest a new tables Items and Answers_Items
Items{
ItemId
QuestionId
Description
}
Answers_Items{
ItemId
AnswerId
AnswerValue
}