如何禁用Oracle中涉及表的所有触发器?

发布于 2024-10-29 06:49:42 字数 373 浏览 1 评论 0原文

在 Postgresql 中,如果我执行 ALTER TABLE mytable DISBLE TRIGGERS ALL ,则与此表相关的所有触发器和约束都会挂起。

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

我如何在 Oracle 中做同样的事情?我的印象是Oracle中的ALTER TABLE mytable DISBLE ALL TRIGGERS将暂停属于mytable的所有触发器和约束,但不会暂停那些涉及mytable但属于其他表(尤其是外键)的触发器和约束。

我是对的吗?如何才能达到与 Oracle 中的 Postgresql 相同的结果?

In Postgresql, if I do ALTER TABLE mytable DISBLE TRIGGERS ALL, all triggers and constraints regarding this table are suspended.

Especially, foreign keys from other tables to mytable are suspended, and I can delete from mytable without problem. I have the risk to break database consistency, but I know what I'm doing, and I must have superuser privileges.

How do I do the same in Oracle ? I am under the impression that ALTER TABLE mytable DISBLE ALL TRIGGERS in Oracle will suspend all trigger and constraints belonging to mytable, but not those that concerns mytable but belong to other tables (especially foreign keys).

Am I right and what would be the way to achieve the same result as in Postgresql in Oracle ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南烟 2024-11-05 06:49:42

该语法确实禁用了 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>

但是它不会对外键或任何其他约束执行任何操作。这是因为 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>

您可能希望将这种东西包装在用户定义的函数中,该函数将 TABLE_NAME 作为范围。此外,您还需要一个类似的函数来重新启用约束。

The syntax does disable triggers in 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>

However it won't do anything for foreign keys, or indeed any other constraint. That's because Oracle doesn't use triggers to enforce such things. Okay, under the covers constraints and user-defined triggers may share certain low-level kernel code. But at the level we're talking they are two different things.

If you want to disable all foreign keys on a table I'm afraid you'll need to use something like this:

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>

This is the sort of thing you'll probably want to wrap in a user-defined function, which takes TABLE_NAME as a parameter. Also you'll need a similar function to re-enable the constraints.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文