wfkfytmy

  Oracle里有unique约束,意思是该字段唯一。

  但如果是两个字段呢?

  比如说一个会员等级表

 

        ID NAME                      POINT   DISCOUNT PRIVILEGE                             MID
---------- -------------------- ---------- ---------- ------------------------------ ----------
      1019 普通会员                      0         101001
      1015 青铜会员                    500        9.8 0                                    1001
      1016 白银会员                   1200        9.5 0                                    1001
      1017 黄金会员                   2100        9.11001

 

mid代表商家的编号,不唯一,point代表消费额或者积分要求,也不唯一。

现在需要实现mid = 1001的商家已经有一个point = 500 的值后,无法插入 mid = 1001 且point = 500。

关于这个我只想到触发器,插入前进行查询,如果存在,则抛出异常。

create or replace trigger level_tri
before
insert on clothingsales_level
for each row
declare
myerr exception; --定义一个异常
num varchar2(20);--定义一个变量,用来记录表是否已经存在相同的数据
begin
    select count(*) into num from clothingsales_level where mid = :new.mid and point =:new.point;--先对该表进行查询,前统计数据行数将其into到num中。
    if num>=1 then
        raise myerr;--声明异常存在
    end if;
    exception
        when myerr then
        raise_application_error('-20002', '该积分等级已存在');--当异常存在时抛出异常。
end;
/

 

分类:

技术点:

相关文章: