I have a problem in updating records to a derby db table with foreign key. Consider the two tables QUESTIONCHOICE and QUESTIONANSWER below. The records in table QUESTIONANSWER is a subset of QUESTIONCHOICE and has a foreign key constraint to make sure QUESTIONANSWER table is always subset of QUESTIONCHOICE table.
Now i am trying to update a record in QUESTIONCHOICE table using the query below and the update is stopped by foreign key constraint.
update “USER”.”QUESTIONCHOICE” set “CHOICE”=’GET1′ where “QID”=10001 and “CHOICE”=’GET’;
UPDATE on table ‘QUESTIONCHOICE’ caused a violation of foreign key constraint ‘QUESTIONANSWER_FK’ for key (10001,GET). The statement has been rolled back.
How am i supposed to update the records in the table QUESTIONCHOICE?
i can delete the record in QUESTIONANSWER table update my record in QUESTIONCHOICE table and then insert the record in QUESTIONANSWER. but it doesnt sounds good to me.
Also can i create QUESTIONANSWER as a VIEW instead a TABLE, If so how will i make a subset of QUESTIONCHOICE table?
please suggest.
Thanks,
-Vijay Selvaraj
----------
CREATE TABLE USER.QUESTIONCHOICE(
QID INT NOT NULL,
Choice VARCHAR(100) NOT NULL,
CONSTRAINT QUESTIONCHOICE_PK PRIMARY KEY (QID, Choice),
CONSTRAINT QUESTIONCHOICE_FK FOREIGN KEY (QID)
REFERENCES user.questionbank (QuestionID)
);
----------
create table USER.QUESTIONANSWER(
QID INT NOT NULL,
Answer VARCHAR(100) NOT NULL,
CONSTRAINT QUESTIONANSWER_PK PRIMARY KEY (QID, Answer),
CONSTRAINT QUESTIONANSWER_FK FOREIGN KEY (QID, Answer)
REFERENCES USER.QUESTIONCHOICE (QID, Choice)
);
Why dont you add a additional column to the QUESTIONCHOICE table to mark a row as the answer row, this way, the view also can be created by applying a constraint on the answer column.