MySQL UUID() Function
In MySQL, the UUID()
function generates a Universally Unique Identifier (UUID) according to RFC 4122 and returns it.
UUID is a globally unique value. Any two calls to the UUID()
function should produce different values.
UUID()
Syntax
Here is the syntax of the MySQL UUID()
function:
UUID()
Parameters
MySQL UUID()
function does not have any parameters.
Return value
The MySQL 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.
You can use UUID_TO_BIN()
and BIN_TO_UUID()
to convert between string and binary values โโof UUIDs.
You can use the IS_UUID
function to check if a string is a valid UUID value.
UUID()
Examples
Here is the basic usage of MySQL UUID()
function.
SELECT UUID();
+--------------------------------------+
| UUID() |
+--------------------------------------+
| d114115a-ce6a-11ec-8607-63ec778e6346 |
+--------------------------------------+