如何在Oracle中重命名主键以便可以重用 [英] How to rename a primary key in Oracle such that it can be reused

查看:621
本文介绍了如何在Oracle中重命名主键以便可以重用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

On Oracle, I create a table like this:


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

然后,我将PK重命名:

Then, I rename the PK:


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

然后,我将表重命名:


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"  ) 
) 

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

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?

推荐答案

有一个与主键约束关联的索引,它可能仍被称为"PK_Mig1".试试这个:

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";

这篇关于如何在Oracle中重命名主键以便可以重用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