PL/SQL触发器触发问题
我有一个关于 PL/SQL 触发器何时触发的问题。
我编写了以下触发器
CREATE OR REPLACE TRIGGER gradeInputCheck
BEFORE INSERT ON GRADE
FOR EACH ROW
DECLARE
newGrade GRADE.NUMERIC_GRADE%TYPE := :NEW.NUMERIC_GRADE;
grade_too_low EXCEPTION;
grade_too_high EXCEPTION;
BEGIN
DBMS_OUTPUT.PUT_LINE(newGrade);
IF (newGrade < 0) THEN
RAISE grade_too_low;
ELSIF (newGrade > 100) THEN
RAISE grade_too_high;
END IF;
EXCEPTION
WHEN grade_too_low THEN
DBMS_OUTPUT.PUT_LINE('Grades must be between 0 and 100');
WHEN grade_too_high THEN
DBMS_OUTPUT.PUT_LINE('Grades must be between 0 and 100');
END;
但是,当我运行像触发器这样的简单语句时,
UPDATE grade SET numeric_grade = -1;
触发器不会触发。 关于如何让扳机点火有什么要点吗?
谢谢!
I have a question in regards to when a PL/SQL trigger fires.
I've written the following trigger
CREATE OR REPLACE TRIGGER gradeInputCheck
BEFORE INSERT ON GRADE
FOR EACH ROW
DECLARE
newGrade GRADE.NUMERIC_GRADE%TYPE := :NEW.NUMERIC_GRADE;
grade_too_low EXCEPTION;
grade_too_high EXCEPTION;
BEGIN
DBMS_OUTPUT.PUT_LINE(newGrade);
IF (newGrade < 0) THEN
RAISE grade_too_low;
ELSIF (newGrade > 100) THEN
RAISE grade_too_high;
END IF;
EXCEPTION
WHEN grade_too_low THEN
DBMS_OUTPUT.PUT_LINE('Grades must be between 0 and 100');
WHEN grade_too_high THEN
DBMS_OUTPUT.PUT_LINE('Grades must be between 0 and 100');
END;
However, when I run a simple statement like
UPDATE grade SET numeric_grade = -1;
The trigger doesn't fire.
Any points on how I can make the trigger fire?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的触发器是插入触发器。插入触发器不会因更新语句而触发。你应该使用这样的东西:
Your trigger is an insert trigger. Insert triggers do not fire for update statements. You should use something like this instead:
另外:
要解决这两个问题,您可以:
然而,对于像这样相对简单的约束,使用 CHECK 约束而不是触发器有很好的理由,例如性能、正确性、可维护性和“声明性”。
Also:
To address both of these concerns, you could:
However, for relatively simple constraints like these, there are good arguments for using a CHECK constraint instead of a trigger, like performance, correctness, maintainability, and "declarativeness".