The RANDOM_BYTES function generates a random byte sequence of specified length. The returned byte sequence is represented as a hexadecimal string.
RANDOM_BYTES(<len>)
| Parameter | Description |
|---|---|
<len> | Number of random bytes to generate. Type: INT |
Returns VARCHAR type, a hexadecimal-encoded random byte sequence (prefixed with 0x).
Special cases:
<len> must be greater than 0, otherwise returns errorSELECT random_bytes(8);
+--------------------+ | random_bytes(8) | +--------------------+ | 0x1a2b3c4d5e6f7089 | +--------------------+
SELECT random_bytes(4);
+----------------+ | random_bytes(4) | +----------------+ | 0xab12cd34 | +----------------+
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.
SELECT random_bytes(NULL);
+--------------------+ | random_bytes(NULL) | +--------------------+ | NULL | +--------------------+
SELECT random_bytes(16);
+------------------------------------+ | random_bytes(16) | +------------------------------------+ | 0x1a2b3c4d5e6f708192a3b4c5d6e7f809 | +------------------------------------+
RANDOM_BYTES