Table of contents
1.
Introduction
2.
UUID In MySQL
2.1.
Structure Of UUID
2.2.
Generating UUID values in MySQL
3.
UUID vs AUTO_INCREMENT
3.1.
Advantages
3.2.
Disadvantages
3.3.
Overcoming UUID Disadvantages
3.3.1.
UUID_TO_BIN
3.3.2.
BIN_TO_UUID
3.3.3.
IS_UUID
4.
UUID() vs UUID(short)
5.
FAQs
6.
Key Takeaways
Last Updated: Mar 27, 2024

MySQL UUID

Author Pakhi Garg
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

You must now be familiar with MySQL, its features and its functionality. In this blog, we will be exploring a new component of MySQL, i.e. UUID

UUID stands for Universal Unique Identifier specified by RFC 4122. RFC stands for Request for Comments. A series of publications issued by the Internet's primary technical development and standards-setting bodies, most notably the Internet Engineering Task Force (IETF).

In this blog, we will study UUID in detail.

Let’s get started.

UUID In MySQL

As said above, UUID is a Universal Unique Identifier. It is a 128-bit long value. The primary benefit of using UUID is that it always generates a unique number. This random number is neither predictable nor guessable and is always unique according to space and time.

Source: Memes Monkey

Now, let’s see the structure of UUID.

Structure Of UUID

Whenever we call UUID, it returns a 128-bit long number. It is represented as a UTF8 string in a human-readable format with five hexadecimal numbers separated by hyphens (-). 

Below is the format of UUID.

aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee

Let’s understand this format.

  • The first three (aaaaaaaa-bbbb-cccc) represent the timestamp format such as low, middle and high. The UUID version number is contained in the high part of this format.
  • The fourth(dddd) is in charge of preserving temporal uniqueness when the timestamp loses monotonicity.
  • The fifth(eeeeeeeeeeee) is the IEEE 802 node number, which represents spatial uniqueness. If the letter is not available, a random number will be used, which does not guarantee spatial uniqueness.

Now, let’s see some examples of UUID.

8ac9defa-8b63-11ec-9c44-8c16450c827b
959bcb2d-8b63-11ec-9c44-8c16450c827b

Now, we will see how to generate UUID values in MySQL.

Generating UUID values in MySQL

We can also generate UUID values in MySQL. We just need to use the SELECT statement for UUID.

Below is the syntax of generating UUID.

SELECT UUID();

The above command calls the UUID function. This function takes no parameter and returns a Universal Unique Identifier number.

Let’s run this command on MySQL.

mysql> SELECT UUID();

The output of the above query will be-

Let’s see one more example. But this time, we will call two UUID functions. 

mysql> SELECT UUID() AS UUID1, UUID() AS UUID2;

The output of the above query will be-

As you can see, if we call two UUID simultaneously, we will get two different values.

Since UUID always generates a unique value whenever used, it is often confused with the AUTO_INCREMENT in MySQL.

Note: AUTO_INCREMENT is a function in MySQL that works with numeric data. When a record is inserted into a table with an auto-increment field, it automatically generates sequential numeric values.

Let’s compare UUID and AUTO_INCREMENT.

UUID vs AUTO_INCREMENT

Since both UUID and AUTO_INCREMENT can generate unique values, we can consider UUID as an alternative to AUTO_INCREMENT.  

Let’s discuss the advantages and disadvantages of UUID over AUTO_INCREMENT.

Advantages

Below are the advantages of using UUID over AUTO_INCREMENT.

  • UUID values in MySQL are always unique between tables, databases, and servers. It enables us to merge rows from distributed databases across servers.
  • UUID values are safe. They do not provide information about our data. As a result, our data is safe from hackers. Moreover, we can use UUID in URLs.
  • UUID values can be generated offline, so we do not need to involve the database server every time while generating a UUID.
  • It also makes the logic in the application easier. For instance, if we want to insert data into both the parent and child tables, we must first insert data into the parent table, get the generated id, and then fill the record into the child table. We can generate the PRIMARY KEY value of the parent table using UUID and insert rows into both tables simultaneously.

Now, let’s see the disadvantages of using UUID over AUTO_INCREMENT.

Disadvantages

Below are the advantages of using UUID over AUTO_INCREMENT.

  • UUID takes more space in memory than integers. UUID takes 8-bytes of memory, whereas integers take 4-bytes.
  • UUID makes readability and debugging difficult. For example, it is easier to read and debug the primary key as ‘4’ rather than ‘979257b0-8b69-11ec-9c44-8c16450c827b’.
  • Because of the unordered values and their size, it can also cause performance issues.

As we have discussed the disadvantages of using UUID over AUTO_INCREMENT, what if I say, we can overcome the above-discussed disadvantages using some functions. Yes, you read it right. 

Overcoming UUID Disadvantages

We can overcome the disadvantages of UUID using three functions. These are-

  1. UUID_TO_BIN
  2. BIN_TO_UUID
  3. IS_UUID

The above functions allow us to store UUID values in BINARY (0,1) format, and whenever we want to see these values, we can see them in human-readable (VARCHAR) format.

Note: The above functions can only be used in MySQL 8.0 version or later.

Let’s dive into these functions one by one.

UUID_TO_BIN

In MySQL, the UUID_TO_BIN function converts the UUID values into Binary format. This function is used to store UUID values in databases.

Let’s understand this function using an example.

First, we will create a table Employee having columns emp_id and name. We will declare the emp_id column with data type BINARY and size 16 (because UUID contains 16 digits) and name with data type VARCHAR.

Below is the query for creating the Employee table.

mysql> CREATE TABLE Employee (
        emp_id BINARY(16) PRIMARY KEY,
        name VARCHAR(50)
        );

