• oracle 生成千万测试数据


    做数据库开发或管理的人经常要创建大量的测试数据,动不动就需要上万条,如果一条一条的录入,那会浪费大量的时间,本文介绍了Oracle中如何通过一条SQL快速生成大量的测试数据的方法。
    产生测试数据的SQL如下:

    SQL> select rownum as id,
      2                 to_char(sysdate + rownum / 24 / 3600, 'yyyy-mm-dd hh24:mi:ss') as inc_datetime,
      3                 trunc(dbms_random.value(0, 100)) as random_id
      4                 dbms_random.string('x', 20) random_string
      5            from dual
      6          connect by level <= 10;
            ID INC_DATETIME         RANDOM_ID RANDOM_STRING
    ---------- ------------------- ---------- --------------------------------------------------------------------------------
             1 2009-12-08 19:43:14         76 GWMU280MIVBKKOCZV620
             2 2009-12-08 19:43:15         34 GNV88O6TDHD3TWC5GWI5
             3 2009-12-08 19:43:16         77 LI6H4O5IAHQIMO4B0WMH
             4 2009-12-08 19:43:17         99 LP7XP49I0YOJIYSJDQZO
             5 2009-12-08 19:43:18         55 V3284X9RXW4UZI8BQMO3
             6 2009-12-08 19:43:19         16 T0OA52UAOGHL1TT46H25
             7 2009-12-08 19:43:20         61 UY6RUOF7HWTO86942FLP
             8 2009-12-08 19:43:21         25 JYXO4OPEW8J1CKVCPDJR
             9 2009-12-08 19:43:22         10 DONU6W9QVQM3KJ2UG8LO
            10 2009-12-08 19:43:23         76 J8DJLVNOUIZDXE4UXUJG
    
    10 rows selected

    上面SQL是利用了Oracle数据库语法的几个实用小技巧实现的:
    1、利用Oracle特有的“connect by”树形连接语法生成测试记录,“level <= 10”表示要生成10记录;
    2、利用rownum虚拟列生成递增的整数数据;
    3、利用sysdate函数加一些简单运算来生成日期数据,本例中是每条记录的时间加1秒;
    4、利用dbms_random.value函数生成随机的数值型数据,本例中是生成0到100之间的随机整数;
    5、利用dbms_random.string函数生成随机的字符型数据,本例中是生成长度为20的随机字符串,字符串中可以包括字符或数字。
    ok,那要生成10万条测试记录表可以用如下SQL:

    create table myTestTable as 
    select rownum as id,
                   to_char(sysdate + rownum/24/3600, 'yyyy-mm-dd hh24:mi:ss') as inc_datetime,
                   trunc(dbms_random.value(0, 100)) as random_id,
                   dbms_random.string('x', 20) random_string
              from dual
            connect by level <= 100000;
    // 随机生成测试数据
    update es_sms_customer set 
           sex=floor(dbms_random.value(0,3)) 
           ,name=dbms_random.string('A',6)
           ,id_card=111311198305100988 + floor(dbms_random.value(0,811311198305100988))
           ,house_phone='0'||floor(dbms_random.value(1000000001,80000000000))
           ,mobile=10000000000 + floor(dbms_random.value(3111111111,3999999999))
           ,fax='0'||floor(dbms_random.value(1000000001,80000000000))
           ,post_code=''||floor(dbms_random.value(100001,999999))
           ,email=dbms_random.string('L',6)||'@'||dbms_random.string('L',4)||'.com'
           ,qq=floor(dbms_random.value(10000001,999999999))
           ,addr=dbms_random.string('L',16)
           ,birth_day=birth_day+365*floor(dbms_random.value(1,50))
           ,occupation=floor(dbms_random.value(0,5))
           ,fixed_assets=floor(dbms_random.value(0,8))
           ,car_owner=floor(dbms_random.value(0,3))
           ,car_buy_time=birth_day+365*floor(dbms_random.value(1,50))
           ,car_brand=dbms_random.string('L',5)
           ,bui_name=dbms_random.string('L',5)
           ,car_price=floor(dbms_random.value(5,500))
           ,bui_area_count=floor(dbms_random.value(80,300))
           ,bui_addr=dbms_random.string('L',10)
           ,bui_post=''||floor(dbms_random.value(100001,999999))
           ,bui_manager=dbms_random.string('L',10)
           ,bui_developer=dbms_random.string('L',10)
           where rownum<1000;
  • 相关阅读:
    进度条
    html5 表单新增事件
    html5 表单的新增type属性
    html5 表单的新增元素
    html5 语义化标签
    jq 手风琴案例
    codeforces 702D D. Road to Post Office(数学)
    codeforces 702C C. Cellular Network(水题)
    codeforces 702B B. Powers of Two(水题)
    codeforces 702A A. Maximum Increase(水题)
  • 原文地址:https://www.cnblogs.com/liuweihua/p/2484444.html
Copyright © 2020-2023  润新知