如何使用触发器保护表数据免受意外删除命令的影响

时间:2018-01-03 17:07:16

标签: database oracle12c

我想创建一个触发器,它将确保除非我们不禁用触发器,否则不能从表中删除任何记录。   请把宝贵的时间用于解决这个问题。

1 个答案:

答案 0 :(得分:0)

您可以使用如下。只要在表上触发delete语句,此trigger就会引发错误。

SQL> CREATE OR REPLACE TRIGGER DONT_DELETE
  2       BEFORE DELETE
  3       ON TAB1
  4       FOR EACH ROW
  5  BEGIN
  6       RAISE_APPLICATION_ERROR (-20000,'No delete allowed');
  7  END;
  8  /

Trigger created.

SQL> select * from tab1;

       COL
----------
         1
         2
         3

SQL> delete from tab1 where col = 1;
delete from tab1 where col = 1
            *
ERROR at line 1:
ORA-20000: No delete allowed
ORA-06512: at "SCOTT.DONT_DELETE", line 2
ORA-04088: error during execution of trigger 'SCOTT.DONT_DELETE'