Now, we will insert some records into this table. But remember, the emp_id column can include only binary values. So, first, we will generate a UUID value, and then we will use the UUID_TO_BIN function to convert the value to binary.

Below is the query for inserting records in the Employee table.

mysql> INSERT INTO Employee VALUES
        (UUID_TO_BIN(UUID()), 'Arjun'),
        (UUID_TO_BIN(UUID()), 'Kalpana'),
        (UUID_TO_BIN(UUID()), 'Kiran'),
        (UUID_TO_BIN(UUID()), 'Veer');

We can see our records using the SELECT statements.

mysql> SELECT * FROM Employee;

The output of the above query will be-

Now, we will see the next function BIN_TO_UUID.

BIN_TO_UUID

In MySQL, the BIN_TO_UUID function is used to convert UUID values from Binary format to human-readable format. This function is generally used for displaying UUID values.

Let’s understand this function using an example.

We will see our above-inserted records using the BIN_TO_UUID function using the following query-

mysql> SELECT BIN_TO_UUID(emp_id), name FROM Employee;

The output of the above query will be-

As you can see, the values of the emp_id column are displayed in human-readable (hexadecimal) format.

Next, we will discuss the IS_UUID function.

IS_UUID

In MySQL, the IS_UUID function is used to validate the string format of UUID. 

Below is the format to use the IS_UUID function.

IS_UUID(string_value);

We have to pass the UUID value (string_value), which we want to check as a parameter to this function.

And this function returns 1 if the string_value is valid and 0 if the string value is invalid. If the argument is NULL, then it returns NULL.

The following are valid strings format of UUID in MySQL.

aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee
aaaaaaaabbbbccccddddeeeeeeeeeeee
{aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee}

Let’s take some examples to understand this function.

Example 1: We will check whether the string ‘aaa6c37b-8c17-11ec-9c44-8c16450c827b’ is a valid UUID value or not.

mysql> SELECT IS_UUID('aaa6c37b-8c17-11ec-9c44-8c16450c827b') AS Valid_or_Not;

The output of the above query will be-

Example 2: We will check whether the string ‘31d6f4398c1811ec9c448c16450c827b’ is a valid UUID value or not.

mysql> SELECT IS_UUID('31d6f4398c1811ec9c448c16450c827b') AS Valid_or_Not;

The output of the above query will be-

Example 3: We will check whether the string ‘{722f5a8e-8c18-11ec-9c44-8c16450c827b}’ is a valid UUID value or not.

mysql> SELECT IS_UUID('{722f5a8e-8c18-11ec-9c44-8c16450c827b}') AS Valid_or_Not;

The output of the above query will be-

Example 4: We will check whether the string ‘7acd798c-daba-6731-4123-8b8c7751’ is a valid UUID value or not.

mysql> SELECT IS_UUID('7acd798c-daba-6731-4123-8b8c7751') AS Valid_or_Not;

The output of the above query will be-

As the given string does not match the format of valid UUID, it is invaild.

Next, we will discuss the difference between UUID() and UUID(short).

UUID() vs UUID(short)

In MySQL, the functions UUID() and UUID(short) are distinct. The following table discusses the key differences between them.

S.No

UUID()

UUID(short)

1. UUID stands for Universal Unique Identifier and is a 128-bit long value. UUID(short) stands for Short Universal Unique Identifier and is a 64-bit unsigned integer. 
2. UUID is called as UUID(). UUID(short) is called as UUID_SHORT().
3. The value returned by UUID consists of the bitwise conglomeration of the server ID, the current timestamp, a few bits and utility bits. The value returned by UUID(short) consists of the bitwise conglomeration of the server ID, a fairly static time component, and a sequentially increasing 24-bit integer.
4. The server ID is 6 bytes long in UUID. The server ID is 1 byte long in UUID(short).

Refer to know about :  Update Query in MySQL

FAQs

  1. What is the structure of UUID in MySQL?
    UUID in MySQL is represented in ‘aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee’ format where ‘aaaaaaaa-bbbb-cccc’ represents the timestamp format, ‘dddd’  is in charge of preserving temporal uniqueness and ‘eeeeeeeeeeee’  is the IEEE 802 node number, which represents spatial uniqueness.
     
  2. How many UUID values can we generate at a time?
    We can generate multiple UUID values at a time. There is no restriction in generating UUID values. We simply need to call the UUID() function as many times as we want.
     
  3. Why do we prefer UUID over AUTO_INCREMENT?
    We prefer UUID over AUTO_INCREMENT since UUID values are always unique between tables, databases, and servers. Whenever we use UUID as the primary key, our data becomes safe from hackers. Moreover, we do not need to connect to the database always for generating UUID values.
     
  4. What is the difference between UUID_TO_BIN and BIN_TO_UUID functions?
    The UUID_TO_BIN function is used to convert the UUID values into Binary format, whereas the BIN_TO_UUID function is used to convert UUID values from Binary format to human-readable format.
     
  5. What is the use of the IS_UUID function?
    In MySQL, the IS_UUID function is used to validate the string format of UUID. We have to pass the UUID value (string_value), which we want to check as a parameter to this function, and this function returns 1 if the string_value is valid and 0 if the string value is invalid. If the argument is NULL, then it returns NULL.

Key Takeaways

In this blog, we talked about UUID in MYSQL. We started with the structure of UUID and how to generate UUID values in MYSQL. Then our discussion went to the difference between UUID and AUTO_INCREMENT, followed by UUID_TO_BIN, BIN_TO_UUID and IS_UUID functions. Finally, we discussed the difference between UUID and UUID(short). 

Attention Ninja!!!!! Don’t stop here. Start your DBMS journey with the DBMS course on Coding Ninjas. To master SQL, solve our Top 100 SQL Problems asked in various company interviews.

Happy Learning!

Live masterclass