So, as a response to comments. The correct way to store a 36-char UUID as binary(16) is to perform the insert in a manner like:
INSERT INTO sometable (UUID) VALUES
(UNHEX(REPLACE("3f06af63-a93c-11e4-9797-00505690773f", "-","")))
UNHEX
because an UUID is already a hexed value. We trim (REPLACE
) the dashes in the statement to bring the length down to 32 characters (our 16 bytes represented as HEX
). You can do this at any point before storing it, obviously, so it doesn't have to be handled by the database.
You may retrieve the UUID like this:
SELECT HEX(UUID) FROM sometable;
Just in case someone comes across this thread and is unsure how this works.
And remember: If you're selecting a row using the UUID, use UNHEX()
on the condition:
SELECT * FROM sometable WHERE UUID = UNHEX('3f06af63a93c11e4979700505690773f');
or literal notation (as mentioned by Alexis Wilke):
SELECT * FROM sometable WHERE UUID = 0x3f06af63a93c11e4979700505690773f;
And NOT HEX()
on the column:
SELECT * FROM sometable WHERE HEX(UUID) = '3f06af63a93c11e4979700505690773f';
The last solution, while it works, requires that MySQL HEX
es all UUIDs before it can determine which rows match. It's very inefficient.
Edit: If you're using MySQL 8 you should have a look at the UUID functions as mentioned in SlyDave's answer. This answer is still correct, but it doesn't optimise the UUID indexes which can be done natively using those functions. If you're on < MySQL 8 you can implement Devon's polyfill, which provides identical functionality on previous versions of MySQL.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…