MySQL trigger to update a field to the value of id

前端 未结 6 510
臣服心动
臣服心动 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:25

    I don't know of any way to do this in one statement, even using a trigger.

    The trigger solution that @Lucky suggested would look like this in MySQL:

    CREATE TRIGGER MyTrigger BEFORE INSERT ON MyTable
    FOR EACH ROW BEGIN
      SET NEW.group_id = COALESCE(NEW.group_id, NEW.id);
    END
    

    However, there's a problem. In the BEFORE INSERT phase, the auto-generated id value hasn't been generated yet. So if group_id is null, it defaults to NEW.id which is always 0.

    But if you change this trigger to fire during the AFTER INSERT phase, so you have access to the generated value of NEW.id, you can't modify column values.

    MySQL doesn't support expressions for the DEFAULT of a column, so you can't declare this behavior in the table definition either. *Update: MySQL 8.0.13 supports DEFAULT (<expression>) but the expression still can't depend on an auto-increment value (this is documented).

    The only solution is to do the INSERT, and then immediately do an UPDATE to change the group_id if it's not set.

    INSERT INTO MyTable (group_id, value) VALUES (NULL, 'a');
    UPDATE MyTable SET group_id = COALESCE(group_id, id) WHERE id = LAST_INSERT_ID();
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-27 21:29

    I am answering here, as in the accepted answer Bill Karwin states:

    In the BEFORE INSERT phase, the auto-generated id value hasn't been generated yet. So if group_id is null, it defaults to NEW.id which is always 0.


    I have an answer for it - for OP (and the visitors to come), here are few points: You cannot update the table from where the trigger gets invoked, for it you'll get Error 1442:

    Error Code: 1442
    Can't update table 'MyTable' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
    

    1.to Update the new row Use BEFORE INSERT ON trigger, this way you can update all the fields for the new row, which can be accessible via NEW operator i.e.

    set NEW.group_id = NEW.id
    

    2.Get auto_increment value before insert:

    SELECT AUTO_INCREMENT FROM information_schema.TABLES WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='MyTable'
    

    To sum up - the trigger SQL for 'd be something as following:

    DELIMITER //
    DROP TRIGGER IF EXISTS MyTrigger//
    CREATE TRIGGER MyTrigger BEFORE INSERT ON MyTable
    FOR EACH ROW BEGIN
        IF new.group_id IS NULL
            set @auto_id := (SELECT AUTO_INCREMENT FROM INFORMATION_SCHEMA.TABLES
                             WHERE TABLE_NAME='MyTable' AND TABLE_SCHEMA=DATABASE() ); 
            set NEW.group_id = @auto_id;
        ENF IF;
    END;
    //
    DELIMITER ;
    
    0 讨论(0)
  • 2020-11-27 21:30

    This works for me

    DELIMITER $$
    CREATE TRIGGER `myTriggerNameHere`
    BEFORE INSERT ON `table` FOR EACH ROW
    BEGIN
        SET NEW.group_id = IF(NEW.group_id IS NULL, LAST_INSERT_ID()+1, NEW.group_id);
    END;
    $$
    DELIMITER ;
    
    0 讨论(0)
  • 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'
    )
    
    0 讨论(0)
  • 2020-11-27 21:49

    I believe that this will work for you

    I have two tables

    test_b: a_id, value
    test_c: a_id, value
    

    And here is a trigger on the insert of test b. It checks to see if a_id is null and if it is it inserts 0

    CREATE TRIGGER test_b AFTER INSERT ON test_b
      FOR EACH ROW 
        INSERT INTO test_c (a_id, value) VALUES (IFNULL(NEW.a_id, 0),NEW.value)
    
    0 讨论(0)
提交回复
热议问题