RANDOM_BYTES
Description
The RANDOM_BYTES function generates a random byte sequence of specified length. The returned byte sequence is represented as a hexadecimal string.
Syntax
RANDOM_BYTES(<len>)
Parameters
| Parameter | Description |
|---|---|
<len> | Number of random bytes to generate. Type: INT |
Return Value
Returns VARCHAR type, a hexadecimal-encoded random byte sequence (prefixed with 0x).
Special cases:
<len>must be greater than 0, otherwise returns error- If parameter is NULL, returns NULL
- Each invocation generates a random result
Examples
- Basic usage: Generate 8-byte random sequence
SELECT random_bytes(8);
+--------------------+
| random_bytes(8) |
+--------------------+
| 0x1a2b3c4d5e6f7089 |
+--------------------+
- Generate short sequence
SELECT random_bytes(4);
+----------------+
| random_bytes(4) |
+----------------+
| 0xab12cd34 |
+----------------+
- Invalid parameter: Negative number
SELECT random_bytes(-1);
ERROR 1105 (HY000): errCode = 2, detailMessage = (10.16.10.3)[INVALID_ARGUMENT]argument -1 of function random_bytes at row 0 was invalid.
- NULL value handling
SELECT random_bytes(NULL);
+--------------------+
| random_bytes(NULL) |
+--------------------+
| NULL |
+--------------------+
- Generate longer sequence (16 bytes)
SELECT random_bytes(16);
+------------------------------------+
| random_bytes(16) |
+------------------------------------+
| 0x1a2b3c4d5e6f708192a3b4c5d6e7f809 |
+------------------------------------+
Keywords
RANDOM_BYTES