在 create table 语句中内联创建外键索引的语法
Syntax for creating foreign key index inline in create table statement
我想创建一个 table bar
,其中包含一个由命名索引支持的命名外键约束。我想在 create table DDL 语句中使用内联定义来做到这一点。查看 Oracle 19 SQL Language Reference 时,Oracle 似乎应该支持执行此内联操作。
执行以下语句时...
create table foo (
id number not null primary key
);
create table bar (
id number not null primary key,
nick varchar2(16) not null constraint foo_nick_ck unique using index,
foo_id number not null constraint foo_fk references foo using index
);
Oracle 将响应 [42000][907] ORA-00907: missing right parenthesis
并指向最后一行 using index
之前的位置。如果我删除 using index
它会起作用(但当然没有创建索引)。我保留了列 nick
作为创建内联支持索引的示例,但用于唯一约束而不是外键约束。
我知道一个解决方法是在单独的 DDL 语句中创建支持索引,但我非常希望它内联整洁。
I am aware that a workaround is to create the backing index in a separate DDL statement, but I would very much like to have it neat and tidy inline.
遗憾的是,不存在用于内联创建索引的语法。
USING INDEX 子句是语法糖:无论我们是否包含该子句 (*),Oracle 都会创建索引来强制执行主键和唯一约束。这是因为索引是Oracle 实现唯一约束所必需的。但是不需要索引来强制执行外键,它只是可取的。
(*) 除非 Oracle 可以使用的约束列上存在现有索引。
You can specify the using_index_clause only when enabling unique or
primary key constraints
You cannot specify this clause for a NOT NULL
, foreign key, or
check constraint.
我想创建一个 table bar
,其中包含一个由命名索引支持的命名外键约束。我想在 create table DDL 语句中使用内联定义来做到这一点。查看 Oracle 19 SQL Language Reference 时,Oracle 似乎应该支持执行此内联操作。
执行以下语句时...
create table foo (
id number not null primary key
);
create table bar (
id number not null primary key,
nick varchar2(16) not null constraint foo_nick_ck unique using index,
foo_id number not null constraint foo_fk references foo using index
);
Oracle 将响应 [42000][907] ORA-00907: missing right parenthesis
并指向最后一行 using index
之前的位置。如果我删除 using index
它会起作用(但当然没有创建索引)。我保留了列 nick
作为创建内联支持索引的示例,但用于唯一约束而不是外键约束。
我知道一个解决方法是在单独的 DDL 语句中创建支持索引,但我非常希望它内联整洁。
I am aware that a workaround is to create the backing index in a separate DDL statement, but I would very much like to have it neat and tidy inline.
遗憾的是,不存在用于内联创建索引的语法。
USING INDEX 子句是语法糖:无论我们是否包含该子句 (*),Oracle 都会创建索引来强制执行主键和唯一约束。这是因为索引是Oracle 实现唯一约束所必需的。但是不需要索引来强制执行外键,它只是可取的。
(*) 除非 Oracle 可以使用的约束列上存在现有索引。
You can specify the using_index_clause only when enabling unique or primary key constraints
You cannot specify this clause for a
NOT NULL
, foreign key, or check constraint.