oracle ORA-00955“名称已被现有对象使用”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25993456/
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
ORA-00955 "name is already used by an existing object"
提问by My-Name-Is
I need to modify an existing PK. Therefore I drop an recreate it.
我需要修改现有的 PK。因此我放弃了重新创建它。
ALTER TABLE B DROP CONSTRAINT PK_B;
ALTER TABLE B ADD CONSTRAINT PK_B PRIMARY KEY ("TYP", "NR", "HH", "QUART");
Unfortunately the last Statement will give me an error ORA-00955
不幸的是,最后一条语句会给我一个错误 ORA-00955
If I create the PK constraint like it was defined originally with:
如果我像最初定义的那样创建 PK 约束:
ALTER TABLE B ADD CONSTRAINT PK_B PRIMARY KEY ("TYP", "NR", "HH");
everything works fine.
一切正常。
回答by Lalit Kumar B
Perhaps there is an INDEX
associated with the PRIMARY KEY CONSTRAINT
, and it is also named as PK_B
.
也许有一个INDEX
与 相关联PRIMARY KEY CONSTRAINT
,它也被命名为PK_B
。
You can check it as :
您可以将其检查为:
SELECT * FROM USER_INDEXES WHERE TABLE_NAME='<table_name>';
If that's true, then do :
如果这是真的,那么做:
ALTER INDEX "PK_B" RENAME TO "PK_XYZ";
Update : Regarding ALTER INDEX
statement, few important points as mentioned by Justin in the comments
更新:关于ALTER INDEX
声明,贾斯汀在评论中提到的几个要点
Oracle implicitly creates an UNIQUE
index to support the PRIMARY KEY CONSTRAINT
. Since, the index is of the same name that of the primary key, and now that the primary key is being modified, it is betterto drop and re-create the index again as per the definition of the old primary key.
Oracle 隐式地创建了一个UNIQUE
索引来支持PRIMARY KEY CONSTRAINT
. 由于索引与主键的名称相同,现在正在修改主键,最好根据旧主键的定义再次删除并重新创建索引。
My conclusion :
我的结论:
- The primary key constraint is enforced through a unique index.
- If Oracle already finds an index – unique or non-unique – it uses it for the primary key.
- If the index was initially created as non-unique, it will continue to show as non-unique, however it will actually be a unique index.
- 主键约束通过唯一索引强制执行。
- 如果 Oracle 已经找到了一个索引——唯一的或非唯一的——它会将它用作主键。
- 如果索引最初创建为非唯一索引,它将继续显示为非唯一索引,但实际上它是唯一索引。
A good demonstration and quite detailed on other aspects too, by Arup : Primary Keys Guarantee Uniqueness? Think Again.
一个很好的演示,并且在其他方面也非常详细,作者:Arup:主键保证唯一性?再想想。
回答by donsasikumar
I had the same issue where I had to do the following to delete reference to a table from the view whilst recreating the database from the scratch. I was searching for the same in tables and indexes first.
我遇到了同样的问题,我必须执行以下操作才能从视图中删除对表的引用,同时从头开始重新创建数据库。我首先在表和索引中搜索相同的内容。
connect sys/oracle as sysdba;
select * from all_tables
select * from all_indexes
(finally located the reference in the views)
select * from all_views where view_name like '%WKSTSTATE%';
drop view RUEGEN.WKSTSTATE;