Oracle 唯一约束和主键不为空
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9864299/
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
Oracle unique constraint and primary key not null
提问by David Garcia
Can a table have a primary key attribute and a unique constraint to another attribute?
一个表可以有一个主键属性和另一个属性的唯一约束吗?
I have the following
我有以下
CREATE TABLE BRANCH(
BRA_CODE NUMBER NOT NULL PRIMARY KEY,
BRA_NAME VARCHAR(15),
BRA_ADDR VARCHAR(30),
CITY_ID NUMBER);
and im trying to add
我正在尝试添加
ALTER TABLE BRANCH ADD CONSTRAINT UNIQUE_BRANCH_NAME UNIQUE (BRA_NAME);
and i get the following error;
我收到以下错误;
ERROR at line 1:
ORA-02261: such unique or primary key already exists in the table
回答by GolezTrol
You can have a unique contraint apart from the primary key, but the message indicates that you already added such a constraint.
除了主键之外,您还可以拥有唯一的约束,但该消息表明您已经添加了这样的约束。
回答by paulsm4
Q: Can a table have a primary key attribute and a unique constraint to another attribute?
问:一个表可以有一个主键属性和一个对另一个属性的唯一约束吗?
A: Yes:
答:是的:
A table can have no more than one primary key.
A primary key may consist of multiple columns (a "composite primary key")
Any column may have a "unique constraint", whether or not it's a primary key column
A primary key is always "unique", and always has a "unique" constraint
一张表最多只能有一个主键。
主键可能由多列组成(“复合主键”)
任何列都可能有“唯一约束”,无论它是否是主键列
主键始终“唯一”,并且始终具有“唯一”约束
ERROR at line 1: ORA-02261: such unique or primary key already exists in the table
第 1 行的错误:ORA-02261:表中已存在此类唯一键或主键
A: Check your schema. You've already already got a primary key, and/or you already defined the same unique constraint.
答:检查您的架构。您已经有一个主键,和/或您已经定义了相同的唯一约束。
For example:
例如:
http://www.shutdownabort.com/dbaqueries/Structure_Constraints.php
http://www.shutdownabort.com/dbaqueries/Structure_Constraints.php
col type format a10
col cons_name format a30
select decode(constraint_type,
'C', 'Check',
'O', 'R/O View',
'P', 'Primary',
'R', 'Foreign',
'U', 'Unique',
'V', 'Check view') type
, constraint_name cons_name
, status
, last_change
from dba_constraints
where table_name like 'BRANCH'
order by 1