default value of GUID in for a column in mysql

前端 未结 2 1612
甜味超标
甜味超标 2020-12-16 16:53

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?<

2条回答
  •  一向
    一向 (楼主)
    2020-12-16 17:19

    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
    $$
    

提交回复
热议问题