2012-09-10 3 views
1

누구든지이 문제를 해결할 수 있는지 궁금합니다. SQL에서 모든 것을 실행했지만 그 오류가 발생했습니다.PK가 oracle sql에서 위반했습니다

Error report: SQL Error: ORA-00001: unique constraint (GAMES.ATHLETE_PK) violated 00001. 00000 - "unique constraint (%s.%s) violated" *Cause: An UPDATE or INSERT statement attempted to insert a duplicate key. For Trusted Oracle configured in DBMS MAC mode, you may see this message if a duplicate entry exists at a different level. *Action: Either remove the unique restriction or do not insert the key.

한 번에 모든 레코드를 삽입하려고합니다. 시퀀스를 사용하여 새로운 기본 키를 생성 할 수도 있습니다. 나는 하나씩 하나씩 삽입 해 보았습니다. 하지만 한번에 모두 삽입하면 오류가 발생합니다 ...

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; 
+2

는 '당신은 다중 테이블 삽입 문의 어떤 부분의 순서를 지정할 수 없습니다': 여기

는 게시 된 데이터와이 테이블을 채울 수있는 한 가지 방법이다 /docs.oracle.com/cd/E11882_01/server.112/e26088/statements_9014.htm#i2080134). 시퀀스에 대한 모든 참조는 동일한 값을 갖습니다. 단일 * 트랜잭션 *이 아닌 단일 * 명령문 *에 모든 것을 삽입해야합니까? –

+0

각 요소의 삽입을 단일 트랜잭션으로 처리해야합니다. "하나의 요소를 하나의 트랜잭션으로 처리하는 것"으로 이해하지 못합니다. –

+0

전체 문장을 입력하는 올바른 방법이 무엇인지 알 수 있습니까? –

답변

2

게시 된 코드는 INSERT ALL 구문을 사용하여 단일 명령문에 여러 행을 삽입합니다. 각 행에 고유 식별자를 생성하기 위해 시퀀스 NEXTVAL을 사용하려고 시도합니다. NEXTVAL의 기능에 documentation is quite clear

:

"Within a single SQL statement containing a reference to NEXTVAL, Oracle increments the sequence once"

그래서 NEXTVAL에 이러한 호출의 각 시퀀스에서 같은 값을 반환합니다, 그래서 문은 ORA-00001 던진다.

문제는 OP 코드가 다중 테이블 삽입 구문을 잘못 사용하고 있다는 것입니다. 하나의 원본 데이터 집합에서 여러 테이블로 행을 분산하거나 행을 조건부로 하나의 테이블로 조작하기위한 것입니다. 두 경우 모두 소스 데이터에 이미 기본 키가 있다고 가정합니다.

ID를 하드 코딩하여 여러 개의 단일 테이블 삽입 문을 사용하는 방법부터 다양한 방법으로이 문제를 해결할 수 있습니다./: - 마지막 총알 [여기] (HTTP를 참조

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 
    ) 
/
+0

고마워요, 친구. 고맙습니다. – Keven

관련 문제