Sql Conditional Not Null Constraint

前端 未结 4 1159
臣服心动
臣服心动 2021-02-01 04:20


I am curious to know is it possible to create a conditional not null constraint in sql? In otherwords is it possible to create a constraint such that a column B can be null

4条回答
  •  礼貌的吻别
    2021-02-01 04:55

    I think your first stated requirement is:

    IF ( B IS NULL ) THEN ( A = 'NEW' )
    

    Apply the implication rewrite rule:

    IF ( X ) THEN ( Y )   <=>   ( NOT ( X ) OR ( Y ) )
    

    In your case;

    ( NOT ( B IS NULL ) OR ( A = 'NEW' ) )
    

    Minor rewrite to take advantage of SQL syntax:

    ( B IS NOT NULL OR A = 'NEW' )
    

    Your second stated ("extend") requirement:

    IF ( A = 'NEW' ) THEN ( B IS NULL )
    

    Apply rewrite rule:

    ( NOT ( A = 'NEW' ) OR ( B IS NULL ) )
    

    Minor rewrite:

    ( A <> 'NEW' OR B IS NULL )
    

提交回复
热议问题