[英]How to insert 1 million random row into table database Oracle
我无法弄清楚如何在表格中放入大量数据。 数据不得重复
建议,可能还有其他方法吗?
create table COUNTRIES (
COUNTRY_ID VARCHAR2(7),
COUNTRY_NAME VARCHAR2(40),
constraint COUNTRY_C_ID_PK primary key (COUNTRY_ID)
);
Begin
For IDS in 1..1000000
Loop
INSERT INTO "SYSTEM"."COUNTRIES" (COUNTRY_ID, COUNTRY_NAME) VALUES (dbms_random.string('L', 7), dbms_random.string('L', 15));
Commit;
End loop;
End;
如果您只是想要数据量并且不关心内容的随机性,
insert into countries select rownum, 'Name'||rownum from dual
connect by rownum<=1000000;
应该做的伎俩。
如果你有一个非常具体的随机定义,并且不能允许重复,那么异常处理可以帮助避免重复。
这种方法会很慢。 如果您需要多次执行此操作,或者需要执行大量数据,您可能需要放宽“随机”的定义并使用像Erich这样的解决方案。
--Create temporary unique constraint. (Assuming you want each column to be unique?)
alter table countries add constraint countries_name_uq unique (country_name);
--Insert random data until it worked 1 million times.
Declare
rows_inserted number := 0;
Begin
Loop
Begin
INSERT INTO COUNTRIES(COUNTRY_ID, COUNTRY_NAME)
VALUES(dbms_random.string('L', 7), dbms_random.string('L', 15));
--Only increment counter when no duplicate exception
rows_inserted := rows_inserted + 1;
Exception When DUP_VAL_ON_INDEX Then Null;
End;
exit when rows_inserted = 1000000;
End loop;
commit;
End;
/
--Drop the temporary constraint
alter table countries drop constraint countries_name_uq;
--Double-check the count of distinct rows
select count(*) from
(
select distinct country_id, country_name from countries
);
Result
------
1000000
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.