Let's say I have the following tables:
Questions, Choices, Answers
Each Question has many Choices (Choices has a question_id foreign key.)
The Answers table has two foreign keys, question_id and choice_id, and these constraints prevent answers that refer to a non-existent question or choice, but I want to prevent invalid choices, too.
How could I express a constraint (maybe that's not the right word) that only allows answers where the choice_id is a valid choice for the given question_id? For example, if I had 2 questions, each with 2 choices:
- Q1 (id = 1)
- A (id = 1)
- B (id = 2)
 
- Q2 (id = 2)
- C (id = 3)
- D (id = 4)
 
I want to allow answers like (question_id = 1, choice_id = 1 (or 2)) and prevent answers like (question_id = 1, choice_id = 4) because that's not a valid choice for the question.
 
     
    