SQL ORA-02264: 名称已被现有约束使用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22089212/
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-02264: name already used by an existing constraint
提问by Khizar Iqbal
I Am Working On SQL And I Am Facing A Problem In Creating The Table! Here Is My Code:
我正在处理 SQL,但在创建表时遇到了问题!这是我的代码:
CREATE TABLE Voucher_Types
(
ID Number(3, 0),
Abbreviation VarChar(2),
Voucher_Type VarChar(100),
EntryBy VarChar(25),
EntryDate Date,
CONSTRAINT ID_PK Primary Key(ID)
);
And I Get The Following Error,
ORA-02264: name already used by an existing constraint
I Am Using Oracle10g
I Goggled It And Found Some Topics But They Didn't Help Me, Can Somebody Help Me In This Problem! Thanks In Advance..!
并且我
收到以下错误,ORA-02264:名称已被现有约束
使用我正在使用 Oracle10g
我查看了它并找到了一些主题但他们没有帮助我,有人可以帮助我解决这个问题!提前致谢..!
回答by René Nyffenegger
You have another table that has already a constrained with the name ID_PK
.
您还有另一个表,该表已经受名称约束ID_PK
。
If you want to find out which table it is, you might try
如果你想找出它是哪个表,你可以试试
select owner, table_name from dba_constraints where constraint_name = 'ID_PK';
Most probably, you copied your create table
statement but did not change the primary key's constraint name.
最有可能的是,您复制了create table
语句但没有更改主键的约束名称。
It is generally felt to be good practice to include the table name in a constraint's name. One reason is exactly to prevent such "errors".
通常认为在约束名称中包含表名称是一种很好的做法。原因之一正是为了防止此类“错误”。
So, in your case, you might use
所以,在你的情况下,你可能会使用
CREATE TABLE Voucher_Types
(
...
CONSTRAINT Voucher_Types_PK Primary Key(ID)
);
UpdateWhy can't the same constraint name be used twice? (As per your question in the comment): This is exactly because it is a name that identifiesthe constraint. If you have a violation of a constraint in a running system, you want to know which constraint it was, so you need a name. But if this name can refer multiple constraints, the name is of no particular use.
更新为什么不能两次使用相同的约束名称?(根据您在评论中的问题):这正是因为它是一个标识约束的名称。如果您在运行的系统中违反了某个约束,您想知道它是哪个约束,因此您需要一个名称。但是如果这个名字可以引用多个约束,这个名字就没有什么特别的用处了。
回答by Frank Schmitt
The error message tells you that there's already another constraint named ID_PK in your schema - just use another name, and you should be fine:
错误消息告诉您,您的架构中已经有另一个名为 ID_PK 的约束 - 只需使用另一个名称,您应该没问题:
CREATE TABLE Voucher_Types
(
ID Number(3, 0),
Abbreviation VarChar(2),
Voucher_Type VarChar(100),
EntryBy VarChar(25),
EntryDate Date,
CONSTRAINT VOUCHER_TYPES_ID_PK Primary Key(ID)
);
To find the offending constraint:
要找到违规约束:
SELECT * FROM user_constraints WHERE CONSTRAINT_NAME = 'ID_PK'
回答by Vimal
you have same constraint name in other table you need to change the constraint name
您在其他表中有相同的约束名称,您需要更改约束名称
if you want to see table name which you used this constraint you can see following query:
如果您想查看使用此约束的表名,您可以看到以下查询:
select table_name,constraint_name from user_constraints where lower(constraint_name)='id_pk';
select table_name,constraint_name from user_constraints where lower(constraint_name)='id_pk';
回答by i100
this means that there is a constraint named ID_PK try with
CONSTRAINT Voucher_Types_ID_PK Primary Key(ID)
for instance
这意味着有一个名为 ID_PK try 的约束
CONSTRAINT Voucher_Types_ID_PK Primary Key(ID)
,例如
you can check whether exists with
你可以检查是否存在
select * from user_constraints where upper(constraint_name) = 'ID_PK';
or
或者
select * from all_constraints where upper(constraint_name) = 'ID_PK';
回答by wieseman
The anwser of Frank Schmitt is good. However for the name of your constraint you can also use the table alias as part of your constraint name. The name of your constraint would be something like: vte_pk. And there is no necessary to invoke the name of the concerning column in the constraint name. What if the primary key is over 2 (or more) columns?
Frank Schmitt 的回答很好。但是,对于约束名称,您还可以使用表别名作为约束名称的一部分。您的约束的名称类似于:vte_pk。并且没有必要在约束名称中调用相关列的名称。如果主键超过 2(或更多)列怎么办?
Thus ... CONSTRAINT VTE_PK Primary Key(ID) ...
.
因此... CONSTRAINT VTE_PK Primary Key(ID) ...
。