create table test
(
  name varchar2(12),
  idType varchar2(20),
  idDesc varchar2(100)
)我有一张这样的表,如果我想通过check约束来实现如果idType='KK',idDesc就不能为空,这个约束怎么写,
很简单,也很急,谢谢!

解决方案 »

  1.   

    这个需要使用触发器吧。
    单纯的check约束我也不知道怎么实现。
      

  2.   

    create table test2(
      name VARCHAR2(12),
      idType VARCHAR2(20),
      idDesc VARCHAR2(100),
      CONSTRAINT test_ck_2 CHECK (
      ((idType = 'KK') AND (idDesc IS NOT null))
      OR idType != 'KK'
      )
    );
    --insert valid data
    insert into test2 values ('Tracy','KK','sdf');
    1 rows inserted--insert invalid data
    insert into test2 values ('Tracy','KK','');Error starting at line 1 in command:
    insert into test2 values ('Tracy','KK','')
    Error report:
    SQL Error: ORA-02290: check constraint (GBADMIN.TEST_CK_2) violated
    02290. 00000 -  "check constraint (%s.%s) violated"
    *Cause:    The values being inserted do not satisfy the named check
               
    *Action:   do not insert values that violate the constraint.