Cross database trigger in Mysql

后端 未结 2 1302
一个人的身影
一个人的身影 2021-01-22 04:09

Is it possible to apply trigger for cross database access in MySQL If so please give one example. My purpose is to insert/update/delete data in database2 if there is any new dat

2条回答
  •  故里飘歌
    2021-01-22 04:38

    Yes, you can. You could make a procedure and call it in your trigger. Procedure example :

    DELIMITER //
    
    CREATE PROCEDURE delete(in table VARCHAR(300), in db VARCHAR(300), in id INT)
    BEGIN
    
    set @query0 = CONCAT('DELETE FROM ', new_db, '.', tabela, ' WHERE id=',id);
    
    PREPARE select_query0 FROM @query0;
    EXECUTE select_query0;
    DEALLOCATE PREPARE select_query0;
    
    END; //
    
    DELIMITER ;
    

    And then to create the trigger:

    CREATE TRIGGER del_trigger BEFORE DELETE ON table
      FOR EACH ROW BEGIN
        CALL delete(db, table, OLD.id); 
      END;
    

提交回复
热议问题