我是靠谱客的博主 清新玉米,最近开发中收集的这篇文章主要介绍触发器写法,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

CREATE TABLE IF NOT EXISTS `wp_postmeta_log` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `table_name` varchar(15) DEFAULT NULL,
  `action` varchar(15) DEFAULT NULL,
  `meta_id` bigint(20) unsigned NOT NULL,
  `post_id` bigint(20) unsigned NOT NULL DEFAULT '0',
  `meta_key` varchar(255) DEFAULT NULL,
  `meta_value` longtext,
  `startDate` datetime DEFAULT NULL,
  `endDate` datetime DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `post_id` (`post_id`),
  KEY `meta_key` (`meta_key`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=14894 ;

DELIMITER //
/* Trigger for INSERT */
CREATE TRIGGER WpPostMetaInsert AFTER INSERT ON wp_postmeta FOR EACH ROW BEGIN
   DECLARE N DATETIME;
   SET N = now();
 
   INSERT INTO wp_postmeta_log (table_name, action, meta_id, post_id, meta_key, meta_value, StartDate, EndDate)
   VALUES ('wp_postmeta', 'INSERT', NEW.meta_id, NEW.post_id, NEW.meta_key, NEW.meta_value, N, NULL);
END //

/* Trigger for DELETE */
CREATE TRIGGER WpPostMetaDelete AFTER DELETE ON wp_postmeta FOR EACH ROW BEGIN
   DECLARE N DATETIME;
   SET N = now();

   UPDATE wp_postmeta_log
      SET EndDate = N
    WHERE meta_id = OLD.meta_id
      AND EndDate IS NULL;

   INSERT INTO wp_postmeta_log (table_name, action, meta_id, post_id, meta_key, meta_value, StartDate, EndDate)
   VALUES ('wp_postmeta', 'DELETE', OLD.meta_id, OLD.post_id, OLD.meta_key, OLD.meta_value, N, NULL);
END //

/* Trigger for UPDATE */
CREATE TRIGGER WpPostMetaUpdate AFTER UPDATE ON wp_postmeta FOR EACH ROW BEGIN
   DECLARE N DATETIME;
   SET N = now();

   UPDATE wp_postmeta_log
      SET EndDate = N
    WHERE meta_id = OLD.meta_id
      AND EndDate IS NULL;
 
   INSERT INTO wp_postmeta_log (table_name, action, meta_id, post_id, meta_key, meta_value, StartDate, EndDate)
   VALUES ('wp_postmeta', 'UPDATE', NEW.meta_id, NEW.post_id, NEW.meta_key, NEW.meta_value, N, NULL);
END //

/* --- */

/* Trigger for INSERT */
CREATE TRIGGER WpPostMetaInsert2 AFTER INSERT ON wp_2_postmeta FOR EACH ROW BEGIN
   DECLARE N DATETIME;
   SET N = now();
 
   INSERT INTO wp_postmeta_log (table_name, action, meta_id, post_id, meta_key, meta_value, StartDate, EndDate)
   VALUES ('wp_2_postmeta', 'INSERT', NEW.meta_id, NEW.post_id, NEW.meta_key, NEW.meta_value, N, NULL);
END //

/* Trigger for DELETE */
CREATE TRIGGER WpPostMetaDelete2 AFTER DELETE ON wp_2_postmeta FOR EACH ROW BEGIN
   DECLARE N DATETIME;
   SET N = now();

   UPDATE wp_postmeta_log
      SET EndDate = N
    WHERE meta_id = OLD.meta_id
      AND EndDate IS NULL;

   INSERT INTO wp_postmeta_log (table_name, action, meta_id, post_id, meta_key, meta_value, StartDate, EndDate)
   VALUES ('wp_2_postmeta', 'DELETE', OLD.meta_id, OLD.post_id, OLD.meta_key, OLD.meta_value, N, NULL);
END //

/* Trigger for UPDATE */
CREATE TRIGGER WpPostMetaUpdate2 AFTER UPDATE ON wp_2_postmeta FOR EACH ROW BEGIN
   DECLARE N DATETIME;
   SET N = now();

   UPDATE wp_postmeta_log
      SET EndDate = N
    WHERE meta_id = OLD.meta_id
      AND EndDate IS NULL;
 
   INSERT INTO wp_postmeta_log (table_name, action, meta_id, post_id, meta_key, meta_value, StartDate, EndDate)
   VALUES ('wp_2_postmeta', 'UPDATE', NEW.meta_id, NEW.post_id, NEW.meta_key, NEW.meta_value, N, NULL);
END //

/* --- */

/* Trigger for INSERT */
CREATE TRIGGER WpPostMetaInsert3 AFTER INSERT ON wp_3_postmeta FOR EACH ROW BEGIN
   DECLARE N DATETIME;
   SET N = now();
 
   INSERT INTO wp_postmeta_log (table_name, action, meta_id, post_id, meta_key, meta_value, StartDate, EndDate)
   VALUES ('wp_3_postmeta', 'INSERT', NEW.meta_id, NEW.post_id, NEW.meta_key, NEW.meta_value, N, NULL);
END //

/* Trigger for DELETE */
CREATE TRIGGER WpPostMetaDelete3 AFTER DELETE ON wp_3_postmeta FOR EACH ROW BEGIN
   DECLARE N DATETIME;
   SET N = now();

   UPDATE wp_postmeta_log
      SET EndDate = N
    WHERE meta_id = OLD.meta_id
      AND EndDate IS NULL;

   INSERT INTO wp_postmeta_log (table_name, action, meta_id, post_id, meta_key, meta_value, StartDate, EndDate)
   VALUES ('wp_3_postmeta', 'DELETE', OLD.meta_id, OLD.post_id, OLD.meta_key, OLD.meta_value, N, NULL);
END //

/* Trigger for UPDATE */
CREATE TRIGGER WpPostMetaUpdate3 AFTER UPDATE ON wp_3_postmeta FOR EACH ROW BEGIN
   DECLARE N DATETIME;
   SET N = now();

   UPDATE wp_postmeta_log
      SET EndDate = N
    WHERE meta_id = OLD.meta_id
      AND EndDate IS NULL;
 
   INSERT INTO wp_postmeta_log (table_name, action, meta_id, post_id, meta_key, meta_value, StartDate, EndDate)
   VALUES ('wp_3_postmeta', 'UPDATE', NEW.meta_id, NEW.post_id, NEW.meta_key, NEW.meta_value, N, NULL);
END //

/* --- */

/* Trigger for INSERT */
CREATE TRIGGER WpPostMetaInsert4 AFTER INSERT ON wp_4_postmeta FOR EACH ROW BEGIN
   DECLARE N DATETIME;
   SET N = now();
 
   INSERT INTO wp_postmeta_log (table_name, action, meta_id, post_id, meta_key, meta_value, StartDate, EndDate)
   VALUES ('wp_4_postmeta', 'INSERT', NEW.meta_id, NEW.post_id, NEW.meta_key, NEW.meta_value, N, NULL);
END //

/* Trigger for DELETE */
CREATE TRIGGER WpPostMetaDelete4 AFTER DELETE ON wp_4_postmeta FOR EACH ROW BEGIN
   DECLARE N DATETIME;
   SET N = now();

   UPDATE wp_postmeta_log
      SET EndDate = N
    WHERE meta_id = OLD.meta_id
      AND EndDate IS NULL;

   INSERT INTO wp_postmeta_log (table_name, action, meta_id, post_id, meta_key, meta_value, StartDate, EndDate)
   VALUES ('wp_4_postmeta', 'DELETE', OLD.meta_id, OLD.post_id, OLD.meta_key, OLD.meta_value, N, NULL);
END //

/* Trigger for UPDATE */
CREATE TRIGGER WpPostMetaUpdate4 AFTER UPDATE ON wp_4_postmeta FOR EACH ROW BEGIN
   DECLARE N DATETIME;
   SET N = now();

   UPDATE wp_postmeta_log
      SET EndDate = N
    WHERE meta_id = OLD.meta_id
      AND EndDate IS NULL;
 
   INSERT INTO wp_postmeta_log (table_name, action, meta_id, post_id, meta_key, meta_value, StartDate, EndDate)
   VALUES ('wp_4_postmeta', 'UPDATE', NEW.meta_id, NEW.post_id, NEW.meta_key, NEW.meta_value, N, NULL);
END //

/* --- */

/* Trigger for INSERT */
CREATE TRIGGER WpPostMetaInsert5 AFTER INSERT ON wp_5_postmeta FOR EACH ROW BEGIN
   DECLARE N DATETIME;
   SET N = now();
 
   INSERT INTO wp_postmeta_log (table_name, action, meta_id, post_id, meta_key, meta_value, StartDate, EndDate)
   VALUES ('wp_5_postmeta', 'INSERT', NEW.meta_id, NEW.post_id, NEW.meta_key, NEW.meta_value, N, NULL);
END //

/* Trigger for DELETE */
CREATE TRIGGER WpPostMetaDelete5 AFTER DELETE ON wp_5_postmeta FOR EACH ROW BEGIN
   DECLARE N DATETIME;
   SET N = now();

   UPDATE wp_postmeta_log
      SET EndDate = N
    WHERE meta_id = OLD.meta_id
      AND EndDate IS NULL;

   INSERT INTO wp_postmeta_log (table_name, action, meta_id, post_id, meta_key, meta_value, StartDate, EndDate)
   VALUES ('wp_5_postmeta', 'DELETE', OLD.meta_id, OLD.post_id, OLD.meta_key, OLD.meta_value, N, NULL);
END //

/* Trigger for UPDATE */
CREATE TRIGGER WpPostMetaUpdate5 AFTER UPDATE ON wp_5_postmeta FOR EACH ROW BEGIN
   DECLARE N DATETIME;
   SET N = now();

   UPDATE wp_postmeta_log
      SET EndDate = N
    WHERE meta_id = OLD.meta_id
      AND EndDate IS NULL;
 
   INSERT INTO wp_postmeta_log (table_name, action, meta_id, post_id, meta_key, meta_value, StartDate, EndDate)
   VALUES ('wp_5_postmeta', 'UPDATE', NEW.meta_id, NEW.post_id, NEW.meta_key, NEW.meta_value, N, NULL);
END //

/* --- */

/* Trigger for INSERT */
CREATE TRIGGER WpPostMetaInsert6 AFTER INSERT ON wp_6_postmeta FOR EACH ROW BEGIN
   DECLARE N DATETIME;
   SET N = now();
 
   INSERT INTO wp_postmeta_log (table_name, action, meta_id, post_id, meta_key, meta_value, StartDate, EndDate)
   VALUES ('wp_6_postmeta', 'INSERT', NEW.meta_id, NEW.post_id, NEW.meta_key, NEW.meta_value, N, NULL);
END //

/* Trigger for DELETE */
CREATE TRIGGER WpPostMetaDelete6 AFTER DELETE ON wp_6_postmeta FOR EACH ROW BEGIN
   DECLARE N DATETIME;
   SET N = now();

   UPDATE wp_postmeta_log
      SET EndDate = N
    WHERE meta_id = OLD.meta_id
      AND EndDate IS NULL;

   INSERT INTO wp_postmeta_log (table_name, action, meta_id, post_id, meta_key, meta_value, StartDate, EndDate)
   VALUES ('wp_6_postmeta', 'DELETE', OLD.meta_id, OLD.post_id, OLD.meta_key, OLD.meta_value, N, NULL);
END //

/* Trigger for UPDATE */
CREATE TRIGGER WpPostMetaUpdate6 AFTER UPDATE ON wp_6_postmeta FOR EACH ROW BEGIN
   DECLARE N DATETIME;
   SET N = now();

   UPDATE wp_postmeta_log
      SET EndDate = N
    WHERE meta_id = OLD.meta_id
      AND EndDate IS NULL;
 
   INSERT INTO wp_postmeta_log (table_name, action, meta_id, post_id, meta_key, meta_value, StartDate, EndDate)
   VALUES ('wp_6_postmeta', 'UPDATE', NEW.meta_id, NEW.post_id, NEW.meta_key, NEW.meta_value, N, NULL);
END //

  

转载于:https://www.cnblogs.com/freespider/p/11207260.html

最后

以上就是清新玉米为你收集整理的触发器写法的全部内容,希望文章能够帮你解决触发器写法所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(33)

评论列表共有 0 条评论

立即
投稿
返回
顶部