Go Oracle dbms_random function Usage quickly generates multiple test data

Source: Internet
Author: User
Tags printable characters

People doing database development or management often want to create a large number of test data, there will be tens of thousands of things, if one entry, it will waste a lot of time, this article describes how Oracle in a SQL to quickly generate a large number of test data method.

First, simulate 100 random data.

[SQL]View Plaincopyprint?
    1. 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)) as random_id,
    4. Dbms_random.string (' $ ', random_string)
    5. From dual
    6. Connect by Level <= 100;

The above code is not inserted into the database, to insert only need to add the CREATE table table to the SQL as or INSERT into select mode

The above SQL is implemented using several practical tips for Oracle database syntax:
1, using Oracle's unique "Connect by" tree connection syntax to generate test records, "level <= 10" means to generate 10 records;
2, using rownum virtual column to generate incremental integer data;
3, using the Sysdate function plus some simple operations to generate date data, in this case, the time of each record plus 1 seconds;
4, using the Dbms_random.value function to generate random numerical data, in this case, a random integer from 0 to 100 is generated;
5, the use of the dbms_random.string function to generate random character data, in this case, the generation of a random string length of 20, the string can include characters or numbers.

Dbms_random. Parameters in the function description

[SQL]View Plaincopyprint?
    1. --**************************************** Random digital ***************************************************
    2. Select Decode (trunc (dbms_random.value (0, 2)),' 0 ',' female ',' 1 ',' Male ') from dual
    3. Select Length (dbms_random.random) from dual; --produce a number with any length of 9,10,11 bit
    4. Select ABS (mod (dbms_random.random,100)) from dual; --Generate a random number within 100
    5. Select Trunc (10+90*dbms_random.value) from dual; --Generates any 2-bit random integer between 10~100 (contains 10, not including)
    6. Select Dbms_random.value from dual; --Produces a decimal number (37 or 38 digits after the decimal point) that is greater than 0 and less than 1
    7. Select Dbms_random.value (10,20) from dual; --Produce a decimal number (38 digits after the decimal point) that is greater than 10 and less than 20
    8. Select Dbms_random.normal from dual; --The normal function returns a set of numbers that obey a normal distribution. This normal distribution has a standard deviation of 1 and a expected value of 0.  68% of the values returned by this function are between 1 and +1, 95% between 2 and +2, and 99% between 3 and +3.
    9. --**************************************** Random date ***************************************************
    10. Select To_date (Trunc (Dbms_random.value (To_char (sysdate-10, ' J ')), To_number (To_char (Sysdate, ' J ')  )))), ' J ') from dual; --Take a random date in the last 10 days
    11. Select (Sysdate-1/24/60 *) + dbms_random.value (1, 1800)/3600/24 from dual; --Take random time in the last 30 minutes
    12. Select To_date (To_char (to_date (' 2015-01-01 ', ' yyyy-mm-dd '), ' J ') + trunc (dbms_random.   VALUE (0, 365)), ' J ') from dual; --Query for any date in 2015 years
    13. --take random time within the specified date range
    14. Select To_date (Trunc (Dbms_random.value (To_number (To_char (' To_date', ' 20150501 '),' J ')),
    15. To_number (To_char (to_date (' 20150531 ', ' YYYYMMDD ') + 1, ' J ' )))
    16. ), ' J ') + dbms_random.value (1, 3600)/3600 prize_time
    17. from dual;
    18. At any moment between now and the past 100 years
    19. Select To_date (Trunc (Dbms_random.value (
    20. To_number (To_char (add_months (sysdate,-100 *),' J ')),
    21. To_number (To_char (sysdate + 1, ' J ')))
    22. ), ' J ') + dbms_random.value (1, 3600)/3600 from dual;
    23. --**************************************** Random character (produces a string of any length of 20) ************************************************* **
    24. Select Dbms_random.string (' u ', +) from dual --' u ', ' u ' Returns all uppercase strings (uppercase) to be calculated, as long as the parameter is not a Chinese or full-width character, The return data is all uppercase
    25. Select Dbms_random.string (' l ', ') from dual --' l ', ' l '-returns all lowercase strings (lowercase)
    26. Select Dbms_random.string (' A ', ' a ') from dual--' a ', ' a '-returns the case of a string with a combination of uppercase and lowercase (mixed)
    27. Select Dbms_random.string (' x ', ') from dual --' x ', ' X '-Returns all uppercase and numeric strings (Uppercase,alpha&numeric)
    28. Select Dbms_random.string (' P ', ') from dual --' P ', ' p '-Returns a random combination of characters appearing on the keyboard (any printable char)
    29. To generate 100,000 test records, you can use the following 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)) as random_id,
      Dbms_random.string (' x ', ') random_string
      From dual
      Connect by level <= 100000;
    30. Dbms_random. String usage:

      1) option ' u ', ' U '-returning string in uppercase alpha characters
      2) option ' l ', ' l '-returning string in lowercase alpha characters
      3) option ' A ', ' a '-returning string in mixed case alpha characters
      4) option ' x ', ' X '-returning string in uppercase Alpha-numeric characters
      5) option ' P ', ' p '-returning string in any printable characters.
      6) Option Otherwise The returning string is in uppercase alpha characters.

      Example:

      Generate 8-bit passwords consisting of uppercase letters and numbers

      [Email Protected]>select dbms_random.string (' x ', 8) "X_8_password" from dual;

      X_8_password

      ----------------------------------------------

      Tt3isguv

Go Oracle dbms_random function Usage quickly generates multiple test data

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.