程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了插入触发器前的行级大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决插入触发器前的行级?

开发过程中遇到插入触发器前的行级的问题如何解决?下面主要结合日常开发的经验,给出你关于插入触发器前的行级的解决方法建议,希望对你解决插入触发器前的行级有所启发或帮助;

我正在为 product 表创建行级触发器,该触发器将在对产品执行插入操作之前触发。如果 product_type_master 表中的类型可用,此触发器将在产品表中插入新记录。

sql> SELEct * from Product_master;

        ID name            PRICE TYPE
---------- ---------- ---------- --------------------
        1 keyboard         3077 ip
        2 monitor          9847 op
        3 usb               807 sto

 sql> SELEct * FROM type_master;

        NO TYPE
---------- --------------------
        1 in
        2 op
        3 sto

所以条件是如果新的插入类型等于 type_master 表中给定的类型,那么只应该插入它,否则不插入。

我创建了一个:

create or replace trigger ins_trigger
   2  before insert on product_master for each row
   3  begin
   4      if(:new.type == type.type_master)then
   5          insert into product_master values('ID',name,'price',typE);
   6      end if;
   7  end;
   8  /

   Warning: trigger created with compilation errors.

解决方法

您无法通过 type 访问表 type_master 中的列 type.type_master。你必须选择它。

如果你对主表创建外键约束,你根本不需要触发器:

alter table product_master 添加约束 product_master_type_master_fk 外键(类型)参 type_master(typE);

而且您不必在触发器中执行插入语句。触发器由插入语句触发。

,

我会像这样创建表格:

create table product_types
( type_id       Integer generated always as identity consTraint product_type_pk priMary key,type_name     varchar2(30) NOT NULL consTraint type_name_uk unique );

create table products
( prod_id       Integer generated always as identity consTraint product_pk priMary key,prod_name     varchar2(30) NOT NULL consTraint product_name_uk unique,prod_price    number(10,2),prod_type_id  consTraint product_type_fk references product_types(type_id) );

以免输入无效的产品类型:

insert into products (prod_name,prod_price,prod_type_id)
values ('Cheese',123.45,1);

ERROR at line 1:
ORA-02291: integrity consTraint (WILLIAm.PRODUCT_TYPE_FK) violated - parent key not found

然而,只是为了演示触发器语法(因为不需要这个触发器),如果我必须写一个:

create or replace trigger really_should_be_a_foreign_key
    before insert or update on products
    for each row when (new.prod_type_id is not null)
declare
    l_parent_type_count number;
begin
    SELEct count(*) into l_parent_type_count
    from   product_types t
    where  t.type_id = :new.prod_type_id;
    
    if l_parent_type_count = 0 then
        raise_application_error
        ( -20000,'Product type "'||:new.prod_type_id||'" is not defined.' ); 
    end if;
end really_should_be_a_foreign_key;

然后尝试输入无效的产品类型失败,并显示来自触发器的错误消息

ERROR at line 1:
ORA-20000: Product type "1" is not defined.
ORA-06512: at "WILLIAm.REALLY_SHOULD_BE_A_FOREIGN_KEY",line 9
ORA-04088: error during execution of trigger 'WILLIAm.REALLY_SHOULD_BE_A_FOREIGN_KEY'
,

其他人已经解决了您的方法,但我没有看到任何人解决

警告:触发器创建时有编译错误。

收到此信息后,应立即跟进“显示错误”

当你这样做时,你会发现'=='是无效的。检查您的 pl/sqL Language Fundamentals,2.7.5.2.1 算术比较

大佬总结

以上是大佬教程为你收集整理的插入触发器前的行级全部内容,希望文章能够帮你解决插入触发器前的行级所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: