假设有数据库表A,包括3个字段X,Y,Z。其中X为主键必填,但Y和Z能且只能填其一,不能都填或都不填。该如何建立这种数据库约束?

解决方案 »

  1.   

        貌似没见过这种约束 不过通过instead of触发器是可以做到的。  当你插入或者更新的时候 去判断另外一个是否有值  去用别的语句代替执行 
      

  2.   

    create table tmp
    (
    t_no varchar2(2),
    t_x varchar2(2),
    t_y varchar2(2),
    check( (t_x is null) and (t_y is not null)or(t_x is not null) and (t_y is null))
    )
      

  3.   

    少了2括号……^_^
    check( ((t_x is null) and (t_y is not null))or((t_x is not null) and (t_y is null)))        
      

  4.   

    不用括 , and比or优先。