CREATE TABLE test.QUESTIONS
(
   QUESTION_ID INTEGER NOT NULL primary key AUTO_INCREMENT,
   QUESTION_TEXT varchar(512) NOT NULL
);

CREATE UNIQUE INDEX QUESTIONS_INDEX ON test.QUESTIONS (QUESTION_ID);

CREATE TABLE test.CHOICES
(
   CHOICE_ID INTEGER NOT NULL primary key AUTO_INCREMENT,
   CHOICE_TEXT varchar(256) NOT NULL,
   QUESTION_ID int NOT NULL
);

CREATE UNIQUE INDEX CHOICES_INDEX ON test.CHOICES(CHOICE_ID);

CREATE TABLE test.ANSWERS
(
   ANSWER_ID INTEGER NOT NULL primary key AUTO_INCREMENT,
   NAME varchar(128),
   QUESTION_ID int NOT NULL
);

CREATE UNIQUE INDEX ANSWERS_INDEX ON test.ANSWERS(ANSWER_ID);

CREATE TABLE test.ANSWERS_CHOICES
(
   ANSWER_ID int NOT NULL,
   CHOICE_ID int NOT NULL
);

CREATE UNIQUE INDEX ANSWERS_CHOICES_INDEX ON test.ANSWERS_CHOICES (ANSWER_ID, CHOICE_ID);

ALTER TABLE test.CHOICES
ADD FOREIGN KEY (QUESTION_ID) 
REFERENCES test.QUESTIONS(QUESTION_ID);

ALTER TABLE test.ANSWERS
ADD FOREIGN KEY (QUESTION_ID) 
REFERENCES test.QUESTIONS(QUESTION_ID);

ALTER TABLE test.ANSWERS_CHOICES
ADD FOREIGN KEY (ANSWER_ID) 
REFERENCES test.ANSWERS(ANSWER_ID);

ALTER TABLE test.ANSWERS_CHOICES
ADD FOREIGN KEY (CHOICE_ID) 
REFERENCES test.CHOICES(CHOICE_ID);