Insert and delete data in table using before insert trigger

杀马特。学长 韩版系。学妹 提交于 2019-12-25 02:25:12

问题


I have created a trigger which inserts the last record of conversation table in conversation_h table, and delete that record from conversation using before insert event.

Any other logic or code is also welcome but i want achieve the above using trigger only.

DELIMITER $$
CREATE
    TRIGGER `transfer_data` BEFORE INSERT 
    ON `conversation`

    FOR EACH ROW 

BEGIN

if  (select count(*) from `conversation`) > 2
then 

  set @mid = (select id from conversation order by id asc limit 0,1);

insert into conversation_h select * from conversation where id=@mid;

delete from conversation where id=@mid;


    END IF;

    END$$
DELIMITER ;

回答1:


This is not possible with triggers:

A (...) trigger cannot modify a table that is already being used (for reading or writing) by the statement that invoked the (...) trigger.



来源:https://stackoverflow.com/questions/21116463/insert-and-delete-data-in-table-using-before-insert-trigger

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!