摆脱插入触发器

发布于 2024-10-16 21:05:53 字数 49 浏览 2 评论 0原文

尝试探索使用插入触发器的替代解决方案。比如基于 API 的方法以及不同方法的优缺点。

Trying to explore solutions alternative to using insert triggers. Like API based ones and pros and cons with different approaches.

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

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

发布评论

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

评论(3

我爱人 2024-10-23 21:05:53

在 API 方法中,您将创建一个过程来执行这两种操作 - 类似于:

package body emp_api is

    procedure insert_emp (...) is
    begin
        insert into emp (...) values (...);
        -- Insert that was previously in trigger
        insert into other_table (...) values (...);
    end;
end;

然后,您通过授予应用程序对 api 包的 EXECUTE 访问权限,但不授予其 INSERT/UPDATE/DELETE 访问权限来强制应用程序使用 API。桌子。

In an API approach you would create a procedure to perform both operations - something like:

package body emp_api is

    procedure insert_emp (...) is
    begin
        insert into emp (...) values (...);
        -- Insert that was previously in trigger
        insert into other_table (...) values (...);
    end;
end;

Then you force applications to use the API by giving them EXECUTE access to the api package but no INSERT/UPDATE/DELETE access to the tables.

七度光 2024-10-23 21:05:53

如果您想保证在向 tableA 插入数据时会将记录插入到 tableB 中,则保留触发器。您可以禁用批量加载到表 A 的功能,并且可以保证在此期间只有唯一的进程加载到该表中。

一旦删除触发器,就无法保证插入到 tableB 中。您唯一的希望是所有可能插入表 A 的程序(您真的了解所有这些吗?) 坚持二次插入表 B。这是“通过公司策略实现数据完整性”,而不是通过 Oracle 强制执行的数据完整性。

我认为这种方法取决于您对 tableB 中数据状态的关心程度。

我不会走表 api (TAPI) 的路线,它现在强制通过一些处理逻辑的 pl/sql api 进行任何/所有操作。根据我的经验,这些几乎总是缓慢且有问题的。

If you want to guarantee that you'll have a record inserted into tableB when something inserts into tableA, then keep the trigger. You can disable if bulk loading into tableA and can guarantee you'll have the only process loading into that table during that time.

As soon as you remove the trigger, you have NO guarantees about inserts into tableB. Your only hope is that any and all programs that may insert into tableA (do you really know all of these?) adhere to the secondary insert into tableB. This is "data integrity via company policy", not data integrity enforced via Oracle.

This approach depends on how much you care about the state of the data in tableB I suppose.

I would NOT go the route of table apis (TAPIs), which now force any/all operations through some pl/sql api that handles the logic. These almost always tend to be slow and buggy in my experience.

冰葑 2024-10-23 21:05:53

在 DDL 中,您可以使用 ALTER TRIGGER 或 ALTER TABLE 禁用触发器。

ALTER TRIGGER triggername DISABLE;  -- disable a single trigger

ALTER TABLE tablename DISABLE ALL TRIGGERS; -- disable all triggers on a table

要在运行时执行此操作,您必须使用动态 SQL,并且运行过程的模式必须拥有该表或具有必要的权限。

EXECUTE IMMEDIATE 'ALTER TRIGGER tablename DISABLE ALL TRIGGERS';

有关启用/禁用触发器的详细信息,请参阅 http:// /download.oracle.com/docs/cd/B28359_01/server.111/b28310/general004.htm

In DDL You can disable a trigger with ALTER TRIGGER or ALTER TABLE.

ALTER TRIGGER triggername DISABLE;  -- disable a single trigger

ALTER TABLE tablename DISABLE ALL TRIGGERS; -- disable all triggers on a table

To do this at runtime, you would have to use dynamic SQL and the schema in which the procedure is running must own the table or otherwise have the necessary privileges.

EXECUTE IMMEDIATE 'ALTER TRIGGER tablename DISABLE ALL TRIGGERS';

For more info on enabling/disabling triggers, see http://download.oracle.com/docs/cd/B28359_01/server.111/b28310/general004.htm

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