2013-05-27 178 views
2

錯誤的命令開始,第35行:ALTER TABLE CHECK約束

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: 

回答

3

就像你正在試圖做的(CASE WHEN condition-A THEN condition-B AND condition-C)您不能使用CASE

看起來要執行以下操作:

  1. 如果person_typeemployee(不區分大小寫),employment_datemanager_id必須NOT NULL

  2. 如果person_typeclient(不區分大小寫),employment_datemanager_id必須NULL

如果是這樣的話,試一下這個:

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條件。