Oracle数据库中违反唯一约束的处理

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Oracle数据库中违反唯一约束的处理相关的知识,希望对你有一定的参考价值。

参考技术A

  根据NULL的定义 NULL表示的是未知 因此两个NULL比较的结果既不相等 也不不等 结果仍然是未知 根据这个定义 多个NULL值的存在应该不违反唯一约束

  实际上Oracle也是如此实现的

  SQL> CREATE TABLE T (ID NUMBER);

  表已创建

  SQL> ALTER TABLE T ADD UNIQUE (ID);

  表已更改

  SQL> INSERT INTO T VALUES ( );

  已创建 行

  SQL> INSERT INTO T VALUES ( );

  INSERT INTO T VALUES ( )

  *第 行出现错误:

  ORA : 违反唯一约束条件 (YANGTK SYS_C )

  SQL> INSERT INTO T VALUES (NULL);

  已创建 行

  SQL> INSERT INTO T VALUES (NULL);

  已创建 行

  SQL> INSERT INTO T VALUES (NULL);

  已创建 行

  但是当唯一约束为复合字段时 则情况发生了变化 根据Oracle文档的描述 对于复合字段的唯一约束 不为空字段的值是不能重复的 也就是说 如果两个字段构成了一个唯一约束 其中一个字段为空 那么另一个字段的值不能出现重复

  SQL> DROP TABLE T PURGE;

  表已删除

  SQL> CREATE TABLE T (ID NUMBER ID NUMBER);

  表已创建

  SQL> ALTER TABLE T ADD UNIQUE (ID ID );

  表已更改

  SQL> INSERT INTO T VALUES ( );

  已创建 行

  SQL> INSERT INTO T VALUES ( NULL);

  已创建 行

  SQL> INSERT INTO T VALUES ( NULL);

  已创建 行

  SQL> INSERT INTO T VALUES ( NULL);

  INSERT INTO T VALUES ( NULL)

  *第 行出现错误:

  ORA : 违反唯一约束条件 (YANGTK SYS_C )

  SQL> INSERT INTO T VALUES (NULL NULL);

  已创建 行

  SQL> INSERT INTO T VALUES (NULL NULL);

  已创建 行

  SQL> INSERT INTO T VALUES (NULL NULL);

  已创建 行

  对于全部为NULL的情况 仍然和单字段唯一约束一样 不会造成重复 但是对于部分为NULL的情况 就如上面例子所示 只要其中不为NULL的部分发生了重复 Oracle就认为约束发生了重复

  而这似乎和NULL的定义有所冲突 第一次看concept的时候一直没有搞明白Oracle为什么这么实现 不过这次再看concept的时候 已经想明白了

  由于Oracle的唯一约束是依赖索引实现的 而Oracle的BTREE索引又是不存储NULL值的 所以键值全部为NULL的记录不会记录在索引中 因此也就不会违反唯一约束了 而对于部分为NULL的记录 索引是要记录数值的 因此一旦键值中非NULL部分发生了冲突 Oracle就认为违反了的唯一约束

lishixinzhi/Article/program/Oracle/201311/18438

在oracle sql中违反了PK

我想知道是否有人可以帮助我。我在sql中运行了整个事情,但它给了我这个错误:

错误报告:SQL错误:ORA-00001:唯一约束(GAMES.ATHLETE_PK)违反00001. 00000 - “违反了唯一约束(%s。%s)”*原因:UPDATE或INSERT语句尝试插入重复键。对于在DBMS MAC模式下配置的受信任Oracle,如果存在不同级别的重复条目,则可能会看到此消息。 *操作:删除唯一限制或不插入密钥。

我想在一个事务中一次插入所有记录。并且还使用序列来生成新的主键。我试着逐个插入,没关系。但如果我一次插入所有,这是错误...

