Updated June 6, 2023
Introduction to MySQL UUID
MySQL UUID is defined as a “Universally Unique Identifier” which shows the usage of a Primary key for a table. It is described based on RFC 4122, a URN Namespace, i.e., Universally Unique Identifier (UUID). Here, we will show UUID as PK (Primary Key) and its pros and cons during application in MySQL query. In the universe and time interval, we consider UUID as a distinct numeral. If two independent servers, associated with each other, produce UUIDs, we project that both UUID values will be unique.
Syntax of MySQL UUID
A UUID value represents a 128-bit numeral that signifies a UTF8 string consisting of five hexadecimal digits. It follows the following format:
dddddddd-eeee-ffff-gggg-hhhhhhhhhhhh
For generating the UUID values, we need to apply the UUID function shown below:
UUID()
Here, the function UUID() gives a UUID value in agreement with the UUID version 1 termed in RFC 4122.
We can illustrate this by utilizing the UUID() function as follows:
SELECT UUID();
Output:
It should be noted that the function is insecure for applications based on statements. If this function is implemented whenever the binlog_format is fixed to an account, it will log a warning.
How UUID works in MySQL?
UUID() function usage may be similar to a Primary key for a table in the database. Let us view the MySQL UUID compared to Primary Key with AUTO_INCREMENT INT attributes in the database.
The use of MySQL UUID for a primary key offers several advantages, including:
- The UUID values are distinct across databases, tables, and servers, allowing us to unite rows from several records or distributed databases across MySQL servers.
- UUIDs do not describe the info about any data; therefore, they are more secure for URLs. Assume that if an employee having employee id 05 is accessing their account by using the URL: http://www.test.com/employees/05/then, for others also, it will be easy to guess and use as 07, 08, etc.. So it will provide a target for any harmful attack to breach security and increase risk.
- UUID values can be produced anywhere to evade a round trip to the database server. Similarly, it is a bridge’s logic in the application. Let us assume that to input records into a parent table and child tables, we need to insert them into the base table, i.e., the parent table initially receives the produced id, and next, the data will be inserted into the child tables. Thus, by applying UUID, we can first create the value of the base table’s primary key and input rows at identical times into both parent and child tables within a transaction.
Drawbacks are briefed below beside the pros of using UUID relating to a Primary key:
- If we use integers, i.e., 4 bytes, or big integers, i.e., 8 bytes, then it takes less storage capacity than UUID values, i.e., storing 16 bytes.
- Restoring can be more problematic. For the expression WHERE Employee_ID = 05 is easy instead of WHERE Employee_ID = ‘fd7bc2fb-9ve4-10t7-49yy-p9vbc73cbxyf’.
- Using UUID values can potentially lead to performance issues due to their larger size and unordered form.
UUID Solution in MySQL is as follows:
MySQL allows you to store the UUID values in BINARY format or, say, compress form and can be displayed in human-readable, i.e., VARCHAR format.
You can achieve this by utilizing the following MySQL functions that are related to UUIDs:
- UUID_TO_BIN: This function is responsible for converting a UUID value from the VARCHAR format, which is humanly readable, into a BINARY format compacted one for storing.
- BIN_TO_UUID: It is just opposite to the previous one. BIN_TO_UUID function changes the UUID from a condensed form or BINARY format to VARCHAR format, which is human-understandable for demonstrating.
- IS_UUID: This IS_UUID function provides 1 when the argument is an effective string-format UUID. But if the argument is not legal string format UUID, the function gives 0 as a result. Also, if the argument is NULL, the IS_UUID function will return NULL.
The formats below are the valid string UUID arrangements used in MySQL:
dddddddd-eeee-ffff-gggg-hhhhhhhhhhhh
ddddddddeeeeffffgggghhhhhhhhhhhh
{ dddddddd-eeee-ffff-gggg-hhhhhhhhhhhh }
Remember that these functions, IS_UUID, UUID_TO_BIN, and BIN_TO_UUID, are only present or supportive in MySQL version 8.0 or, say, later ones.
Examples of MySQL UUID
Let us evaluate some of the examples of the working of UUID as the Primary key for a table in the MySQL database server:
Example #1
Suppose we have shown here an experiment for UUID to have unique values at intervals on the same server, so; we have executed a UUID value which gives the following result and provides a gap in time interval for 3 seconds with the SLEEP() in MySQL.
Code:
SELECT UUID(); do sleep(3); SELECT UUID();
Output:
After executing the UUID value, we observed that it differed from the original value.
Example #2
We will initially create a new table in our database using the CREATE statement query.
Code:
CREATE TABLE Employees (Employee_IDBINARY(16) PRIMARY KEY, Employee_Name VARCHAR(255));
After executing the above command, a table structure will be created in the database. Next, we will insert some data into it. The primary key column is specified with binary (16) with no character and no collation.
Let us input a few UUID values into the column Employee_ID in the Employees table.
To implement this, we will also use the functions UUID() and UUID_TO_BIN() with the help of the INSERT query below.
Code:
INSERT INTO Employees (Employee_ID, Employee_Name) VALUES
('1','Nikhil'), ('2', 'Rita'),('3', 'Sangam'), ('4', 'Hari');
Now see the contents of the table by the following query.
Code:
SELECT * FROM Employees;
Output:
Or, you can use this query with a function in MySQL version 8.0 and above.
Code:
INSERT INTO Employees (Employee_ID, Employee_Name) VALUES
(UUID_TO_BIN(UUID()), 'Nikhil'), (UUID_TO_BIN(UUID()), 'Rita'),
(UUID_TO_BIN(UUID()), 'Sangam'), (UUID_TO_BIN(UUID()), 'Hari');
You can see that the employee id having BINARY Data type has the 16-bit digits in the table.
To query info from a UUID column from the table, we will apply the function BIN_TO_UUID() that transforms the binary data value to human readable form as follows.
Code:
SELECT BIN_TO_UUID(Employee_ID) ID , Employee_Name FROM Employees;
The table displays the UUID values in a human-readable character structure for the Primary key column. This transformation from binary digits to a readable format ensures the security of the main values.
Conclusion
While UUID () values are designed to be distinct, they may not be unpredictable. We can create the UUID values further if unpredictability is mandatory. Hence, we have seen how to apply UUID for a column with a Primary key. Applying UUID values in a URL is a safer approach, and they can be generated without requiring a round trip to the MySQL database server. This makes UUIDs suitable for use in various scenarios.
Recommended Articles
We hope that this EDUCBA information on “MySQL UUID” was beneficial to you. You can view EDUCBA’s recommended articles for more information.