Oracle 10g 中的触发器

发布于 2024-11-26 17:19:41 字数 348 浏览 2 评论 0原文

我有一个关系,其中帐户、客户是两个与 cl_ac 关系相关的表。帐户具有 ac_id,客户端具有 c_id 作为主键,这两个相同的键在 cl_ac 表中作为外键。我在帐户(ac_id)和客户端(c_id)中有一个主键触发器,因此在这两个表中我只需要插入剩余的值,并且主键会为每个记录增加自身。现在我想要一个触发器,当我在帐户和客户端中插入值时,它会自动在“cl_ac”表中插入值。

或者另一个选项而不是触发器,不违反外键约束。

我尝试了很多选项,但无法在 cl_ac 表中一次插入 2 个值(c_id 和 ac_id)。请有人帮助我。如果我可以插入图像以便更好地理解,那就更好了,我希望,我已经用可以理解的语言提出了我的问题。

谢谢。

I've an relationship in which Account, Client are two tables are related with cl_ac relationship. Account has ac_id and client has c_id as primary keys and these same two keys are in cl_ac table as foreign keys. I have a trigger for primary keys in account (ac_id) and client (c_id) so that in both table i need to insert only remaining values and primary keys increment itself for each record. Now i want a trigger that inserts values in "cl_ac" table automatically when i insert values in account and client.

or else another option rather trigger that do not violate the foreign key constraint.

I tried many option but was unable to insert 2 values (c_id and ac_id) at a time in cl_ac table. Please somebody help me. It would have been better if i could insert an image for better understanding, i hope, i've put my question in words that is understandable .

Thank you.

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

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

发布评论

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

评论(3

烟雨扶苏 2024-12-03 17:19:41

也许您需要使用 RETURNING 来获取要插入的两个值,而不是触发器?

INSERT INTO account (ac_id,....) VALUES(seq_ac_id.NEXTVAL,....) RETURNING ac_id INTO return_account_id;
INSERT INTO client  (c_id,....) VALUES(seq_c_id.NEXTVAL,....) RETURNING c_id INTO return_client_id;
INSERT INTO relationship (ac_id,c_id) values(return_account_id, return_client_id);
COMMIT;

perhaps you need to use RETURNING to get both of the values you want to insert, rather than a trigger ?

INSERT INTO account (ac_id,....) VALUES(seq_ac_id.NEXTVAL,....) RETURNING ac_id INTO return_account_id;
INSERT INTO client  (c_id,....) VALUES(seq_c_id.NEXTVAL,....) RETURNING c_id INTO return_client_id;
INSERT INTO relationship (ac_id,c_id) values(return_account_id, return_client_id);
COMMIT;
泪眸﹌ 2024-12-03 17:19:41

触发器如何知道哪个帐户与哪个客户关联?

您最好从创建帐户/客户记录的代码中插入关系记录。那是您获得必要信息的地方。

How would the trigger know which account to associate with which client?

You are better off inserting the relationship record from the code that creates the account/client records. That's where you have the necessary information.

尘世孤行 2024-12-03 17:19:41

您可能想将其包装在一个过程中,有点像下面的伪代码

CREATE OR REPLACE PACKAGE BODY cli_acc IS
  TYPE tab_cli IS TABLE OF client%ROWTYPE;
  TYPE tab_acc IS TABLE OF account%ROWTYPE;
  PROCEDURE ins_cli_acc (i_tab_cli tab_cli, i_tab_acc tab_acc) IS
  BEGIN
    --insert all of i_tab_cli into CLIENT
    --insert all of i_tab_acc into ACCOUNT
    FOR i IN i_tab_cli.FIRST .. i_tab_cli.LAST LOOP
      FOR j IN i_tab_acc.FIRST .. i_tab_acc.LAST LOOP
         INSERT INTO cli_acc (cli_id, acc_id) 
         VALUES (i_tab_cli(i).cli_id, i_tab_acc(j).acc_id);
      END LOOP;
    END LOOP;
  END ins_cli_acc;
END cli_acc;

You might want to wrap the thing in a procedure, sort of like the pseduo code below

CREATE OR REPLACE PACKAGE BODY cli_acc IS
  TYPE tab_cli IS TABLE OF client%ROWTYPE;
  TYPE tab_acc IS TABLE OF account%ROWTYPE;
  PROCEDURE ins_cli_acc (i_tab_cli tab_cli, i_tab_acc tab_acc) IS
  BEGIN
    --insert all of i_tab_cli into CLIENT
    --insert all of i_tab_acc into ACCOUNT
    FOR i IN i_tab_cli.FIRST .. i_tab_cli.LAST LOOP
      FOR j IN i_tab_acc.FIRST .. i_tab_acc.LAST LOOP
         INSERT INTO cli_acc (cli_id, acc_id) 
         VALUES (i_tab_cli(i).cli_id, i_tab_acc(j).acc_id);
      END LOOP;
    END LOOP;
  END ins_cli_acc;
END cli_acc;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文