SQL Check Constraint无法引用其他列

时间:2012-11-06 04:01:51

标签: sql oracle

我尝试在

中添加此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

添加列调用Role

我正在使用Oracle SQL。

感谢您的帮助!

1 个答案:

答案 0 :(得分:5)

将角色列添加到表

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

向Role列添加约束。

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'));