MySQL Random Unique String

I am writing a "reset password" for a website, and as part of this, I need to create a random unique string of characters that will be inserted into the MySQL database.

At the moment, this is simply done using SHA1(RAND()) , and it works great. However, it only gives 1.46150164E48 (for SHA1 I am not so sure that the unique range for RAND ) there are different combinations and is pedantic. I want to ensure that a situation where two identical values ​​are never inserted.

Now I could easily do this using PHP (since this is a PHP-based website), iterating through the loop until a random line is created that does not exist, which in most cases will be 1 iteration, but to me Was it interesting if this is possible with MySQL?

That is: is it possible in MySQL to generate a pseudo-random string of characters that MySQL itself will guarantee is unique in this table and field?

+4
source share
1 answer

You can also use the global UUID () function, which generates a unique value each time:

 SELECT UUID() 
+9
source

All Articles