views:

25

answers:

1

I want a column to default to a GUID, so if I am doing an insert and I don't explicitly set the value, I want it to default to a new GUID value.

how can I do this?

+1  A: 

Being that UUID() isn't accepted as a DEFAULT constraint, you need to use a trigger.

This one sets the value for the NEW_TABLE.uuid column:

delimiter $$

CREATE
DEFINER=`root`@`localhost`
TRIGGER `example`.`newid`
BEFORE INSERT ON `example`.`new_table`
FOR EACH ROW
BEGIN
  SET NEW.`uuid` = UUID();
END
$$
OMG Ponies
a trigger? man that's ugly :) thanks!
Blankman