INSERT ALL 
    INTO athlete (athlete_no, athlete_name, athlete_birthdate, athlete_birthplace, athlete_born_country, athlete_gender, athlete_height, athlete_weight, athlete_team_country)
    VALUES (athlete_no_seq.nextval, 'Michael Phelps', to_date('1985-06-30','yyyy-mm-dd'), 'Towson', 'USA', 'M', 193, 88, 'USA')
    INTO athlete (athlete_no, athlete_name, athlete_birthdate, athlete_birthplace, athlete_born_country, athlete_gender, athlete_height, athlete_weight, athlete_team_country)
    VALUES (athlete_no_seq.nextval, 'Stephanie Rice', to_date('1988-06-17','yyyy-mm-dd'), 'Brisbane', 'AUS', 'F', 176, 67, 'AUS')
    INTO athlete (athlete_no, athlete_name, athlete_birthdate, athlete_birthplace, athlete_born_country, athlete_gender, athlete_height, athlete_weight, athlete_team_country)
    VALUES (athlete_no_seq.nextval, 'Rebecca Adlington', to_date('1989-02-17','yyyy-mm-dd'), 'Mansfield', 'GBR', 'F', 179, 870, 'GBR')
    INTO athlete (athlete_no, athlete_name, athlete_birthdate, athlete_birthplace, athlete_born_country, athlete_gender, athlete_height, athlete_weight, athlete_team_country)
    VALUES (athlete_no_seq.nextval, 'Lee Chong Wei', to_date('1982-10-21','yyyy-mm-dd'), 'Perak', 'MAS', 'M', 170, 60, 'MAS')
    INTO athlete (athlete_no, athlete_name, athlete_birthdate, athlete_birthplace, athlete_born_country, athlete_gender, athlete_height, athlete_weight, athlete_team_country)
    VALUES (athlete_no_seq.nextval, 'Lin Dan', to_date('1983-10-14','yyyy-mm-dd'), 'Fujian', 'CHN', 'M', 176, 68, 'CHN')
    INTO athlete (athlete_no, athlete_name, athlete_birthdate, athlete_birthplace, athlete_born_country, athlete_gender, athlete_height, athlete_weight, athlete_team_country)
    VALUES (athlete_no_seq.nextval, 'Peter Gade', to_date('1976-12-14','yyyy-mm-dd'), 'Aalborg', 'DEN', 'M', 183, 73, 'DEN')
INTO competes (athlete_no, discipline_code, sg_gameno)
    VALUES (athlete_no_seq.currval, (SELECT discipline_code FROM discipline where discipline_name = 'Swimming'), 30)
    INTO competes (athlete_no, discipline_code, sg_gameno)
    VALUES (athlete_no_seq.currval, (SELECT discipline_code FROM discipline where discipline_name = 'Swimming'), 30)
    INTO competes (athlete_no, discipline_code, sg_gameno)
    VALUES (athlete_no_seq.currval, (SELECT discipline_code FROM discipline where discipline_name = 'Swimming'), 30)
    INTO competes (athlete_no, discipline_code, sg_gameno)
    VALUES (athlete_no_seq.currval, (SELECT discipline_code FROM discipline where discipline_name = 'Swimming'), 30)
    INTO competes (athlete_no, discipline_code, sg_gameno)
    VALUES (athlete_no_seq.currval, (SELECT discipline_code FROM discipline where discipline_name = 'Swimming'), 30)
    INTO competes (athlete_no, discipline_code, sg_gameno)
    VALUES (athlete_no_seq.currval, (SELECT discipline_code FROM discipline where discipline_name = 'Swimming'), 30)
INTO venue(venue_no, venue_name, venue_location, venue_usedfrom, venueused_to, venue_seatingcapacity, venue_structure, venue_use)
    VALUES(venue_no_seq.nextval, 'Aquatics Centre', 'Olympics Park, East London', to_date('2012-07-28','yyyy-mm-dd'), to_date('2012-10-08','yyyy-mm-dd'), 17500, 'N', 'P')
    INTO venue(venue_no, venue_name, venue_location, venue_usedfrom, venueused_to, venue_seatingcapacity, venue_structure, venue_use)
    VALUES(venue_no_seq.nextval, 'Wembley Arena', 'North West London', to_date('2012-07-28','yyyy-,mm-dd'), to_date('2012-08-05','yyyy-mm-dd'), 6000, 'E', 'P')
