MySQL trigger to update a field to the value of id

前端 未结 6 590
臣服心动
臣服心动 2020-11-27 21:21

I would like to have a trigger to perform following operation for inserted records:

 # pseudocode
 if new.group_id is null
    set new.group_id = new.id
 els         


        
6条回答
  •  一向
    一向 (楼主)
    2020-11-27 21:26

    This trigger should do what you asked.

       CREATE TRIGGER mytrigger BEFORE INSERT ON mytable
              IF new.group_id IS NULL
                SET new.group_id = new.id
              END IF
          END;
    

    It's copied from a very similar example in the MYSQL documentation page.

提交回复
热议问题