触发以跟踪MySQL数据库中的更改 [英] Trigger to track changes in MySQL Database

查看:58
本文介绍了触发以跟踪MySQL数据库中的更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎无法创建触发器.我已经尝试了以下两种方式进行更新.我通过插入语句不断收到语法错误.我搜索了过去4个小时的论坛和网络搜索,没有任何变化.对此还有更多代码,它基本上会重复执行.任何帮助,将不胜感激.谢谢你.我正在运行MySQL 5.0,并通过phpMyAdmin 2.8.2.4以Administrator/Root身份访问.

I cannot seem to create a trigger. I have tried it the two ways below for the update. I keep getting a syntax error with the insert statement. I have searched forums and web searches for the past 4 hrs with no change. There is alot more code to this, It basically repeats itself. Any help would be appreciated. Thank You. I am running MySQL 5.0 and acessing via phpMyAdmin 2.8.2.4 as Administrator/Root.

CREATE TRIGGER insert_classes
AFTER insert ON Classes
FOR EACH ROW
BEGIN
    insert into insert_tracking_classes (classID, Title, classDesc, Category, isEvent, picLeft, picTop, picRight, picBottom, prnColor, modified)
    values(NEW.classID, NEW.Title, NEW.classDesc, NEW.Category, NEW.isEvent, NEW.picLeft, NEW.picTop, NEW.picRight, NEW.picBottom, NEW.prnColor, NOW());
END;

CREATE TRIGGER insert_classes
AFTER insert ON Classes
FOR EACH ROW
BEGIN
insert into insert_tracking_classes
        set classID = NEW.classID,
        Title = NEW.Title,
        classDesc = NEW.classDesc,
        Category = NEW.Category,
        isEvent = NEW.isEvent,
        picLeft = NEW.picLeft,
        picTop = NEW.picTop,
        picRight = NEW.picRight,
        picBottom = NEW.picBottom,
        prnColor = NEW.prnColor,
        modified = NOW(); 
END;


错误

 #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line * (at the end of the insert/values statement)


尝试2

 DELIMITER $$
 CREATE TRIGGER insert_classes AFTER insert ON Classes
 FOR EACH ROW BEGIN
insert into insert_tracking_classes (classID, Title, classDesc, Category, isEvent, picLeft, picTop, picRight, picBottom, prnColor, modified)
values(NEW.classID, NEW.Title, NEW.classDesc, NEW.Category, NEW.isEvent, NEW.picLeft, NEW.picTop, NEW.picRight, NEW.picBottom, NEW.prnColor, NOW());
 END$$
 DELIMITER ;


错误

    SQL query:

    DELIMITER $$ CREATE TRIGGER insert_classes AFTER INSERT ON Classes
    FOR EACH
    ROW BEGIN
    INSERT INTO insert_tracking_classes( classID, Title, classDesc, Category, isEvent, picLeft, picTop, picRight, picBottom, prnColor, modified )
    VALUES (
-->     NEW.classID, NEW.Title, NEW.classDesc, NEW.Category, NEW.isEvent, NEW.picLeft, NEW.picTop, NEW.picRight, NEW.picBottom, NEW.prnColor, NOW( )
    );

    MySQL said: Documentation
    #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DELIMITER $$
         CREATE TRIGGER insert_classes AFTER insert ON Classes
         F' at line 1 


我的桌子

CREATE TABLE insert_tracking_classes (  --- Same table layout for Classes table minus Modified time and tracking_id
  tracking_id int(11) NOT NULL AUTO_INCREMENT,
  classID int(11) NOT NULL,
  Title varchar(48) NOT NULL,
  classDesc text,
  Category varchar(128) default NULL,
  isEvent int(8) default NULL,
  picLeft int(8) default NULL,
  picTop int(8) default NULL,
  picRight int(8) default NULL,
  picBottom int(8) default NULL,
  prnColor varchar(4) default NULL,
  modified datetime NOT NULL,
  PRIMARY KEY (tracking_id)
);

更新:尝试使用静态值并删除if和Modifyed = NOW()语句而未作任何更改.

Update: Have tried to use static values and removing the if and modified = NOW() statements with no change.

推荐答案

必须删除Delimiter,Begin和End语句.

Had to remove Delimiter, Begin, and End statements.

DROP TRIGGER IF EXISTS insert_classes;
CREATE TRIGGER insert_classes AFTER insert ON Classes
FOR EACH ROW
insert into tracking_classes (command, classID, Title, classDesc, Category, isEvent, picLeft, picTop, picRight, picBottom, prnColor, modified)
values('insert', NEW.classID, NEW.Title, NEW.Desc, NEW.Category, NEW.isEvent, NEW.picLeft, NEW.picTop, NEW.picRight, NEW.picBottom, NEW.prnColor, NOW());

这篇关于触发以跟踪MySQL数据库中的更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