为什么这个检查约束不起作用?3

时间:2014-12-04 13:17:46

标签: sql oracle

我有这个表格问题(id_qquestionpictureanswer),我需要制定一个不允许我插入的约束如果问题中包含问题,请在表格中添加一个新问题'指标化验'而且图片为空。

我试过这种方式:

ALTER TABLE question ADD
CONSTRAINT CK_question check (((question like '%indicator semnification%') and 
                               (picture in(null))) or 
                               (question not like '%indicator semnification%'));

但它不起作用。

2 个答案:

答案 0 :(得分:2)

这是你的约束:

ALTER TABLE question
    ADD CONSTRAINT CK_question check (((question like '%indicator semnification%') and
                                      (picture in(null))) or
                                      (question not like '%indicator semnification%')
                                     )

问题可能是picture in (null)。这永远不会回归真实。试试这个:

ALTER TABLE question
    ADD CONSTRAINT CK_question check (((question like '%indicator semnification%') and
                                      (picture is null)) or
                                      (question not like '%indicator semnification%')
                                     )

此外,如果您使用的是MySQL,则可以表达约束,但它们并未实现。

编辑:

以上修复了您的版本中的逻辑。我认为你真正想要的逻辑是:

ALTER TABLE question
    ADD CONSTRAINT CK_question check ((question not like '%indicator semnification%') or 
                                      (picture is not null)
                                     ) ;

或等效地(当question不为空时):

ALTER TABLE question
    ADD CONSTRAINT CK_question check (not ((question like '%indicator semnification%') and 
                                           (picture is null))
                                     ) ;

答案 1 :(得分:0)

尝试使用()条件集

修改您的约束定义,如下所示
ALTER TABLE question ADD
CONSTRAINT CK_question check (question LIKE '%indicator semnification%'  
                               OR
                               (picture IS NULL  
                                OR  
                               question NOT LIKE '%indicator semnification%'));