UPDATE: the delimiter thing actually solved my original problem. But I now ended up with a new Error. I'll describe it below along with the implemented changes that solved the first problem.
I am trying to write a trigger that will log changes in the database to a separate table. But I keep getting the same error. I have looked at the MySQL documentation and searched this forum and found a lot of helpful answers. Trouble is that I now have a piece of SQL code that looks exactly like the answers given in this forum but I still get an error.
The trigger I am trying to use is:
CREATE TRIGGER logInsert AFTER INSERT ON test_table
FOR EACH ROW
BEGIN
INSERT INTO datalog (action, id, timestamp, data1, data2)
VALUES ('insert', NEW.id, NOW(), NEW.data1, NEW.data2);
END;
The error message I get back is:
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 5
I have tried using " instead of single quotes. someone suggested in on of the answers here that you should use backticks, so tried that as well.
No matter what, the error message is exactly the same.
A friendly soul here posted a fix for my original error. I needed to add DELIMITER to the statement so that it should look like this:
DELIMITER $$
CREATE TRIGGER logInsert AFTER INSERT ON test_table
FOR EACH ROW
BEGIN
INSERT INTO datalog (action, id, timestamp, data1, data2)
VALUES ('insert', NEW.id, NOW(), NEW.data1, NEW.data2);
END$$
DELIMITER ;
This change solved the original error, but also let to a new error.
The new error is:
Unknown Column 'id' in table NEW
This is the table I'm trying to write to:
CREATE TABLE datalog (
id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
timestamp TIMESTAMP,
data1 VARCHAR(255) NOT NULL,
data2 DECIMAL(5,2) NOT NULL
);
Hope someone here has an idea of what is wrong.
Kind regards, Jonas
DELIMITERso the parser does not assume you are trying to end the definition the first time it hits a semicolon. Typically,DELIMITER $$followed by def terminated withEND$$, followed byDELIMITER ;to return things to normal.