例如 T表有两个字段A,B
如果A的一个值是“1”,则“1”不能再次出现A,B字段中,如何保证?

解决方案 »

  1.   

    alter table A_DRB2
      add constraint CHECK_TEST
      check (A!=B);
      

  2.   

    alter table T
      add constraint CHECK_TEST
      check (A!=B);
      

  3.   

    不是在一条记录中A<>B,
    而是A中的某个值在B中都没有,B中的某个值在A中都没有
      

  4.   

    增加一个约束:
     alter table t add constraint ck_a_b check(a<>b);
      

  5.   

    再增加一个约束:
    alter table t add constraint uk_t_a unique(a);
      

  6.   

    A,B是分别建有唯一索引,问题是要保证A与B的唯一性
    A    B
    1    2
    3    4
    5    6
    这时1,2,3,4,5,6都不能出现在A或B字段
      

  7.   

    我试过触发器
    UPDATE时,不能执行SELECT本表的操作,
    也就是 SELECT COUNT(*) INTO NUM FROM T WHERE B=NEW.A; 不能执行,因为本表正处于修改状态
      

  8.   

    不知有没有谁写过在UPDATE时,SELECT本表的触发器
      

  9.   

    建一个触发器:
    create or replace trigger tr_a before insert or update on table_a for each row
    declare
    num number;
     begin
       if inserting then
         SELECT COUNT(*) INTO NUM FROM T WHERE B=NEW.A; 
         if num >0 then 
           return;
         end if; 
       end if;
       if updating then
         SELECT COUNT(*) INTO NUM FROM T WHERE B=NEW.A; 
         if num >0 then 
           return;
         end if;
       end if;
    end;
      

  10.   

    好象还要判断SELECT COUNT(*) INTO NUM FROM T WHERE A=NEW.B;
      

  11.   

    create or replace trigger tr_F_ck
     before insert or update on f
     for each row
     declare
     num number;
     begin
     select count(*) into num from f
     where a=:new.a or b=:new.a 
        or a=:new.b or b=:new.b 
        or :new.a=:new.b;
     if num > 0 then
     dbms_output.put_line('abcd');
     raise_application_error(-20901, 'multiuse', false);
     return;
     end if;
     end;
    将就用。楼主可能还是考虑一下表的设计
      

  12.   

    SQL> desc f;
     Name                                      Null?    Type
     ----------------------------------------- -------- ----------------------------
     A                                                  NUMBER
     B                                                  NUMBER
      

  13.   

    呵呵
    还会有这样的需求,bt啊
    为什么不把A,B字段合并为一个字段呢,实现唯一不就容易了。
    如果实在有必要还可以增加一个对合并后的值的解析表不就都ok了
      

  14.   

    你可以建一个表t_test,可以只有一个字段col,这个字段是主键
    然后写触发器
    create or replace trigger tr_a before insert or update on table_a for each row
    declare
    num number;
     begin
       if inserting then
         insert into t_test(col) values(NEW.A); 
         insert into t_test(col) values(NEW.B); 
         
       end if;
       if updating then
         delete from t_test where col = old.a;
         delete from t_test where col = old.b;
         insert into t_test(col) values(NEW.A); 
         insert into t_test(col) values(NEW.B); 
       end if;
    end;