如何禁用与Oracle中的表有关的所有触发器?

alc*_*lci 3 oracle postgresql

在Postgresql中,如果我这样做ALTER TABLE mytable DISBLE TRIGGERS ALL,那么关于该表的所有触发器和约束都将被暂停.

特别是,从其他表到mytable的外键被暂停,我可以毫无问题地从mytable中删除.我有破坏数据库一致性的风险,但我知道我在做什么,而且我必须拥有超级用户权限.

我如何在Oracle中执行相同的操作?我的印象是,ALTER TABLE mytable DISBLE ALL TRIGGERS在Oracle中将暂停属于mytable的所有触发器和约束,但不包括那些涉及mytable但属于其他表(尤其是外键)的触发器和约束.

我是对的,在Oracle中的Postgresql中获得相同结果的方法是什么?

APC*_*APC 5

该语法会在Oracle中禁用触发器:

SQL> select trigger_name, status from user_triggers
  2  where table_name='TEST'
  3  /

TRIGGER_NAME                   STATUS
------------------------------ --------
TEST_TRIGGER                   ENABLED

SQL> ALTER TABLE test DISABLE ALL TRIGGERS
  2  /

Table altered.

SQL> select trigger_name, status from user_triggers
  2  where table_name='TEST'
  3  /

TRIGGER_NAME                   STATUS
------------------------------ --------
TEST_TRIGGER                   DISABLED

SQL>
Run Code Online (Sandbox Code Playgroud)

但是它不会对外键或任何其他约束做任何事情.这是因为Oracle不使用触发器来强制执行此类操作.好的,在封面下,约束和用户定义的触发器可以共享某些低级内核代码.但在我们所说的水平上,它们是两个不同的东西.

如果你想禁用表上的所有外键,我担心你需要使用这样的东西:

SQL> select constraint_name, status from user_constraints
  2  where table_name = 'EMP'
  3  and constraint_type = 'R'
  4  /

CONSTRAINT_NAME                STATUS
------------------------------ --------
FK_DEPTNO                      ENABLED


SQL> begin
  2      for r in ( select constraint_name, status from user_constraints
  3                 where table_name = 'EMP'
  4                 and constraint_type = 'R' )
  5      loop
  6          execute immediate 'alter table emp disable constraint '||r.constraint_name;
  7      end loop;
  8* end;
  9  /

PL/SQL procedure successfully completed.

SQL> select constraint_name, status from user_constraints
  2  where table_name = 'EMP'
  3  and constraint_type = 'R'
  4  /

CONSTRAINT_NAME                STATUS
------------------------------ --------
FK_DEPTNO                      DISABLED

SQL>
Run Code Online (Sandbox Code Playgroud)

这是你可能想要在用户定义的函数中包装的东西,它将TABLE_NAME作为参数.此外,您还需要一个类似的功能来重新启用约束.