Is possible to do a ROLLBACK in a MySQL trigger?

前端 未结 3 2017
逝去的感伤
逝去的感伤 2021-01-13 14:22

Just that is the question: is possible to do a ROLLBACK in a MySQL trigger?

If answer is yes, then, please, explain how.

3条回答
  •  自闭症患者
    2021-01-13 15:09

    I've found out that this functionnality exists since MySQL 5.5 and does not work in earlier releases.

    The trigger does no rollback or commit. To initiate any rollback, you have to raise an exception. Thus your insert/update/delete command will abort. The rollback or commit action has to be raised around your SQL command.

    To raise your exception, in your XXX's trigger (eg.) :

    create trigger Trigger_XXX_BeforeInsert before insert on XXX
    for each row begin
    
        if [Test]
        then
    
          SIGNAL sqlstate '45001' set message_text = "No way ! You cannot do this !";
    
        end if ;
    
    end ;
    

提交回复
热议问题