In MariaDB, UUID_SHORT()
is a built-in function that returns a short Universal Unique Identifier (UUID).
It returns a 64-bit unsigned integer (as opposed to the string-form 128-bit identifier as returned by the UUID()
function).
Syntax
The syntax goes like this:
UUID_SHORT()
So, no arguments are required (or accepted).
Example
Here’s an example to demonstrate:
SELECT UUID_SHORT();
Result:
+-------------------+ | UUID_SHORT() | +-------------------+ | 99291717236162560 | +-------------------+
If I call it another time, I get a different value:
SELECT UUID_SHORT();
Result:
+-------------------+ | UUID_SHORT() | +-------------------+ | 99291717236162561 | +-------------------+
About the Result
The value returned by UUID_SHORT()
is guaranteed to be unique if the following conditions are true:
- The
server_id
of the current host is unique among your set of master and slave servers server_id
is between0
and255
- You don’t set back your system time for your server between
mysqld
restarts - You do not invoke
UUID_SHORT()
on average more than 16 million times per second betweenmysqld
restarts
Note that statements using the UUID_SHORT()
function are not safe for statement-based replication.
Compared to the UUID()
Function
The result returned by UUID_SHORT()
is similar to the one returned by the UUID()
function, except that the result returned by UUID()
is a 128-bit number represented by a utf8 string of five hexadecimal numbers in aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee
format.
Here’s a comparison of the two:
SELECT
UUID(),
UUID_SHORT();
Result:
+--------------------------------------+-------------------+ | UUID() | UUID_SHORT() | +--------------------------------------+-------------------+ | e67d0fc6-cd8d-11eb-be04-88e9fe739f3d | 99291717236162562 | +--------------------------------------+-------------------+
In addition to this, from MariaDB 10.6.1, a SYS_GUID()
function has been added in order to enhance Oracle compatibility.
No Arguments
As mentioned, UUID_SHORT()
doesn’t accept any arguments. Here’s what happens when I pass an argument:
SELECT UUID_SHORT(3);
Result:
ERROR 1582 (42000): Incorrect parameter count in the call to native function 'UUID_SHORT'