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

僤鯓⒐⒋嵵緔 提交于 2019-12-18 05:42:10

问题


On Oracle, I create a table like this:

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

Then, I rename the 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:

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?


回答1:


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

ALTER INDEX "PK_Mig1" RENAME TO "PK_XXX";


来源:https://stackoverflow.com/questions/6252355/how-to-rename-a-primary-key-in-oracle-such-that-it-can-be-reused

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!