How UUID() Works in MariaDB

In MariaDB, UUID() is a built-in function that returns a Universal Unique Identifier (UUID).

The UUID is generated according to “DCE 1.1: Remote Procedure Call” (Appendix A) CAE (Common Applications Environment) Specifications published by The Open Group in October 1997 (Document Number C706).

Syntax

The syntax goes like this:

UUID()

So, no arguments are required (or accepted).

Example

Here’s an example to demonstrate:

SELECT UUID();

Result:

+--------------------------------------+
| UUID()                               |
+--------------------------------------+
| e5aa36ee-cd8a-11eb-be04-88e9fe739f3d |
+--------------------------------------+

If I call it another time, I get a different value:

SELECT UUID();

Result:

+--------------------------------------+
| UUID()                               |
+--------------------------------------+
| f11b4702-cd8a-11eb-be04-88e9fe739f3d |
+--------------------------------------+

About the UUID

A UUID (Universal Unique Identifier) is a number that is designed to be globally unique in space and time. Two calls to UUID() are expected to generate two different values, even if these calls are performed on two separate computers that are not connected to each other.

A UUID is a 128-bit number represented by a utf8 string of five hexadecimal numbers in aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee format:

  • The first three numbers are generated from a timestamp.
  • The fourth number preserves temporal uniqueness in case the timestamp value loses monotonicity (for example, due to daylight saving time).
  • The fifth number is an IEEE 802 node number that provides spatial uniqueness. A random number is substituted if the latter is not available. In such cases, spatial uniqueness cannot be guaranteed. Having said that, a collision should be an extremely low probability event.

Note that statements using the UUID() function are not safe for statement-based replication.

Compared to the SYS_GUID() Function

The result returned by UUID() is similar to the one returned by the SYS_GUID() function, except that SYS_GUID() doesn’t include the hyphen symbol (-) in the result (whereas, UUID() does).

Here’s a comparison of the two:

SELECT 
    UUID(),
    SYS_GUID();

Result:

+--------------------------------------+----------------------------------+
| UUID()                               | SYS_GUID()                       |
+--------------------------------------+----------------------------------+
| 9e795ffc-cd71-11eb-8f75-0800270503a7 | 9E796001CD7111EB8F750800270503A7 |
+--------------------------------------+----------------------------------+

The SYS_GUID() function was introduced in MariaDB 10.6.1 to enhance Oracle compatibility.

There’s also a UUID_SHORT() function that returns a short UUID as a 64-bit unsigned integer.

Passing Arguments

As mentioned, UUID() doesn’t accept any arguments. Here’s what happens when I pass an argument:

SELECT UUID(3);

Result:

ERROR 1582 (42000): Incorrect parameter count in the call to native function 'UUID'