Skip to content

Instantly share code, notes, and snippets.

@ArtskydJ
Created January 26, 2024 19:47
Show Gist options
  • Save ArtskydJ/279de72bcbea06c925de655b1255531a to your computer and use it in GitHub Desktop.
Save ArtskydJ/279de72bcbea06c925de655b1255531a to your computer and use it in GitHub Desktop.

https://stackoverflow.com/questions/46134550/mysql-set-default-id-uuid

CREATE TABLE foo_uuid (
  guid CHAR(32)  DEFAULT (UUID())
);

problem: that's UUIDv1 which is time-based.

workaround: doesn't need to be an actual UUIDv4. Get random bytes, and convert that to a format that works with a URL.

CREATE TABLE foo_uuid (
  guid CHAR(32)  DEFAULT (HEX(RANDOM_BYTES(16)))
);

HEX turns 1 byte into 2 characters, and needs no URL-escaping.

TO_BASE64 turns 3 bytes into 4 characters, but needs URL-escaping.

SELECT HEX(RANDOM_BYTES(16)), LENGTH(HEX(RANDOM_BYTES(16))), TO_BASE64(RANDOM_BYTES(16)), LENGTH(TO_BASE64(RANDOM_BYTES(16)));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment