主键和唯一索引——SQL Developer生成的sql脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13235099/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 11:56:41  来源:igfitidea点击:

Primary Key and Unique Index -- sql scripts generated by SQL Developer

sqloracleoracle11g

提问by Dreamer

When export sql scripts by SQL Developer there are multiple options available, but either way there have to generate a UNIQUE INDEXon primary key like this

当 SQL Developer 导出 sql 脚本时,有多个选项可用,但无论哪种方式,都必须UNIQUE INDEX像这样生成主键

CREATE UNIQUE INDEX "SYS_C0018099" ON "TRANSACTION" ("ID") 

and add PRIMARY KEYto the same table and same column

并添加PRIMARY KEY到同一个表和同一列

ALTER TABLE "TRANSACTION" ADD PRIMARY KEY ("ID")

So the question is: does it looks like kind of redundancy? I thought creating a primary key on a column should by default create an unique index on that column too? So why the first command is necessary?

所以问题是:它看起来像某种冗余吗?我认为在列上创建主键默认情况下也应该在该列上创建唯一索引?那么为什么第一个命令是必要的呢?

And this may cause data redundancy?

这可能会导致数据冗余?

I am on Oracle 11g so please share any ideas about why it should look like above.

我使用的是 Oracle 11g,所以请分享有关为什么它看起来像上面那样的任何想法。

Thanks in advance.

提前致谢。

采纳答案by Florin Ghita

There is no redundancy - or only a little bit :)

没有冗余 - 或者只有一点点:)

The second command will use the index available if exists. Otherwise(if first DDL does not exists) will create an index.

第二个命令将使用可用的索引(如果存在)。否则(如果第一个 DDL 不存在)将创建一个索引。

The split into two commands is useful when you had given a proper name to the index and want to keep it.

当您为索引指定了正确的名称并希望保留它时,拆分为两个命令非常有用。

UPDATE: The link indicated by Thomas Haratyk is a must read, I really like it: http://viralpatel.net/blogs/understanding-primary-keypk-constraint-in-oracle/

更新:Thomas Haratyk 指出的链接是必读的,我真的很喜欢它:http://viralpatel.net/blogs/understanding-primary-keypk-constraint-in-oracle/

UPDATE2: a_horse_with_no_name is right, it can be done in a single statement like:

UPDATE2: a_horse_with_no_name 是正确的,它可以在单个语句中完成,例如:

alter table TRANSACTION
add CONSTRAINT pk_test PRIMARY KEY (id);

So, it will keep the name(won't create a sysblalbla object name) and if you use the 'USING INDEX' keyword you can specify index atributes, for example storage atributes.

因此,它将保留名称(不会创建 sysblalbla 对象名称),并且如果您使用 'USING INDEX' 关键字,您可以指定索引属性,例如存储属性。

But again, you will not have any problems with those two statements, only an index is created.

但是同样,这两个语句不会有任何问题,只会创建一个索引。

Probably SQL Developer prefer to get a ddl per object and there might be cases when it's better its way.

可能 SQL Developer 更喜欢为每个对象获取一个 ddl,并且在某些情况下可能会更好。