我有以下表格。
create table Surveys (
survey_id int,
cid int,
num_submitted int,
sum_q1 int,
sum_q2 int,
sum_q3 int,
sum_q4 int,
constraint pk_survey primary key (survey_id),
constraint fk_courses foreign key (cid)
references Courses(cid)
);
create table Surveydata (
survey_id int,
sid int,
submit_time date,
q1 int,
q2 int,
q3 int,
q4 int,
q5_str varchar(250),
constraint pk_survey_data primary key (survey_id, sid),
constraint fk_sdata_surveys foreign key (survey_id)
references Surveys(survey_id),
constraint fk_sdata_students foreign key (sid)
references Students(sid)
);
触发器
CREATE OR REPLACE TRIGGER survey_data_trigger
AFTER INSERT OR UPDATE OR DELETE ON SURVEYDATA
FOR EACH ROW
BEGIN
IF INSERTING THEN
UPDATE Surveys SET
num_submitted = num_submitted + 1,
sum_q1 = sum_q1 + :new.q1,
sum_q2 = sum_q2 + :new.q2,
sum_q3 = sum_q3 + :new.q3,
sum_q4 = sum_q4 + :new.q4
WHERE
survey_id = :new.survey_id;
END IF;
IF UPDATING THEN
IF(:new.survey_id = :old.survey_id) THEN
UPDATE Surveys SET
sum_q1 = sum_q1 - :old.q1 + :new.q1,
sum_q2 = sum_q2 - :old.q2 + :new.q2,
sum_q3 = sum_q3 - :old.q3 + :new.q3,
sum_q4 = sum_q4 - :old.q4 + :new.q4
WHERE
survey_id = :new.survey_id;
ELSE
UPDATE Surveys SET
num_submitted = num_submitted + 1,
sum_q1 = sum_q1 + :new.q1,
sum_q2 = sum_q2 + :new.q2,
sum_q3 = sum_q3 + :new.q3,
sum_q4 = sum_q4 + :new.q4
WHERE
survey_id = :new.survey_id;
UPDATE Surveys SET
num_submitted = num_submitted - 1,
sum_q1 = sum_q1 - :old.q1,
sum_q2 = sum_q2 - :old.q2,
sum_q3 = sum_q3 - :old.q3,
sum_q4 = sum_q4 - :old.q4
WHERE
survey_id = :old.survey_id;
END IF;
END IF;
IF DELETING THEN
UPDATE Surveys SET
num_submitted = num_submitted - 1,
sum_q1 = sum_q1 - :old.q1,
sum_q2 = sum_q2 - :old.q2,
sum_q3 = sum_q3 - :old.q3,
sum_q4 = sum_q4 - :old.q4
WHERE
survey_id = :old.survey_id;
END IF;
END;
/
当我发出时,
INSERT INTO Surveydata VALUES(1, 1, SYSDATE, 5, 5, 5, 5, 'Great teacher!');
触发器不会触发并更新此Surveydata记录所指向的调查记录。我在这里做错了什么?
答案 0 :(得分:1)
你的触发器只有在它有效的情况下才会触发 - 也就是说,它是在没有错误的情况下创建的,并且从那时起就没有成功地停止它重新编译 - 并且已启用。您可以在user_objects
表中检查有效性:
select status
from user_objects
where object_type = 'TRIGGER' and object_name = 'SURVEY_DATA_TRIGGER';
如果这表示触发器是INVALID
,您可以看到它在user_errors
表中有哪些错误:
select text
from user_errors
where type = 'TRIGGER' and name = 'SURVEY_DATA_TRIGGER'
order by sequence;
...然后使用更正的代码重新创建它。但是从this trigger seems to work开始,它可能对您有效,因此被禁用,您可以查看:
select status
from user_triggers
where trigger_name = 'SURVEY_DATA_TRIGGER';
如果说是DISABLED
,则可以通过以下方式启用它:
alter trigger survey_data_trigger enable;
您的触发器创建代码不会将其创建为已禁用,因此如果它处于该状态,则可能是您有另一个明确禁用它的步骤。除非你有一个DDL触发器改变了所有新创建的触发器的状态,否则我想不出会自动禁用新触发器*的任何东西,但你可能知道这一点,你可以很容易地测试它。你似乎更有可能自己禁用它。
*好吧,有一个隐藏的初始化参数_system_trig_enabled
,这会使它被创建为禁用,但是这个集合似乎也不太可能。