Trending September 2023 # How Uuid Works In Mysql # Suggested October 2023 # Top 16 Popular |

Trending September 2023 # How Uuid Works In Mysql # Suggested October 2023 # Top 16 Popular

You are reading the article How Uuid Works In Mysql updated in September 2023 on the website We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested October 2023 How Uuid Works In Mysql

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.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

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:


For generating the UUID values, we need to apply the UUID function shown below:


We can illustrate this by utilizing the UUID() function as follows:



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 UUID values are distinct across databases, tables, and servers, allowing us to unite rows from several records or distributed databases across MySQL servers.

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.

SELECT UUID(); do sleep(3); SELECT UUID();


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.


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.


INSERT INTO Employees (Employee_ID, Employee_Name) VALUES ('1','Nikhil'), ('2', 'Rita'),('3', 'Sangam'), ('4', 'Hari');


SELECT * FROM Employees;


Or, you can use this query with a function in MySQL version 8.0 and above.


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.


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.


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.

You're reading How Uuid Works In Mysql

Update the detailed information about How Uuid Works In Mysql on the website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!