oracle USING 索引子句
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23243479/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
USING index clause
提问by EvilPuma
I saw the following statement in a db patch:
我在数据库补丁中看到以下语句:
ALTER TABLE tablename ADD PRIMARY KEY (somepk_columnname) USING INDEX;
I wanted to look up what USING INDEX does here, but only got from google, that it lets me specify some storage specific stuff, etc.
我想看看 USING INDEX 在这里做了什么,但只是从谷歌那里得到的,它让我指定一些存储特定的东西,等等。
My question really is, what exactlyhappens here? Does the db use some default values here? Creates an index for the PK in the default tablespace? I thought that an index is created for every pk per default...
我的问题真的是,这里到底发生了什么?db 是否在这里使用了一些默认值?在默认表空间中为 PK 创建索引?我认为默认为每个 pk 创建一个索引...
采纳答案by Lajos Veres
http://docs.oracle.com/cd/B28359_01/server.111/b28286/clauses002.htm#SQLRF52209
http://docs.oracle.com/cd/B28359_01/server.111/b28286/clauses002.htm#SQLRF52209
Using Indexes to Enforce Constraints
When defining the state of a unique or primary key constraint, you can specify an index for Oracle to use to enforce the constraint, or you can instruct Oracle to create the index used to enforce the constraint.
using_index_clause You can specify the using_index_clause only when enabling unique or primary key constraints. You can specify the clauses of the using_index_clause in any order, but you can specify each clause only once.
If you specify schema.index, then Oracle attempts to enforce the constraint using the specified index. If Oracle cannot find the index or cannot use the index to enforce the constraint, then Oracle returns an error.
If you specify the create_index_statement, then Oracle attempts to create the index and use it to enforce the constraint. If Oracle cannot create the index or cannot use the index to enforce the constraint, then Oracle returns an error.
If you neither specify an existing index nor create a new index, then Oracle creates the index. In this case:
The index receives the same name as the constraint.
If table is partitioned, then you can specify a locally or globally partitioned index for the unique or primary key constraint.
使用索引来强制约束
在定义唯一键或主键约束的状态时,您可以为 Oracle 指定一个索引以用于强制约束,或者您可以指示 Oracle 创建用于强制约束的索引。
using_index_clause 只有在启用唯一键或主键约束时才能指定 using_index_clause。您可以按任意顺序指定 using_index_clause 的子句,但每个子句只能指定一次。
如果您指定 schema.index,则 Oracle 将尝试使用指定的索引强制执行约束。如果 Oracle 找不到索引或无法使用索引来强制执行约束,则 Oracle 将返回错误。
如果您指定 create_index_statement,则 Oracle 将尝试创建索引并使用它来强制执行约束。如果 Oracle 无法创建索引或无法使用索引来强制执行约束,则 Oracle 将返回错误。
如果您既不指定现有索引也不创建新索引,则 Oracle 将创建索引。在这种情况下:
索引接收与约束相同的名称。
如果表已分区,则可以为唯一键或主键约束指定本地或全局分区索引。
回答by Clay Mitchell
The response above was very helpful but I also wanted to use a specific tablespace for my unique key. This worked for me:
上面的回复非常有帮助,但我也想为我的唯一键使用特定的表空间。这对我有用:
alter table tablename add constraint uk_name unique (col1, col2) using index tablespace tablespace_name;