UUID() - Generating UUID String

Q

How to generate a UUID (Universal Unique IDentifier) string using the UUID() function?

✍: FYIcenter.com

A

UUID() is a MySQL built-in function that returns a UUID (Universal Unique IDentifier) string. For example:

SELECT UUID(), UUID();
  -- +--------------------------------------+--------------------------------------+
  -- | UUID()                               | UUID()                               |
  -- +--------------------------------------+--------------------------------------+
  -- | 447f9022-956d-11ee-b1eb-65d6038dbafa | 447f902c-956d-11ee-b1eb-65d6038dbafa |
  -- +--------------------------------------+--------------------------------------+

SELECT UUID(), SLEEP(3), UUID();
  -- +--------------------------------------+----------+--------------------------------------+
  -- | UUID()                               | SLEEP(3) | UUID()                               |
  -- +--------------------------------------+----------+--------------------------------------+
  -- | cbd1deb6-956f-11ee-b1eb-65d6038dbafa |        0 | cd9c631a-956f-11ee-b1eb-65d6038dbafa |
  -- +--------------------------------------+----------+--------------------------------------+

Reference information of the UUID() function:

UUID(): uuid
  Returns a UUID string using the current time and host information.

Arguments, return value and availability:
  uuid: Return value. The generated UUID.
  Available since MySQL 5.7.

UUID() function returns a UUID value that that conforms to UUID version 1 as described in RFC 4122. The value is a 128-bit number represented in 32 hexadecimal digits divided into 5 parts as in "aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee" format, where:

  • Part 1: "aaaaaaaa" - Generated from the low component of the current timestamp, most likely the second and nanosecond level.
  • Part 2: "bbbb" - Generated from the middle component of the current timestamp, most likely the minutes and hour level.
  • Part 3: "cccc" - Generated from the high component of the current timestamp, most likely the month and year level.
  • Part 4: "dddd" - Additional temporal information in case the timestamp is unique due to daylight saving time adjustments, most likely the time information.
  • Part 5: "eeeeeeeeeeee" - Generated from the host identification, most likely the MAC address.

 

UUID_SHORT() - Short 64-Bit UUID Integer

IS_UUID() - Validating UUID String Format

MySQL Functions on UUID Values

⇑⇑ MySQL Function References

2023-12-08, 260🔥, 0💬