Generate a random and unique string in MySQL
Today, we will learn to use various functions in MySQL to generate random and unique strings. These functions include MD5()
, RAND()
, , SUBSTR()
and UUID()
.
There is no inbuilt method to generate random string in MySQL, but there are many other methods which can give us an advantage to meet the requirement. Here, we will learn about the simplest and most efficient way to generate random and unique string in MySQL.
Generate a random and unique string in MySQL using MD5()
, RAND()
andSUBSTR()
Sample code:
SELECT SUBSTR(MD5(RAND()),1,8) AS RandomString;
Output:
+--------------+
| RandomString |
+--------------+
| 7d192f5f |
+--------------+
1 row in set (0.00 sec)
The MD5() function generates a 128-bit checksum representation of its argument. In the above example, the value generated by the RAND() function is MD5()
the function's argument; RAND()
the function generates a random value.
However, the checksum result is MD5()
32 alphanumeric characters generated from the parameter passed to the function. Furthermore, we use SUBSTR()
extract a portion of the string as per the requirement of our project MD5
.
SUBSTR() takes three parameters: the string, the starting position to extract, and the length. In the sample code above, we MD5
extracted 8 characters from the string, starting at the first character.
UUID()
Generate a random and unique string in MySQL using
Sample code:
SELECT LEFT(UUID(), 8)
Output:
+-----------------+
| LEFT(UUID(), 8) |
+-----------------+
| 4a1f35bc |
+-----------------+
1 row in set (0.00 sec)
Another way to generate an 8-character string in MySQL is LEFT(UUID(),8)
as described above. RFC 4122 (Universally Unique Identifier URN Namespace) specifies UUID() (Universally Unique Identifier), which is a 128-bit long value.
It generates a globally unique value across time and space. Since UUID()
the values produced are unique, they are unpredictable or unguessable.
This means that UUID()
a random value is always generated. We can use the following query to get a string of 8 or 10 characters long.
Sample code:
-- for 8 characters long string
SELECT LEFT(UUID(),8) random_string ;
-- for 10 characters long string
SELECT RIGHT(UUID(),10) random_string ;
Remember that UUID()
we output a hexadecimal value consisting of five parts separated by hyphens. Therefore, we can get a string containing numbers, hyphens, and letters, depending on the desired string length.
Sample code:
SELECT UUID(), LEFT(UUID(), 8), right(uuid(),20);
Output:
+--------------------------------------+-----------------+----------------------+
| UUID() | LEFT(UUID(), 8) | right(uuid(),20) |
+--------------------------------------+-----------------+----------------------+
| b3e73af1-d811-11ec-b138-d8d09042fdaa | b3e73b03 | ec-b138-d8d09042fdaa |
+--------------------------------------+-----------------+----------------------+
1 row in set (0.00 sec)
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Changing MySQL root password on Mac
Publish Date:2025/04/21 Views:160 Category:MySQL
-
This article teaches you how to change the MySQL user password on OS X. root We will be using XAMPP so that you can change the password using the MySQL console. Installing XAMPP for OSX First, download and install XAMPP for OSX from Apache
Using CURRENT_TIMESTAMP as a default value in MySQL
Publish Date:2025/04/21 Views:196 Category:MySQL
-
This article teaches you how to use as 5.6.5 in MySQL versions lower than . Thus, you can prevent MySQL error 1293. CURRENT_TIMESTAMP DEFAULT Our approach involves reordering table columns and using DEFAULT 0 and time values. Reproduce the
If ELSE in MySQL
Publish Date:2025/04/11 Views:86 Category:MySQL
-
In this tutorial, we aim to explore how to use IF ELSE the statement in MySQL. One of the key roles of a data analyst is to gather insights from the data and produce meaningful results. It can be done with the help of several data filtering
DATETIME vs. TIMESTAMP in MySQL
Publish Date:2025/04/11 Views:117 Category:MySQL
-
DATETIME and TIMESTAMP are two different data types that can be used to store values that must contain both a date and a time portion. In this article, we will understand how it is stored in the database and the memory required for ea
Execute multiple joins in one query in MYSQL
Publish Date:2025/04/11 Views:94 Category:MySQL
-
Have you ever wondered how to include multiple joins in one query in MySQL? You have come to the right place. Remember that joins allow us to access information from other tables. This information is included separately to avoid redundancy.
Joining 3 tables in MySQL
Publish Date:2025/04/11 Views:187 Category:MySQL
-
In this tutorial, we will learn how to join three tables in MySQL. Businesses and organizations may have to visualize three tables simultaneously based on certain matching columns common to all three tables. This operation is allowed in MyS
Use of UPDATE JOIN in MySQL
Publish Date:2025/04/11 Views:85 Category:MySQL
-
This tutorial will explain how to use the statement in MySQL database UPDATE JOIN . We generally use joins to iterate over the rows in a particular table which may or may not have similar rows in other tables. We can UPDATE use JOIN the cla
How to use the Row_Number() function in MySQL
Publish Date:2025/04/11 Views:143 Category:MySQL
-
In this tutorial, we will explain how to use the VALUES function in MySQL ROW_NUMBER() . This is a sorting method that assigns consecutive numbers within a partition starting from 1. It is important to note that no two rows within a partiti
Multiple primary keys in MySQL
Publish Date:2025/04/11 Views:66 Category:MySQL
-
In this tutorial, our goal is to explore the concept of multiple primary keys for a table in MySQL. Many times, businesses and organizations have to assign certain columns as primary keys. This primary key has multiple uses and reasons to b