Introduction to MySQL UUID
MySQL UUID is defined as “Universally Unique Identifier” which shows the usage of a Primary key for a table. Basically, it is described on the basis of RFC 4122 that is a URN Namespace i.e. Universally Unique Identifier (UUID). Here, we will show UUID as PK (Primary Key) and its pros and cons while application of it in MySQL query. UUID is considered as a numeral which is distinct worldwide in universe and time interval. If UUID are produced on two independent severs that are associated to each other then also the both UUID values are projected to be unique.
Syntax of MySQL UUID
A UUID value is defined as a 128-bit numeral that signifies an utf8 string of a five hexadecimal digits which has the succeeding 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.
This can be illustrated by using the UUID () function as follows:
SELECT UUID();
Output:
It should be noted that the function is insecure for application based on statement. If this function is implemented whenever the binlog_format is fixed to statement then, 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 comparing to Primary Key with AUTO_INCREMENT INT attributes in database.
Advantages that MySQL UUID for a primary key conveys are listed below:
- The UUID values are distinct across databases, tables and also servers which let us to unite rows from several records or say distributed databases across MySQL servers.
- UUID do not describe the info about any data therefore they are more secure to be used for URL. Assume that if an employee having employee id 05 is accessing his/her 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. and so it will provide a target for any harmful attack to breach security and increase risk.
- To evade a round trip to the database server, UUID values can be produced anywhere. Similarly, it is a bridges logic in the application. Let us assume that to input records into a parent table as well as child tables then we need to insert into the base table i.e. parent table initially, receive the produced id and next the data will be inserted into the child tables. Thus, by applying UUID, we can create the value of primary key of the base table first and input rows at identical time into both parent and child tables within a transaction.
Drawbacks are briefed below beside the pros of using UUID relating a Primary key:
- If we use integers i.e. 4 bytes or also 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. Like the expression WHERE Employee_ID = 05 is easy instead of WHERE Employee_ID = ‘fd7bc2fb-9ve4-10t7-49yy-p9vbc73cbxyf’.
- When UUID values are used it may raise performance issues because of their size and unordered form.
UUID Solution in MySQL as follows:
MySQL provides you the option to store the UUID values in BINARY format or say compress form and can be displayed in human readable i.e. VARCHAR format.
This can be achieved by using the following MySQL functions related to UUID:
- UUID_TO_BIN: This function is responsible to convert a UUID value from VARCHAR format which is human readable into a BINARY format which is compacted one for storing.
- BIN_TO_UUID: It is just opposite to the previous one. BIN_TO_UUID function changes the UUID from 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 then, the function gives 0 as 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 }
It should be remembered 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 provided a gab in time interval for 3 seconds with the SLEEP() in MySQL.
Code:
SELECT UUID(); do sleep(3); SELECT UUID();
Output:
Now, after that again when we have executed the UUID value then, it is found to be different like this.
Example #2
We will create a fresh table in our database initially using the CREATE statement query as follows.
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 few UUID values into the column Employee_ID in the Employees table.
For implementing this we will also use the functions UUID() and UUID_TO_BIN() with the help of 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 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.
Now, 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;
Here, you can view that the UUID values are displayed for the Primary key column values in the table from binary digits to human character readable structure that secures the main values.
Conclusion
Though the UUID () values are planned to be distinct but they may not be essentially unpredictable one. We can create the UUID values in some further way if unpredictability is mandatory. Hence, we have seen how to apply UUID for a column with Primary key. As the UUID values are safer to be applied in a URL and can be produced anyplace that escapes a round trip to the MySQL database server.
Recommended Articles
This is a guide to MySQL UUID. Here we discuss the introduction to MySQL UUID, how UUID works, along with respective query examples. You may also have a look at the following articles to learn more –
12 Online Courses | 10 Hands-on Projects | 92+ Hours | Verifiable Certificate of Completion
4.5
View Course
Related Courses