使用check约束,一大插入0或者空值,数据库回出错
SQL> create table prod(
  2  goodsid char(10),
  3  goodsname char(10),
  4  goodsqty char(10),
  5  goodsprice number(10,6)
  6  );表已创建。SQL> 
SQL> alter table prod
  2  add constraint check_goodsprice check (goodsprice is not null and goodsprice<>0);表已更改。SQL> 
SQL> insert into prod values('1','a','a',0);
insert into prod values('1','a','a',0)
*
ERROR 位于第 1 行:
ORA-02290: 违反检查约束条件 (SYSTEM.CHECK_GOODSPRICE)

解决方案 »

  1.   

    create trigger name_tri 
    before insert on goods
    for each row
    declare
    e_null exception;
    begin
    if :new.goodsprice is null then
     raise e_null;
    end if;
    select id.nextval into :new.goodsid from dual;
    exception
    when e_null then
     --出错处理(检查当没插入goodsid,唯范唯一性);
    end;
    /
      

  2.   

    create trigger name_tri 
    before insert on goods
    for each row
    declare
    e_null exception;
    begin
    if :new.goodsprice is null and :new.goodsprice=0 then
     raise e_null;
    end if;
    select id.nextval into :new.goodsid from dual;
    exception
    when e_null then
     --出错处理(检查当没插入goodsid,违反唯一性);
    end;
    /
      

  3.   

    console进入时倒底有什么用户名和密码:sysman/oem_temp
    使用insert after 行级触发器,
      

  4.   

    --出错处理(检查当没插入goodsid,唯范唯一性);此处我只有回滚或报错。总之,不让他保存。另外,问题没有描述清楚,因为是我新增记录,然后由另外一个人在另外一个程序中打开此表中此记录,然后填入价格后保存,所以需要的触发器是更新的触发器,而不是插入。是不是可以改成。
    create trigger name_tri_update 
    before update on goods
           ~~~~~~ 
    for each row
    declare
    e_null exception;
    begin..........
      

  5.   

    你的意思是一个人填数量,另一个人填单价?
    如果是一人完成记录的话,最好用maohaisheng() 的方法。
      

  6.   

    create trigger name_tri_update
    before update on goods
    for each row
    declare
    b number;--判定是否数字型
    e_null exception;
    begin
    if :new.goodsprice is null and :new.goodsprice=0 then
     raise e_null;
    end if;
    b:=:new.goodsprice;
    exception
    when e_null then
     dbms_output.put_line('此处不能为空');
    when others then
     dbms_output.put_line('此处必需填数字');
    end;
    /
      

  7.   

    楼上说的有些不太正确,由于在触发器截获了用户自定义异常。
    这样即使是错误的数据,也会被提交。
    应该在异常处理部分,再次引发异常或回滚,修改如下:create trigger name_tri_update
    before update on goods
    for each row
    declare
    b number;--判定是否数字型
    e_null exception;
    begin
    if :new.goodsprice is null and :new.goodsprice=0 then
     raise e_null;
    end if;
    b:=:new.goodsprice;
    exception
    when e_null then
    begin
     dbms_output.put_line('此处不能为空');
     raise; --rollback;
    end;
    when others then
    begin
     dbms_output.put_line('此处必需填数字');
     raise; --rollback;
    end;end;
      

  8.   

    if :new.goodsprice is null and :new.goodsprice=0 then
     raise e_null;
    end if;要改成
    if :new.goodsprice is null OR :new.goodsprice=0 then
     raise e_null;
    end if;应该是或条件!
      

  9.   

    想了想,不可以用触发器实,不管触发器成功会否,语句还是执行,而且不会回滚。
    当单击按钮事件,就调用此过程,而p_goodsid参数用一个方法去捕捉它.
    create procedure name_pro(p_goodsid in varchar2,p_goodsprice in number)
    as
    e_null exception;
    begin
    if p_goodsprice is not null or lengthb(p_goodsprice)>0 then
    update goods set goodsprice=p_goodsprice where goodsid=p_goodsid;
    else
    raise e_null;
    end if;
    exception
    when e_null then
    dbms_output.put_line('单价不能为空');
    end;
    /
      

  10.   

    同意Lastdrop(空杯) 的观点。
      

  11.   

    這種情況完全可以不需要觸發器﹐在前端存檔的時候控制就可以了。 is null(變量) 或 len(trim(變量)) = 0