MySQL – How to Generate Random Number

In MySQL, UUID() function returns Universal Unique Identifier that generates 36 characters long value which is 5 part hexadecimal numbers. If you want to generate random password, you can make use of this function which generate random number.

SELECT UUID() ;

Returns the string 85aeb064-8f73-11e5-85ef-02fcc4101968 (Note that this is random, when you execute you will get different value). As the total length is 36, you can make use of the result to get a random password with any length.

Suppose you want to generate 8 character length, you can generate it using

SELECT LEFT(UUID(),8) random_password ;

If you want to generate 10 character length, you can generate it using

SELECT RIGHT(UUID(),10) random_password ;

This way you can generate a random password. As UUID () returns the hexadecimal number with five parts separated by hyphen, your word will have numbers, alphabets and hyphen depends on the length you use.

MySQL - How to Generate Random Number uuid

There can be many ways of doing this, but this is the simplest one. Let me know what you think about this method. If you are using any other method please share the same in the comment sections. I will post it on this blog with due credit to you.

Reference: Pinal Dave (https://blog.sqlauthority.com)

MySQL, SQL Random
Previous Post
SQL SERVER – Install Error – The /UIMode setting cannot be used in conjunction with /Q or /QS
Next Post
SQL SERVER – How to Connect Using NT AUTHORITY \ SYSTEM Account?

Related Posts

2 Comments. Leave new

  • I don’t think, that it is good idea to use guid for password generation. It has bad distribution:
    SELECT UUID(), UUID_SHORT(), PASSWORD(RAND())
    FROM information_schema.COLUMNS
    LIMIT 10;

    Result:
    546444f2-cc95-11e5-8f39-0a0027000000 24396304541043266 8E257F384C
    54644583-cc95-11e5-8f39-0a0027000000 24396304541043267 57502FAD7C
    546445ae-cc95-11e5-8f39-0a0027000000 24396304541043268 96FD454005
    546445d5-cc95-11e5-8f39-0a0027000000 24396304541043269 4A3DEB4FC1
    546445f2-cc95-11e5-8f39-0a0027000000 24396304541043270 6E22261A73
    54644619-cc95-11e5-8f39-0a0027000000 24396304541043271 47262FF134
    54644637-cc95-11e5-8f39-0a0027000000 24396304541043272 988F1E2281
    54644659-cc95-11e5-8f39-0a0027000000 24396304541043273 1AE72FD38D
    54644677-cc95-11e5-8f39-0a0027000000 24396304541043274 9FD59445F2
    54644699-cc95-11e5-8f39-0a0027000000 24396304541043275 1240BBCA5F

    Reply
  • OMG! No. This is not a way to generate truly random passwords!
    Characters will be only numbersand letters from a to f. And the symbol can only be a hyphen.

    So,you’ll have a password that’ll be a combination of 23 elements (10 numbers, 12 letters (a to f lowercase and uppercase) and the hyphen) instead of 78 (10 numbers, 48 letters and about 20 symbols).

    And,of course, that there is a pattern where the hyphen is (two hyphens toghether is not posible).

    Only this reasons should be enough to not use this method. If we considerwhat JeStoneDev is saying, well…

    Reply

Leave a Reply