Oracle表上违反了唯一约束

时间:2015-11-10 12:26:46

标签: oracle oracle11g

我想在Oracle表中生成随机数据:

第一张表

-- TABLE AGENT_HISTORY

CREATE TABLE AGENT_HISTORY(
  EVENT_ID INTEGER NOT NULL,
  AGENTID INTEGER NOT NULL,
  EVENT_DATE DATE NOT NULL
)
/

 CREATE INDEX IX_RELATIONSHIP1 ON AGENT_HISTORY (AGENTID)
/

-- ADD KEYS FOR TABLE AGENT_HISTORY

ALTER TABLE AGENT_HISTORY ADD CONSTRAINT KEY8 PRIMARY KEY (EVENT_ID)
/

第二张表

-- TABLE CPU_HISTORY

CREATE TABLE CPU_HISTORY(
  CPU_HISTORY_ID INTEGER NOT NULL,
  EVENT_ID INTEGER NOT NULL,
  CPU_NAME VARCHAR2(50 ) NOT NULL,
  CPU_VALUE NUMBER NOT NULL
)
/

我试图创建这个PL / SQL块:

Error starting at line : 1,690 in command -
BEGIN

FOR loop_counter IN 1..1000
LOOP
   INSERT INTO CPU_HISTORY_ID (CPU_HISTORY_ID, EVENT_ID, CPU_NAME, CPU_VALUE) VALUES (loop_counter, loop_counter, 'cpu1', dbms_random.value(1,100));
END LOOP;

COMMIT;
END;

错误

  Error report -
    ORA-06550: line 5, column 16:
    PL/SQL: ORA-00942: table or view does not exist
    ORA-06550: line 5, column 4:
    PL/SQL: SQL Statement ignored
    06550. 00000 -  "line %s, column %s:\n%s"
    *Cause:    Usually a PL/SQL compilation error.
    *Action:
    Error starting at line : 1,680 in command -

第二个PL / SQL块

BEGIN

FOR loop_counter IN 1..1000
LOOP
   INSERT INTO AGENT_HISTORY (EVENT_ID, AGENTID, EVENT_DATE) VALUES (loop_counter, 22, SYSDATE);
END LOOP;

COMMIT;
END;

错误

Error report -
ORA-00001: unique constraint (ADMIN.KEY8) violated
ORA-06512: at line 5
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.

如果我错了,你能给我一些建议吗?

是否有另一种方法可以在表中生成1000行随机值?

1 个答案:

答案 0 :(得分:4)

第一个错误,因为您要插入的表格是CPU_HISTORY_ID,但它应该是CPU_HISTORY

可能是因为该表已填充数据而导致第二个错误。你是否在没有删除数据的情况下执行了第二次PL?