MySQL column that auto generates data, longer than UUID

google-cloud-sqlMySQLuuid

My friend has the problem as shown below. He asked at StackOverFlow and there is no reply yet till now so I wish to help him to try the luck here. Thanks for everyone that helps.

"I would like to ask if its possible to set up a database table that has a column that auto generates random alphanumerical data that is longer that what UUID can do. I've been using UUID for a while now but I would like to have a longer string of random data in my columns, something similar to that of a token authenticator (around 300+ characters). So when I insert values in columns, this particular column will auto generate data by itself. Thanks in advance."

Quoted from Reuben Tan
Source: https://stackoverflow.com/questions/43461771/mysql-column-that-auto-generates-data-longer-than-uuid

Best Answer

You can create the pair of triggers ON INSERT and ON UPDATE that perform any desired transformations 'on the fly'

CREATE TRIGGER `myuuid_autoinserter` 
BEFORE INSERT ON `table` -- or BEFORE UPDATE
FOR EACH ROW 
BEGIN
  SET loooonguuid = CONCAT(OLD.uuid, OLD.uuid, OLD.uuid, OLD.uuid)
  SET NEW.uuid = loooonguuid;
END

When you run some INSERT or UPDATE query on the table trigger will replace submitted value of the uuid by string stored in the loooonguuid variable.

Sure you can do as complex and randomized transformation as you want.