如何在 Oracle 中重命名主键以便可以重用

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

How to rename a primary key in Oracle such that it can be reused

oracleora-00955

提问by Dejan

On Oracle, I create a table like this:

在 Oracle 上,我创建了一个这样的表:

CREATE TABLE "Mig1"(
  "Id" INTEGER  NOT NULL
  , CONSTRAINT "PK_Mig1" PRIMARY KEY 
(
   "Id"  ) 
) 

Then, I rename the PK:

然后,我重命名PK:

ALTER TABLE "Mig1" RENAME CONSTRAINT "PK_Mig1" TO "PK_XXX"

Then, I rename the table:

然后,我重命名表:

ALTER TABLE "Mig1" RENAME TO "XXX"

Then, I try to create another table that uses the name of the previously renamed table:

然后,我尝试创建另一个使用先前重命名的表的名称的表:

CREATE TABLE "Mig1"(
  "Id" INTEGER  NOT NULL
  , CONSTRAINT "PK_Mig1" PRIMARY KEY 
(
   "Id"  ) 
) 

At this point I get: An error occurred: ORA-00955: name is already used by an existing object. And this is because somehow the primary key of the first table is still around in some way although it was renamed. If I try to create the second table like this:

此时我得到:An error occurred: ORA-00955: name is already used by an existing object。这是因为第一个表的主键以某种方式仍然存在,尽管它被重命名了。如果我尝试像这样创建第二个表:

CREATE TABLE "Mig1"(
  "Id" INTEGER  NOT NULL
  , CONSTRAINT "YYY" PRIMARY KEY 
(
   "Id"  ) 
) 

it works. So how do I rename the primary key correctly with all of its associated resources such that its name can be reused?

有用。那么如何正确重命名主键及其所有关联资源,以便可以重用其名称?

回答by Tony Andrews

There is an index associated with the primary key constraint, and it is probably still called "PK_Mig1". Try this:

有一个与主键约束关联的索引,它可能仍称为“PK_Mig1”。尝试这个:

ALTER INDEX "PK_Mig1" RENAME TO "PK_XXX";