如何在 Oracle 中重命名主键以便可以重用
在 Oracle 上,我创建一个如下表:
CREATE TABLE "Mig1"( "Id" INTEGER NOT NULL , CONSTRAINT "PK_Mig1" PRIMARY KEY ( "Id" ) )
然后,我重命名 PK:
ALTER TABLE "Mig1" RENAME CONSTRAINT "PK_Mig1" TO "PK_XXX"
然后,我重命名该表:
ALTER TABLE "Mig1" RENAME TO "XXX"
然后,我尝试创建另一个使用之前重命名的表的名称的表:
CREATE TABLE "Mig1"( "Id" INTEGER NOT NULL , CONSTRAINT "PK_Mig1" PRIMARY KEY ( "Id" ) )
此时我得到: 发生错误:ORA-00955:名称已被现有对象使用
。这是因为第一个表的主键在某种程度上仍然存在,尽管它被重命名了。如果我尝试像这样创建第二个表:
CREATE TABLE "Mig1"( "Id" INTEGER NOT NULL , CONSTRAINT "YYY" PRIMARY KEY ( "Id" ) )
它有效。那么,如何正确重命名主键及其所有关联资源,以便可以重用其名称?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有一个与主键约束关联的索引,它可能仍然称为“PK_Mig1”。试试这个:
There is an index associated with the primary key constraint, and it is probably still called "PK_Mig1". Try this: