Sql Conditional Not Null Constraint

前端 未结 4 1166
臣服心动
臣服心动 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-01 04:59

    Edit: as mentioned in the other answers, a CHECK is the best method, not the trigger I originally suggested. Original text follows:


    As dbaseman suggests, triggers are the way to go (not so). Try something like this (untested):

    CREATE OR REPLACE TRIGGER test
      BEFORE UPDATE ON table1
    FOR EACH ROW
    WHEN (new.A = 'NEW' and new.B IS NOT NULL)
       RAISE_APPLICATION_ERROR (
         num=> -20001,
         msg=> 'B must be NULL for new rows (A = NEW)'
    );
    

提交回复
热议问题