UUID()

Description

Returns a Universal Unique Identifier (UUID) generated according to RFC 4122, “A Universally Unique IDentifier (UUID) URN Namespace”.

A UUID is designed as a number that is 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 devices not connected to each other.

Info

Although UUID() values are intended to be unique, they are not necessarily unguessable or unpredictable. If unpredictability is required, UUID values should be generated some other way.

UUID() returns a value that conforms to UUID version 1 as described in RFC 4122. The value is a 128-bit number represented as a utf8mb3 string of five hexadecimal numbers in aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee format:

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

  • 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 (for example, because the host device has no Ethernet card, or it is unknown how to find the hardware address of an interface on the host operating system). In this case, spatial uniqueness cannot be guaranteed. Nevertheless, a collision should have very low probability.

Syntax

  1. > UUID()

Examples

  1. drop table if exists t1;
  2. create table t1(a INT, b float);
  3. insert into t1 values(12124, -4213.413), (12124, -42413.409);
  4. mysql> SELECT length(uuid()) FROM t1;
  5. +----------------+
  6. | length(uuid()) |
  7. +----------------+
  8. | 36 |
  9. | 36 |
  10. +----------------+
  11. 2 rows in set (0.00 sec)
  12. mysql> SELECT UUID();
  13. +--------------------------------------+
  14. | uuid() |
  15. +--------------------------------------+
  16. | b293b688-70a7-11ed-a25a-5ad2460dea50 |
  17. +--------------------------------------+
  18. 1 row in set (0.00 sec)

Constraints

UUID() does not support optional argument for now, which means it doesn’t support UUID([number]).