此新租借历史记录表的触发器,可防止从表中删除。
CREATE OR REPLACE TRIGGER RENTALHIS_DEL
BEFORE DELETE ON RENTALHISTORY
BEGIN
dbms_output.put_line( 'Records can not be deleted');
END;
DELETE FROM RENTALHISTORY WHERE RENTALID = 1;
-- It is deleting before it says it can not delete
1 rows deleted.
Records can not be deleted
答案 0 :(得分:7)
dbms_output.put_line( 'Records can not be deleted');
以上只是打印文本并触发成功完成,然后无论如何都会删除。你想做的是提出一个错误,以防止程序完成。
使用标准程序raise_application_error
停止程序并引发错误:
CREATE OR REPLACE TRIGGER RENTALHIS_DEL
BEFORE DELETE ON RENTALHISTORY
BEGIN
raise_application_error(-20001,'Records can not be deleted');
END;
/
答案 1 :(得分:4)
您想引发错误,而不是打印消息。无论如何都会发生删除:
CREATE OR REPLACE TRIGGER RENTALHIS_DEL
BEFORE DELETE ON RENTALHISTORY
BEGIN
RAISE_APPLICATION_ERROR (-20000, 'Deletion not supported on this table');
END;
或者,您可以使用instead of
触发器来阻止删除:
CREATE OR REPLACE TRIGGER RENTALHIS_DEL
INSTEAD OF DELETE ON RENTALHISTORY
BEGIN
dbms_output.put_line( 'Records cannot be deleted');
END;
答案 2 :(得分:0)
之后,您可以使用commit after your delete statement and rollback
来使用Pragma
进入上一个阶段。
CREATE OR REPLACE TRIGGER
RENTALHIS_DEL
AFTER DELETE ON RENTALHISTORY
DECLARE
PRAGMA
AUTONOMOUS_TRANSACTION;
BEGIN
RAISE_APPLICATION_ERROR
(-20000, 'Deletion getting rolled
back');
ROLLBACK;
END;