MariaDB UUID() Function

In MariaDB, UUID() is a built-in function that returns a Universally Unique Identifier (UUID) generated according to RFC 4122.

UUIDs are generated from the “DCE 1.1: Remote Procedure Calls” (Appendix A) CAE (Common Application Environment) specification (Document #C706), published by The Open Group, October 1997.

MariaDB UUID() Syntax

Here is the syntax of the MariaDB UUID() function:

UUID()

Parameters

The MariaDB UUID() function do not have any parameters.

Return value

The MariaDB UUID() function returns a Universally Unique Identifier (UUID) generated according to RFC 4122.

The returned value by UUID() conforms to UUID version 1 as described in RFC 4122. The value is a 128-bit number represented as a utf8 string format aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee:

  • The first three numbers are generated from the low, middle, and high parts of the timestamp. The high-order portion also includes the UUID version number.

  • The fourth number preserves time uniqueness in case the timestamp value loses monotonicity (for example, due to daylight saving time).

  • The fifth number is the IEEE 802 node number, which provides spatial uniqueness. If the latter is not available (for example, because the host device doesn’t have an ethernet card, or doesn’t know how to find the hardware address of the interface on the host OS), a random number is used instead. In this case, spatial uniqueness cannot be guaranteed. However, the probability of collision should be very low .

    The MAC address of the interface is only used on FreeBSD, Linux and Windows. On other operating systems, MySQL uses randomly generated 48-bit numbers.

MariaDB UUID() Examples

The following example shows how to use the UUID() function to obtain a unique identifier.

SELECT UUID();

Output:

+--------------------------------------+
| UUID()                               |
+--------------------------------------+
| bd9677dc-a10c-11ed-ac31-18c04d19fce5 |
+--------------------------------------+

If I call it again, I get different values:

SELECT UUID();

Output:

+--------------------------------------+
| UUID()                               |
+--------------------------------------+
| c3db2503-a10c-11ed-ac31-18c04d19fce5 |
+--------------------------------------+

Even if you call UUID() twice in the same statement, different values ​​will be reached:

SELECT
  UUID(),
  UUID()\G

Output:

UUID(): ca49cd73-a10c-11ed-ac31-18c04d19fce5
UUID(): ca49cd76-a10c-11ed-ac31-18c04d19fce5

SYS_GUID() vs UUID() vs UUID_SHORT()

The following example shows the difference between SYS_GUID(), UUID(), and UUID_SHORT():

SELECT
  UUID_SHORT(),
  UUID(),
  SYS_GUID()\G

Output:

UUID_SHORT(): 100158760672034822
      UUID(): 56884e1f-a10d-11ed-ac31-18c04d19fce5
  SYS_GUID(): 56884e24a10d11edac3118c04d19fce5

Conclusion

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