mysql 触发器

 插入触发器:(snake_order表中有数据插入会同步插入到snake_order_new 中)

DELIMITER //

CREATE TRIGGER sync_insert_snake_order
AFTER INSERT ON snake_order
FOR EACH ROW
BEGIN
  INSERT INTO snake_order_new (user_id, pack_file, pack_note, create_time, update_time, ...)
  VALUES (NEW.user_id, NEW.pack_file, NEW.pack_note, NEW.create_time, NEW.update_time, ...);
END;

//

DELIMITER ;

更新触发器: (snake_order表中有数据更新会同步更新到snake_order_new 中)

DELIMITER //

CREATE TRIGGER sync_update_snake_order
AFTER UPDATE ON snake_order
FOR EACH ROW
BEGIN
  UPDATE snake_order_new
  SET user_id = NEW.user_id, pack_file = NEW.pack_file, pack_note = NEW.pack_note, update_time = NEW.update_time, ...
  WHERE pack_id = NEW.pack_id;
END;

//

DELIMITER ;

删除触发器:snake_order表中有数据删除会同步删除snake_order_new 中)

DELIMITER //

CREATE TRIGGER sync_delete_snake_order
BEFORE DELETE ON snake_order
FOR EACH ROW
BEGIN
  DELETE FROM snake_order_new WHERE pack_id = OLD.pack_id;
END;

//

DELIMITER ;

你可能感兴趣的:(mysql,数据库)