MySQL random string longer than 32 characters

I am trying to create a random 36 character string in MySQL using:

UPDATE my_table SET entity_uid = substring(MD5(RAND()) FROM 1 FOR 36); 

but the result always contains 32 characters. Is there a way to get a longer string?

+4
source share
4 answers
 UPDATE my_table SET entity_uid = UUID(); 
0
source

If you are using MySQL with a version higher than 5.7.4, you can use the recently added RANDOM_BYTES function:

  SELECT TO_BASE64(RANDOM_BYTES(40)); 

This will result in a random string such as r633j3sfgE85f3Jz+3AEx6Xo6qPXPUZruNimhId18iy+J1qOgZyCgg== .

+6
source

One option would be to generate two MD5 hashes, combine them together (a total of 64 hexadecimal characters), and then take the first 36 characters from this:

 SELECT SUBSTR(CONCAT(MD5(RAND()),MD5(RAND())),1,36) 

(NOTE: the MD5 hash is 128 bits, the MySQL MD5 () function returns 32 hexadecimal characters.)

+5
source

MD5 Returns the hash as a 32-character hexadecimal number.

According to MySQL

Calculates a 128-bit MD5 checksum for a string. The value is returned as a string of 32 hexadecimal digits, or NULL if the argument was ZERO. The return value may, for example, be used as a hash key. Seeing the notes at the beginning of this section about storing hash values โ€‹โ€‹is effective.

+2
source

All Articles