Hi,
I have the following table
CREATE TABLE tbAnswers
(answer_id INTEGER NOT NULL PRIMARY KEY,
answer_correct BINARY NOT NULL,
answer_position INTEGER NOT NULL,
answer_text VARCHAR(150) NOT NULL,
item_id INTEGER NOT NULL REFERENCES tbItems(item_id) ON UPDATE CASCADE ON DELETE CASCADE,
UNIQUE(answer_position,item_id);
(actually I wanted a boolean for answer_correct but I had to do that by hand afterwards, anyone know how to do it in a create table statement?)
Now for the real problem: I have a constraint on it that says there should be minimum one correct answer per question, that one works fine. But I also want a constraint that says there can not be more than one correct answer per question. My first try was:
CONSTRAINT cstMax_one_correct_answer CHECK(UNIQUE(SELECT item_id FROM tbAnswers WHERE answer_correct=true))
which isn't accepted by access (also not without the "check"). My second try was:
CONSTRAINT cstMax_one_correct_answer CHECK((SELECT COUNT(*) FROM tbAnswers WHERE answer_correct=true)<=(SELECT COUNT(*) FROM (SELECT DISTINCT item_id FROM tbAnswers)))
which also doesn't work. Apparently I may not use distinct in a check clause. I'd really appreciate some help with this.
kind regards,
Jesse
I have the following table
CREATE TABLE tbAnswers
(answer_id INTEGER NOT NULL PRIMARY KEY,
answer_correct BINARY NOT NULL,
answer_position INTEGER NOT NULL,
answer_text VARCHAR(150) NOT NULL,
item_id INTEGER NOT NULL REFERENCES tbItems(item_id) ON UPDATE CASCADE ON DELETE CASCADE,
UNIQUE(answer_position,item_id);
(actually I wanted a boolean for answer_correct but I had to do that by hand afterwards, anyone know how to do it in a create table statement?)
Now for the real problem: I have a constraint on it that says there should be minimum one correct answer per question, that one works fine. But I also want a constraint that says there can not be more than one correct answer per question. My first try was:
CONSTRAINT cstMax_one_correct_answer CHECK(UNIQUE(SELECT item_id FROM tbAnswers WHERE answer_correct=true))
which isn't accepted by access (also not without the "check"). My second try was:
CONSTRAINT cstMax_one_correct_answer CHECK((SELECT COUNT(*) FROM tbAnswers WHERE answer_correct=true)<=(SELECT COUNT(*) FROM (SELECT DISTINCT item_id FROM tbAnswers)))
which also doesn't work. Apparently I may not use distinct in a check clause. I'd really appreciate some help with this.
kind regards,
Jesse