ALTER TABLE CHECK CONSTRAINT

时间:2013-05-27 00:51:53

标签: oracle constraints alter

在命令中从第35行开始出错:

ALTER TABLE lee_person
  ADD CONSTRAINT check_person_type 
  CHECK(CASE WHEN UPPER(person_type) = 'EMPLOYEE'
             THEN employment_date IS NOT NULL 
             AND  manager_id IS NOT NULL
             WHEN UPPER(person_type) = 'CLIENT'
             THEN employment_date IS NULL 
             AND  manager_id IS NULL                
        END)

错误报告:

SQL Error: ORA-00905: missing keyword
00905. 00000 -  "missing keyword"
*Cause:    
*Action:

1 个答案:

答案 0 :(得分:4)

您无法使用CASE,就像您尝试做的那样(CASE WHEN condition-A THEN condition-B AND condition-C)。

看起来您想强制实施以下内容:

  1. 如果person_typeemployee(不区分大小写),则employment_datemanager_id必须为NOT NULL

  2. 如果person_typeclient(不区分大小写),则employment_datemanager_id必须为NULL

  3. 如果是这种情况,请尝试改为:

    ALTER TABLE lee_person
      ADD CONSTRAINT check_person_type
      CHECK ( (UPPER(person_type) = 'EMPLOYEE' AND
               employment_date IS NOT NULL AND
               manager_id IS NOT NULL)
           OR (UPPER(person_type) = 'CLIENT' AND
               employment_date IS NULL AND
               manager_id IS NULL)
      )
    

    我已经对此进行了测试,但确实有效。

    请注意,它允许的唯一person_type值为employeeclient(不区分大小写)。如果您想允许其他类型(包含或不包含employment_datemanager_id限制),您需要处理其他OR条件。