MySQL inserting UUID to a binary(16) field

It is advantageous to store a UUID (acc to RFC 4122) in a binary(16) field in the database.
It is not hard to create such a table and to insert data into it.

CREATE TABLE IF NOT EXISTS test_table (
  id BINARY(16) NOT NULL,
  name VARCHAR(128) NOT NULL,
  PRIMARY KEY  (id)
) 
 
INSERT INTO test_table (id, name) VALUES
(UNHEX(REPLACE(UUID(),'-','')), 'test1'),
(UNHEX(REPLACE(UUID(),'-','')), 'test2'),
(UNHEX(REPLACE(UUID(),'-','')), 'test3')

One thought on “MySQL inserting UUID to a binary(16) field

  1. Pingback: PHP: converting a uuid stored as BINARY(16) in mysql to RFC 4122 format « Jontas

Comments are closed.