【问题标题】:PK violated in oracle sqloracle sql中违反PK
【发布时间】:2012-09-03 10:34:01
【问题描述】:

我想知道是否有人可以帮助我解决这个问题。我在 sql 中运行了整个过程,但它给了我这个错误:

错误报告: SQL 错误:ORA-00001:违反唯一约束 (GAMES.ATHLETE_PK) 00001. 00000 - “违反了唯一约束 (%s.%s)” *原因:一个 UPDATE 或 INSERT 语句试图插入一个重复的键。 对于在 DBMS MAC 模式下配置的 Trusted 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;

【问题讨论】:

  • '您不能在多表插入语句的任何部分指定序列' - 请参阅最后一个项目符号 here。对序列的所有引用都获得相同的值。您真的需要将它们全部插入到单个 statement 中,而不仅仅是单个 transaction 中吗?
  • 我需要将每个元素的插入视为单个事务。我真的不明白“将一个元素的插入视为单个事务”。
  • 我可以知道输入整个语句的正确方法是什么吗?
  • 将您的多表插入拆分为多个不同的插入操作。确保您没有在会话中启用自动提交,并在完成所有插入后只提交一次。然后是单个提交关闭单个事务,即使在事务期间执行了多个插入语句。
  • 多个不同的插入操作是什么意思?如果你举个例子juha可以吗? :)

标签: oracle insert sequence primary-key unique-constraint


【解决方案1】:

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

documentation is quite clear关于NEXTVAL的作用:

“在包含对 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 
    )
/

【讨论】:

  • 谢谢你,我的朋友。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多