2012-11-06 56 views
1

我嘗試添加該SQL檢查中SQL檢查約束不能引用其他列

ALTER TABLE School 
add Role check_role 
    CHECK (check_role IN ('Teaching Assistant', 'Lecturer', 'Professor')); 

我得到以下

ERROR at line 3: 
ORA-02438: Column check constraint cannot reference other columns 

SQL> desc School; 
Name          Null? Type 
----------------------------------------- -------- ---------------------------- 

STAFFNUM         NOT NULL VARCHAR2(12) 
NAME          NOT NULL VARCHAR2(50) 
ADDRESS         NOT NULL VARCHAR2(300) 
DOB            DATE 

錯誤我想增加一列電話的作用,同檢查約束check_role

我正在使用Oracle SQL。

感謝您的幫助!

回答

5

到角色列添加到表

alter table School 
    add (Role varchar(10)); 

添加約束角色列。

alter table School 
    add constraint check_role 
    check (Role IN ('Teaching Assistant', 'Lecturer', 'Professor')); 

=========

ALTER TABLE School ADD COLUMN Role VARCHAR(50) 
CONSTRAINT check_role CHECK 
    (Role IN ('Teaching Assistant', 'Lecturer', 'Professor')); 
+0

它可以在一個'alter'語句來完成爲好。 –