SELECT * FROM dual;
commit;
答案

发布的代码使用INSERT ALL语法在单个语句中插入多行。它尝试使用序列NEXTVAL为每一行生成唯一标识符。

关于NEXTVAL运作的documentation is quite clear

“在包含对NEXTVAL的引用的单个SQL语句中,Oracle将序列递增一次”

因此,每次对NEXTVAL的调用都会从序列中返回相同的值,因此该语句会抛出ORA-00001。

问题是OP的代码滥用了多表插入语法。它旨在将来自一组源数据的行分布在多个表中,或者有条件地将行操作到一个表中。在任何一种情况下,它都假定源数据已经具有主键。

有很多种方法可以解决这个问题,从硬编码ID到使用多个单表插入语句。

以下是使用发布数据填充这些表的一种方法:

insert into athlete (athlete_no, athlete_name, athlete_birthdate, athlete_birthplace, athlete_born_country, athlete_gender, athlete_height, athlete_weight, athlete_team_country)
select athlete_no_seq.nextval , nm, dt, pl, ctry, gn, ht, wt, tm
from (
    select 'Michael Phelps' nm, to_date('1985-06-30','yyyy-mm-dd') dt, 'Towson' pl, 'USA' ctry, 'M' gn, 193 ht, 88 wt, 'USA' tm from dual union all
    select 'Stephanie Rice', to_date('1988-06-17','yyyy-mm-dd'), 'Brisbane', 'AUS', 'F', 176, 67, 'AUS' from dual union all
    select 'Rebecca Adlington', to_date('1989-02-17','yyyy-mm-dd'), 'Mansfield', 'GBR', 'F', 179, 870, 'GBR' from dual union all
    select 'Lee Chong Wei', to_date('1982-10-21','yyyy-mm-dd'), 'Perak', 'MAS', 'M', 170, 60, 'MAS' from dual union all
    select 'Lin Dan', to_date('1983-10-14','yyyy-mm-dd'), 'Fujian', 'CHN', 'M', 176, 68, 'CHN' from dual union all
    select 'Peter Gade', to_date('1976-12-14','yyyy-mm-dd'), 'Aalborg', 'DEN', 'M', 183, 73, 'DEN' from dual
    )
/    

insert into competes (athlete_no, discipline_code, sg_gameno)
select ath.athlete_no, disc.discipline_code, 30
from athlete ath
     cross join discipline disc
where disc.discipline_name = 'Swimming'
/

insert into venue(venue_no, venue_name, venue_location, venue_usedfrom, venueused_to, venue_seatingcapacity, venue_structure, venue_use)
select venue_no_seq.nextval, nm, loc, dtf, dtt, cap, vs, vu
from (
    select 'Aquatics Centre' nm, 'Olympics Park, East London' loc, to_date('2012-07-28','yyyy-mm-dd') dtf, to_date('2012-10-08','yyyy-mm-dd') dtt, 17500 cap, 'N' vs, 'P' vu  from dual union all
    select 'Wembley Arena', 'North West London', to_date('2012-07-28','yyyy-,mm-dd'), to_date('2012-08-05','yyyy-mm-dd'), 6000, 'E', 'P'  from dual 
    )
/

以上是关于Oracle数据库中违反唯一约束的处理的主要内容,如果未能解决你的问题,请参考以下文章

ORA-02292: 违反完整约束条件 处理

在导入Oracle数据库的时候违反唯一约束条件是为啥?要怎么解决?

在oracle sql中违反了PK

ORA-00001: 违反唯一约束条件

oracle唯一约束错误信息

oracle唯一约束