简体   繁体   English

MySQL触发器错误添加新记录

[英]MySql trigger error with adding a new record

I've just learned the MySQL triggers and how they work. 我刚刚学习了MySQL触发器及其工作方式。 I decided to apply it on my small website. 我决定将其应用在我的小型网站上。

I have a Users table where new users accounts are created and I would like to keep a history of adding new accounts in a UsersHistory table. 我有一个Users表,用于在其中创建新用户帐户,并且我想保留在UsersHistory表中添加新帐户的历史记录。

The error is that when I execute the following query, it gives me an error: 错误是当我执行以下查询时,它给了我一个错误:

Query: 查询:

CREATE TRIGGER User_After_Insert 
AFTER INSERT ON UsersHistory FOR EACH ROW WHEN NOT NEW.Deleted 
BEGIN                  
    SET @changeType = 'DELETE';  

    INSERT INTO UsersHistory (UserID, changeType) 
    VALUES (NEW.ID, @changeType);    
END;

CREATE TRIGGER User_After_Insert1 
AFTER INSERT ON UsersHistory FOR EACH ROW WHEN NEW.Deleted 
BEGIN              
    SET @changeType = 'NEW';  

    INSERT INTO UsersHistory (UserID, changeType) 
    VALUES (NEW.ID, @changeType);        
END;

The error is: 错误是:

1064 - You have an error in your SQL syntax; 1064-您的SQL语法有误; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHEN NOT NEW.Deleted BEGIN Â 请查看与您的MySQL服务器版本相对应的手册,以获取在'WHEN NOT NEW'附近使用正确的语法。
SET @changeType = 'DELE' at line 1 第1行的SET @changeType ='DELE'

I looked for a solution but I couldn't find. 我正在寻找解决方案,但找不到。

Thanks 谢谢

Have you set a DELIMITER to something different than ";"? 您是否将DELIMITER设置为不同于“;”的内容? Also, I saw some stuff I didn't know to be supported in mysql (the WHEN statements before the BEGIN blocks), so heres my suggestion: 另外,我看到了一些我不知道在mysql中受支持的东西(BEGIN块之前的WHEN语句),所以这是我的建议:

delimiter //
CREATE TRIGGER User_After_Insert AFTER INSERT ON UsersHistory FOR EACH ROW
INSERT INTO UsersHistory (UserID, changeType) VALUES (NEW.ID, 'NEW')//
CREATE TRIGGER User_After_Delete AFTER DELETE ON UsersHistory FOR EACH ROW              
INSERT INTO UsersHistory (UserID, changeType) VALUES (OLD.ID, 'DELETE')//
delimiter ;

UPDATE 更新

Due to your comment below, I think you need to read up on trigger a bit more mate. 由于您在下面的评论,我认为您需要进一步了解触发器。 But here's the gist. 但这是要点。

The above statements, create triggers in the actual database. 上面的语句在实际数据库中创建触发器。 In effect, you "install" the triggers in your database schema. 实际上,您在数据库模式中“安装”了触发器。 Running the statements in any mysql client, will create the triggers if you have appropriate account rights. 如果您具有适当的帐户权限,则在任何mysql客户端中运行语句将创建触发器。

Now, from this stage on, you dont explicitly call them from PHP or anything like that. 现在,从此阶段开始,您无需通过PHP或类似的方式显式调用它们。 They live in the database and are called automatically when you perform certain actions. 它们位于数据库中,在您执行某些操作时会自动被调用。 In the above case, AFTER a record is deleted from UserHistory or AFTER a record is inserted into UserHistory. 在上述情况下,将记录从UserHistory中删除后或将记录插入UserHistory中之后。

So, when you run "INSERT INTO UserHistory VALUES ..." from your php script, the database will fire the trigger automatically. 因此,当您从php脚本运行“ INSERT INTO UserHistory VALUES ...”时,数据库将自动触发触发器。

Hope that makes sence. 希望有道理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM