HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlMajor

MySQL - Alter table to automatically put in a UUID

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
automaticallyputmysqluuidaltertable

Problem

Ok, I know I've done this before. But I cannot for the life of me figure it out. I created a table. One of the columns is labled "LogID", it is also the primary key.

How do I alter it so that this column generates a UUID for each new entry?

Thanks

Solution

Just create a trigger to run before insert to generate the UUID for the given column.

CREATE TRIGGER before_insert_mytable
  BEFORE INSERT ON mytable
  FOR EACH ROW
  SET new.LogID = uuid();

Code Snippets

CREATE TRIGGER before_insert_mytable
  BEFORE INSERT ON mytable
  FOR EACH ROW
  SET new.LogID = uuid();

Context

StackExchange Database Administrators Q#57293, answer score: 23

Revisions (0)

No revisions yet.