Generating a unique random 10 character string using MySQL?


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.

Updated on: 30-Jul-2019

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements