You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

创建外键时同时创建索引的SQL命令是否正确?遇ORA-01735错误

Oracle添加外键时ORA-01735错误的解决方法

你的SQL语句写法不正确,导致触发ORA-01735错误,问题出在using index子句的用法上。

错误原因

在Oracle的ALTER TABLE ADD CONSTRAINT FOREIGN KEY语法中,using index子句不能直接内嵌CREATE INDEX语句。它只能用来指定已存在的索引,或者设置索引的存储属性(如表空间、存储参数等),无法通过这种方式直接创建新索引。

正确写法

方法1:先创建索引,再添加外键约束

如果需要自定义索引名称,先单独创建索引,再在添加外键时指定使用该索引:

-- 第一步:创建自定义名称的索引
create index i_test_prim_fk on test(prim_id) tablespace index01;

-- 第二步:添加外键约束并指定使用已创建的索引
alter table test
add constraint test_fk foreign key (prim_id)
references prim (prim_id)
using index i_test_prim_fk
on delete cascade;
/

方法2:让Oracle自动创建索引(指定存储属性)

如果不需要自定义索引名称,可以通过using index指定索引的存储参数,Oracle会自动生成与约束同名的索引:

alter table test
add constraint test_fk foreign key (prim_id)
references prim (prim_id)
using index tablespace index01
on delete cascade;
/

内容的提问来源于stack exchange,提问作者Dmytro

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.05 00:02:05