Christopher_L1n
Christopher_L1n
2017-03-21 07:09
采纳率: 100%
浏览 767

关于mysql触发器的问题

现在我经常会调用delete from A where state='500';这条命令
然后在A表创建一个触发器,在'delete'之前,执行插入数据,把要删除的数据插入另一个表中。这可行吗?要怎么实现?
还有什么更好的方法可以实现?

刚学数据库,不太会,可能描述不清楚,谢谢各位大牛抽出时间解答。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

5条回答 默认 最新

  • Derica88
    Derica88 2017-03-21 07:35
    已采纳

    一条 或 多条触发

    create trigger [A_delete]
    on [dbo].[A]
    for delete
    as

    begin distributed transaction
    SET XACT_ABORT on

    IF EXISTS(SELECT 1 FROM inserted) AND NOT EXISTS(SELECT 1 FROM deleted)
    BEGIN

        Insert into B (字段名) 
        select 字段名 from inserted order by ID asc;
    

    end
    commit tran

    点赞 评论
  • lrx2011
    podd 2017-03-21 08:56

    可以实现的,你参考上面的去试试,不过上面是sqlserver的吧,可以参考这个区改改

    点赞 评论
  • lrx2011
    podd 2017-03-21 08:57
    点赞 评论
  • smile_Arielle
    从微笑开始幸福 2017-03-21 09:56

    BEGIN

    IF id IS NULL THEN

    INSERT INTO B VALUES();

    END
    IF;

    写的时候要勾选before delete!
    图片说明
    大概就是这样,多调一下就好了

    点赞 评论
  • ljheee
    ljheee 2017-03-24 05:13

    create trigger [A_delete]
    on [dbo].[A]
    for delete
    as

    begin distributed transaction
    SET XACT_ABORT on

    IF EXISTS(SELECT 1 FROM inserted) AND NOT EXISTS(SELECT 1 FROM deleted)
    BEGIN

    Insert into B (字段名) 
    select 字段名 from inserted order by ID asc;
    

    end
    commit tran

    点赞 评论

相关推荐