[英]What's wrong with this PL/SQL Trigger?
I have this table, and I want to create a trigger on Magazine
, that verifies "after insert" if the name of the Magazine
inserted is either Vogue or People .我有这张表,我想在
Magazine
上创建一个触发器,如果插入的Magazine
的名称是Vogue或People ,它会验证“插入后”。
If it's not one of them, it gets deleted.如果它不是其中之一,它将被删除。
Table:桌子:
My trigger:我的触发器:
CREATE OR REPLACE TRIGGER TMag
AFTER INSERT ON Magazine
FOR EACH ROW
DECLARE
e EXCEPTION;
BEGIN
IF :new.mag_nom != 'Vogue' or :new.mag_nom != 'People' THEN
DELETE Magazine WHERE ISBN = :new.ISBN;
RAISE e;
END IF;
EXCEPTION
WHEN e THEN
DBMS_OUTPUT.PUT_LINE('nom mag incorrecte');
END;
But the problem is my teacher told me:但问题是我的老师告诉我:
This is not suitable for every situation
这并不适合所有情况
I don't know what that means, can you please help me improve this trigger?我不知道那是什么意思,你能帮我改进一下这个触发器吗?
It seemed correct to me, what did I do wrong?这对我来说似乎是正确的,我做错了什么?
You don't need to use a DML, convert the trigger into this您不需要使用 DML,将触发器转换为
CREATE OR REPLACE TRIGGER TMag
AFTER INSERT ON Magazine
FOR EACH ROW
BEGIN
IF :new.mag_nom NOT IN ('Vogue','People') THEN
RAISE_APPLICATION_ERROR(-20202,'nom mag incorrecte !');
END IF;
END;
/
and you would get table is mutating error in this case due to using the table, on which the trigger is created, within the trigger's body.在这种情况下,由于在触发器的主体内使用创建触发器的表,您会得到 table is mutating 错误。
Moreover it would be far better to add a check constraint than creating a trigger such as此外,添加检查约束比创建触发器要好得多,例如
ALTER TABLE abc
ADD CONSTRAINT cc_mag_nom
CHECK (mag_nom IN ('Vogue','People'));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.