创建Staff表时触发ORA-02438错误,请求技术支持
解决Oracle建表时ORA-02438错误
错误原因分析
出现ORA-02438: Column check constraint cannot reference other columns错误,是因为列级CHECK约束的写法不符合Oracle语法规范:
- 列级CHECK约束无需使用
value关键字指代当前列,直接通过列名定义取值规则即可 - 第8行的CHECK约束属于冗余定义,你已通过外键
branchfk约束branchNo必须存在于branch表中,无需重复验证
修正后的建表语句
Create Table Staff ( StaffNo char(4) not null, FName char(15) not null, LName char(20) not null, Position char(10) not null, Sex char(1) check(Sex in('f','m')), Salary numeric(8,2), branchNO char(4), Constraint StaffNopk primary key (StaffNo), constraint branchfk foreign key (branchNo) references branch (branchNo) );
修改细节
- 第6行:将
check(value in('f','m'))改为check(Sex in('f','m')),直接通过列名指定约束条件 - 删除第8行的
check(value in(select branchNo from branch)),利用外键约束完成branchNo的合法性校验
内容的提问来源于stack exchange,提问作者James Sha
相关产品推荐
相关产品推荐

