Generating a unique random 10 character string using MySQL?

MySQLMySQLi Database

In order to generate a 10 character string, we can use inbuilt functions ‘rand()’ and ‘char()’. The following is the query to generate random 10 character string.

mysql>  SELECT concat(
   - > char(round(rand()*25)+97),
   - > char(round(rand()*25)+97),
   - > char(round(rand()*25)+97),
   - > char(round(rand()*25)+97),
   - > char(round(rand()*25)+97),
   - > char(round(rand()*25)+97),
   - > char(round(rand()*25)+97),
   - > char(round(rand()*25)+97),
   - > char(round(rand()*25)+97),
   - > char(round(rand()*25)+97)
   - > )AS Random10CharacterString;

Here is the output showing random 10 character string.

+-------------------------+
| Random10CharacterString |
+-------------------------+
| duscikyspy              |
+-------------------------+
1 row in set (0.00 sec)

The following is the query to generate random characters in upper case.

mysql> select concat(
   - > char(round(rand()*25)+65),
   - > char(round(rand()*25)+65),
   - > char(round(rand()*25)+65),
   - > char(round(rand()*25)+65),
   - > char(round(rand()*25)+65),
   - > char(round(rand()*25)+65),
   - > char(round(rand()*25)+65),
   - > char(round(rand()*25)+65),
   - > char(round(rand()*25)+65),
   - > char(round(rand()*25)+65)
   - > )AS Random10CharacterString;

The following is the output.

+-------------------------+
| Random10CharacterString |
+-------------------------+
| WMWWVOIXPF              |
+-------------------------+
1 row in set (0.00 sec)

The above output displays random characters in upper case.

raja
Published on 20-Nov-2018 08:14:07
Advertisements