views:

69

answers:

2

Lets say I have a table with "Groups of Questions"

GroupID | value
--------+------
42      | How often do you
9071    | Other question
...

And, for each group of questions I have "questions" and "possible answers"

Group | QuestionID | value
------+------------+------
42    | 5          | ... brush your teeth?
42    | 89         | ... go to the movies?
9071  | 709        | ... another question ...
...
Group | Answer | value
------+--------+------
42    | 134    | several times per day
42    | 135    | twice a day
42    | 71     | once a day
42    | 803    | every other day
42    | 8      | once a week
42    | 666    | once a month
...

Now, in PHP/HTML, I do a (virtual) full cross product of questions and possible answers of group 42 to build a 2-entry table where the user will select his/her answers (HTML version)

How often do you      | N/d | 2/d | 1/d | d*2 | 1/w | 1/m |
----------------------+-----+-----+-----+-----+-----+-----+
... brush your teeth? | ( ) | (X) | ( ) | ( ) | ( ) | ( ) |
... go to the movies? | ( ) | ( ) | ( ) | ( ) | (X) | ( ) |

I know I'll need to
insert into answers (question, answer, ...) values (5, 135, ...) for teeth
and
insert into answers (question, answer, ...) values (89, 8, ...) for movies

The question is: is there a way to enforce that both question and answer, in the answers table, "belong" to the same group of questions?

I'd like that
insert into answers (question, answer, ...) values (709, 71, ...)
be disallowed, because question 709 "belongs" to group 9071 and answer 71 belongs to group 42.

+1  A: 

Well, the answer relates to the question, not to the group (at least not directly), so I would have thought the "possible answers" table should be:

Question | Answer | value
---------+--------+------
5        | 134    | several times per day

Then when you insert an answer, you give the QuestionId and AnswerId, and together they reference the "possible answer" and thereby, indirectly, the group.

Tony Andrews
What's to stop QuestionID = 709 (from GroupID = 9071) and Answer = 134 (from GroupID = 42) appearing on the same row?
onedaywhen
Answer 134 would not be a possible answer for Question 709, so therefore it would be invalid for the user to choose it at all.
Tony Andrews
The same set of answers can be used for many many questions. With your approach, the possible answers would be repeated in the `Question` table for every question in the same group.
pmg
@Tony Andrews: what I meant was, what is there _in the database_ to make that combination invalid? You shouldn't rely on the UI to offer only valid choices.
onedaywhen
@onedaywhen: <i>in the database</i> I have answer 134 belongs to question 654 (or whatever), so when the question is 709, 134 isn't a valid answer. So not sure what your point is?
Tony Andrews
@pmg: OK, I see your point (at last!): a group like 42 consists of a set of questions that all have the same set of possible answers. Now my answer would be same as onedaywhen's.
Tony Andrews
Light bulb goes on :) Thanks for hanging on in there.
onedaywhen
A: 

You include the columns Group, QuestionID and Answer then create two foreign keys, one on (Group, QuestionID) and the other on (Group, Answer).

onedaywhen
Thank you, this works :)
pmg