Oracle唯一约束和主键不为null [英] Oracle unique constraint and primary key not null

查看:780
本文介绍了Oracle唯一约束和主键不为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

表格可以有主键属性和另一个属性的唯一约束吗?



我有以下

  CREATE TABLE BRANCH(
BRA_CODE NUMBER NOT NULL PRIMARY KEY,
BRA_NAME VARCHAR(15),
BRA_ADDR VARCHAR(30),
CITY_ID NUMBER);

并尝试添加

  ALTER TABLE BRANCH ADD CONSTRAINT UNIQUE_BRANCH_NAME UNIQUE(BRA_NAME); 

我得到以下错误:

  ERROR在第1行:
ORA-02261:表中已经存在这样的唯一或主键


解决方案


问:表具有主键属性和唯一约束
另一个属性?


A:是:




  • 一个表只能有一个主键。


  • 主键可能由多个列组成(复合主键

  • p>主键始终为唯一,且始终具有唯一约束





第1行的错误:ORA-02261:此类唯一键或主键已存在
在表中


A :检查您的模式。您已经有一个主键,和/或您已经定义了相同的唯一约束。



例如:



http://www.shutdownabort.com/dbaqueries/Structure_Constraints.php

  col类型格式a10 
col cons_name格式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
其中table_name如'BRANCH'
order by 1


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

解决方案

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

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

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

这篇关于Oracle唯一约束和主键不为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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