Oracle随机数据生成最佳方法排行榜与实用工具推荐
使用存储过程生成任意数量的随机数据
先看第一个脚本。若你需要100条、1000条乃至更多测试数据,手动编写INSERT语句显然不现实。该存储过程通过循环与序列灵活控制数据量,每条记录的日期、金额、性别字段均采用随机生成逻辑,模拟真实业务场景。
/** *创建序列,用于获得特征数据*/create sequence mock_data_seqminvalue 1 start with 1 increment by 1 cache 100;/***创建数据库表*/create table tab_mock_data (id number,serno long,username varchar2(50),birthdate date,sex number(1),amount number(19,6),tag varchar2(20),remark varchar2(200),inputdate timestamp);/***创建存储过程*/create or replace procedure mockdatagenertator(maxrecords in number :=100) isi number :=1;beginfor i in 1..maxrecordsloop insert intotab_mock_data VALUES(mock_data_seq.NEXTVAL,i,'测试数据',SYSDATE-(i/24 i/24/3600), mod(i,3),dbms_random.random/10000,'std','备注',current_timestamp);end loop;commit;dbms_output.put_line(maxrecords||' insert done');end mockdatagenertator;-- set serveroutput on; -- 执行,产生数据量通过参数指定 call mockdatagenertator(1000);-- 查询数据select * from tab_mock_data;--- 清理数据drop sequence mock_data_seq;drop table tab_mock_data;drop proceduremockdatagenertator;
批量创建表
某些测试场景需要多张结构完全相同的表,例如分表实验或压力测试。下面的匿名块能轻松搞定——只需指定表数量,循环执行DDL语句,瞬间即可生成10张(或任意数量)结构一致的表格。
declaremaxtablecount number;beginmaxtablecount := 10;while maxtablecount > 0 loopexecute immediate 'create table tab_mock_data' || maxtablecount || ' (id number,serno long,username varchar2(50),birthdate date,sex number(1),amount number(19,6),tag varchar2(20),remark varchar2(200),inputdate timestamp)';maxtablecount := maxtablecount - 1;end loop;end;