MySQL trigger to update a field to the value of id

前端 未结 6 607
臣服心动
臣服心动 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条回答
  •  旧时难觅i
    2020-11-27 21:38

    A trigger seems like overkill in this situation. Simply apply a default.

    CREATE TABLE `test` (
        `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
        `value` varchar(100) NOT NULL,
        `group_id` TINYINT(3) UNSIGNED NOT NULL DEFAULT '2'
    )
    

提交回复
热议问题