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 implementet changes that solved the first problem.
I am trying to write a trigger that will log changes in the database to a seperate table. But I keep getting the same error. I have looked at the MYSQL documentation and searched this forum and found a lot of helpfull 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 surgested 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 Coloumn '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
Use DELIMITER for the trigger
Like
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 ;
Related
Im trying to get this program to add a new row into two different tables: bilde and student.
The values entered into bilde should come from the values used in student.
At this point i get this message: error code: 1136. Column count dosen't match value at row 1, and im not quite sure what to do to fix this.
DELIMITER $$
DROP PROCEDURE IF EXISTS NyStudent $$
CREATE PROCEDURE NyStudent (
brukernavn VARCHAR(45)
,fornavn VARCHAR(45)
,etternavn VARCHAR(45)
,klassekode INT
)
BEGIN
START TRANSACTION;
INSERT INTO bilde (filnavn, beskrivelse)
VALUES (CONCAT('bilder/', fornavn, '.jpg'), CONCAT('bilde av ', fornavn, ' ', etternavn));
INSERT INTO student
VALUES ('donaldduck','donald','duck','1');
COMMIT;
END$$
DELIMITER ;
CALL NyStudent('donaldduck','donald','duck','1');
Try adding the column names in the 'INSERT INTO' statement for students, as that should be the major reason for column count error.
Also, Even when we insert values for every column of the table, it is a best practice to include column names after INSERT INTO statement. Following this would avoid any breakage in code to a certain extent, even on schema changes in the future.
we are having a problem with the created_at on a WordPress system. Somehow some users get that field updated "randomly" to an invalid value, so it get's set as '00000000' datetime. We haven't found the cause on the WP code, and we are now analizing the few plugins the project has. It has a large Code and user base, so we thought it might be faster to use a MySQL trigger to catch this "random" updates.
The thing is we somehow keep on hitting a syntax error on or Trigger code, and lame SQLer's as we are, we need help trying to figure out what it could be.
What we are tying to accomplish is:
Detect when the user table gets updated
Check if the created_at row has been modified
Insert the old data into a new table we created just for this purpose (user_registration_changed_records).
We decided to check for each row just in case there is some weird behaviour going on.
DELIMITER $$
CREATE TRIGGER `user_register_updated` BEFORE UPDATE ON `wp_t8y31tcd9u_users`
FOR EACH ROW
BEGIN
IF OLD.created_at != NEW.created_at
INSERT INTO user_registration_change_records (user_id, created_at) VALUES (OLD.ID, OLD.created_at)
END IF;
END;$$
DELIMITER ;
Thanks in advance for any suggestions!
Syntax Error
#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 'INSERT INTO user_registration_change_records (user_id, created_at) VALUES (OLD.I' at line 6
You have a couple of syntax errors:
missing then keyword before the insert (this is the one the error message relates to)
missing ; after the insert's row
extra ; after the end
At least these are the ones I can see.
DELIMITER $$
CREATE TRIGGER `user_register_updated` BEFORE UPDATE ON `wp_t8y31tcd9u_users`
FOR EACH ROW
BEGIN
IF OLD.created_at != NEW.created_at THEN
INSERT INTO user_registration_change_records (user_id, created_at) VALUES (OLD.ID, OLD.created_at);
END IF;
END$$
DELIMITER ;
It is worthwile using mysql manual on compound statement syntax because all these can be found there.
Could anyone tell me that what is the syntax error in this trigger
DELIMITER |
CREATE TRIGGER User_XEntity_Before_Delete
BEFORE DELETE
ON UserXEntity FOR EACH ROW
BEGIN
-- Insert record into Delete_UserXEntity table
INSERT INTO Delete_UserXEntity
( DeletedUserXEntityId,
UserId,
CreatedAt)
VALUES
( OLD.Id,
OLD.UserId,
NOW() );
END;
|
DELIMITER ;
I've got the solution. Actually I was entering this code in phpmyadmin Trigger window, where it asks about table name, time and event already. So we only need to write the trigger action code in that window. I was writing the whole trigger code and that's why I was giving me a syntax error.
We only need to write the following code in PHPMYADMIN add new trigger windows:
INSERT INTO Delete_UserXEntity
( DeletedUserXEntityId,
UserId,
CreatedAt)
VALUES
( OLD.Id,
OLD.UserId,
NOW() );
I need to write a trigger where if a customerName of an incoming enquiry matches a customer name in a synonymns table then the customerID from that table is used to find the customer in the customer table.
I got a trigger to work which just searched the customer table but not the synonym table first. I was thinking something like: (It's psuedo-esque code)
CREATE TRIGGER `Find Customer` AFTER INSERT ON `enquiry` FOR EACH ROW
BEGIN
INSERT INTO customersmatched (enquiryID, customerID)
SELECT NEW.id, id, customerName, customerID FROM customer, customerSynonyms WHERE
customerSynonyms.customerName = NEW.companyName AND customer.id = customerSynonyms.customerID
HAVING COUNT(id)=1
END;
The error I recieve:
#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 'END' at line 7
But I'm afriad this doesn't work.
Any idea how one would go about this in mySQL? Thanks!
UPDATE
I tried this way too - it didn't work either!
DELIMITER $$
CREATE TRIGGER Find_Customer AFTER INSERT ON enquiry
FOR EACH ROW
BEGIN
IF EXISTS (SELECT customerID FROM customerSynonyms WHERE customerName = NEW.companyName) THEN
IF EXISTS (SELECT id FROM customer WHERE id = #customerID) THEN
INSERT INTO customersMatched (enquiryID, customerID)
HAVING COUNT(id)=1
END IF;
END IF;
END$$
DELIMITER ;
Update 2
It turns out I was WAYYY over complicating things. The customerSynonym table had the customerID stored in a column, which was all I really needed as I could just grab all the data through php with the customerID. The final trigger which works was super simple:
INSERT INTO customersmatched (enquiryID, customerID)
SELECT NEW.id, customerID FROM customersynonyms WHERE
customersynonyms.customerName=NEW.companyName
HAVING COUNT(id)=1
Thanks for all your answers, they're always very much appreciated! :-)
I think you want something like this:
CREATE TRIGGER Find_Customer AFTER INSERT ON enquiry FOR EACH ROW
BEGIN
INSERT INTO customersmatched (enquiryID, customerID)
SELECT NEW.id, customerID FROM customer, customerSynonyms
WHERE customerSynonyms.customerName = NEW.companyName
AND customer.id = customerSynonyms.customerID
HAVING COUNT(id)=1
END;
A few things to note:
I renamed the trigger to Find_Customer. I'm not 100% sure if you can have a space in the trigger name
I removed the single quotes around Find_Customer and enquiry. The single quotes could be read as a string literal by mysql and cause problems. It's better to avoid that if possible.
I changed your select query to only select NEW.id and customerID, since those are the only two things you're inserting. You might have to play with the select a little bit more to get it exactly how you want it.
If this is just part of the trigger, and you have more than just a one-statement trigger, you'll need to set delimiters. Basically, you'd have delimiter | before the trigger definition, and then END| instead of END
Good luck!
I have two tables. The first is my person table which has id, name, creation_date as values, I have a old_person table (id, name, modified_date) which I want to populate the value of person before it actually changes. How would I go about that? I have tried triggers but failed.
I tried as follows
create trigger Person_Trigger Update on person
before update
as
insert into old_person(id, name, modified)
select id, new.name, getdate()
from person
It's giving me syntax errors...Not many Trigger references out there either, a little push would be greatly appreciated!
Have a look at the following example
SQL Fiddle DEMO
IO hade a bit of trouble myself, but from How to Create Triggers in MySQL there was a line that made me think
The first MySQL command we’ll issue is a little unusual:
DELIMITER $$
Our trigger body requires a number of SQL commands separated by a
semi-colon (;). To create the full trigger code we must change
delimiter to something else — such as $$.
Finally, we set the delimiter back to a semi-colon:
DELIMITER ;
So in the SQL Fiddle I changed the query terminator to GO and that seemed top work.
CREATE TABLE person
(
id INT,
name varchar(20)
)
GO
CREATE TABLE old_person
(
id INT,
name varchar(20),
modified DATETIME
)
GO
CREATE TRIGGER Person_Trigger before update on person
FOR EACH ROW BEGIN
INSERT INTO old_person(id, name, modified)
VALUES (OLD.id, OLD.name, NOW());
END
GO
INSERT INTO person VALUES (1,'TADA')
GO
UPDATE person SET name = 'FOO'
GO
Try this:
DELIMITER \\
CREATE TRIGGER `Person_Trigger`
BEFORE UPDATE ON `Person`
FOR EACH ROW
BEGIN
DECLARE date_modified datetime;
SET date_modified = NOW();
INSERT INTO old_person(id, name, modified)
VALUES (OLD.id, OLD.name, date_modified);
END\\
This syntax works for me on my own projects. You may also need to declare delimiters before you begin the trigger. Also if you want to use the NEW keyword it should be AFTER update. Switch to the OLD keyword if you are going to keep using BEFORE update on your trigger.