MySQL Error 1064 42000 while trying to create a trigger - mysql

CREATE TRIGGER moving_average
AFTER INSERT
ON filtered_data FOR EACH ROW
BEGIN
INSERT INTO moving_average_table (pollutant_id,pollutant_value,lastUpdated,station_id)
VALUES (new.pollutant_id,avg(new.pollutant_value),new.lastUpdated,new.station_id)
END;
I am trying to create a trigger on the table filtered_data with this piece of code, is it syntactically wrong? I wonder why I keep on getting a syntax error 1064 42000. Any help would be appreciated.

I tried this, it looks work for me,
delimiter //
CREATE TRIGGER moving_average
AFTER INSERT
ON filtered_data FOR EACH ROW
begin
INSERT INTO moving_average_table (pollutant_id,pollutant_value,lastUpdated,station_id)
VALUES (new.pollutant_id,avg(new.pollutant_value),new.lastUpdated,new.station_id) ;
end//
delimiter ;
according to this https://dev.mysql.com/doc/refman/8.0/en/trigger-syntax.html
if you use the mysql program to define a trigger that executes multiple statements, it is necessary to redefine the mysql statement delimiter so that you can use the ; statement delimiter within the trigger definition.
you should remove the begin...end or redefine the delimiter

Related

The second Insert does not work in my trigger

I am trying to learn more about triggers and I wrote a simple SQL code in MySQL.
This code will work with one insert but it will cause an error when I add the second insert.
I think the code that I wrote does not compile the second insert.
I would be appreciate for Any Help
Code:
create trigger MasterInputTRG
after insert on MasterInput
for each row
insert into NewEMCtestPLAN(ProjectNumber,Manufacture,ModelName,`PO Number`,AccountExecutive,AENotes) values(NEW.ProjectNumber,NEW.Applicant,NEW.ModelName,NEW.PONumber,NEW.AccountExecutive,NEW.AENotes);
insert into NewRF(ProjectNumber,Applicant,ModelName,PONumber,AccountExecutive,AENotes) values(NEW.ProjectNumber,NEW.Applicant,NEW.ModelName,NEW.PONumber,NEW.AccountExecutive,NEW.AENotes);
Error:
Syntax error near 'insert into
NewEMCtestPLAN(ProjectNumber,Applicant,ModelName,PONumber,AccountExe'
at line 5
Update:
I got an answer and I add the other code with errors here.:
Second Code that does not work:
create trigger MasterInputTRG
after insert on MasterInput
for each row
BEGIN
insert into NewEMCtestPLAN(ProjectNumber,Manufacture,ModelName,`PO Number`,AccountExecutive,AENotes) values(NEW.ProjectNumber,NEW.Applicant,NEW.ModelName,NEW.PONumber,NEW.AccountExecutive,NEW.AENotes);
insert into NewRF(ProjectNumber,Applicant,ModelName,PONumber,AccountExecutive,AENotes) values(NEW.ProjectNumber,NEW.Applicant,NEW.ModelName,NEW.PONumber,NEW.AccountExecutive,NEW.AENotes);
END
Errors:
Error in query (1054): Unknown column 'NEW.ProjectNumber' in 'field
list'
But this error did not appear when i used the new.projectnumber in first insert.
second error:
Error in query (1064): Syntax error near 'END' at line 1
A trigger body with multiple statements need to be surrounded bei BEGIN and END. But then (depending on your client) you might need to change the delimiter:
delimiter //
create trigger MasterInputTRG
after insert on MasterInput
for each row
begin
insert into NewEMCtestPLAN (...) values (...);
insert into NewRF (...) values (...);
end //
delimiter ;
The same applies to stored procedures and functions.
Note that the CREATE TRIGGER statement is a single.. well.. statement. Statements are delimited by.. well.. a delimiter. So in your original query the trigger definition ends with the first delimiter (;):
create trigger MasterInputTRG
after insert on MasterInput
for each row
insert into NewEMCtestPLAN (...) values (...); -- trigger ends here
-- anything else is outside the trigger
insert into NewRF (...) values (...);
The second insert could be a valid statement but outside the trigger definition. However I would expect a different error message like: unknown table (alias) 'NEW', because NEW is not known outside the trigger.
Now if we change the delimiter to //, the create statement ends after the first occurance of //, so we can use ; in the trigger body without terminating the CREATE statement.
Usually // or $$ are used for a delimiter. You can try something else, but it should not have any meaning in (My)SQL. For example | is a bit operator.
You should add Begin End in the trigger.
create trigger MasterInputTRG
after insert on MasterInput
for each row
BEGIN
insert into NewEMCtestPLAN(ProjectNumber,Manufacture,ModelName,`PO Number`,AccountExecutive,AENotes) values(NEW.ProjectNumber,NEW.Applicant,NEW.ModelName,NEW.PONumber,NEW.AccountExecutive,NEW.AENotes);
insert into NewRF(ProjectNumber,Applicant,ModelName,PONumber,AccountExecutive,AENotes) values(NEW.ProjectNumber,NEW.Applicant,NEW.ModelName,NEW.PONumber,NEW.AccountExecutive,NEW.AENotes);
END

Make a trigger that copies new data from table to another table after insert

I have this code for the trigger:
CREATE TRIGGER `insert_after` AFTER INSERT ON `hyk50_0001`
FOR EACH ROW BEGIN
INSERT INTO hyk50_0001_copy(Fecha)
SELECT Fecha FROM hyk50_0001
END;
but doesn't work, it says a syntax error but I didn't see it
I'm using Navicat. And hyk50_0001_copy it's a identical copy of hyk50_0001.
The target is take the new row of hyk50_0001 and INSERT in hyk50_0001_copy
I want put all the database, but if it doesn't work with only a value I can't progress.
I don't see a syntax error, but I assume you intend:
DELIMITER $$
CREATE TRIGGER `insert_after` AFTER INSERT ON `hyk50_0001`
FOR EACH ROW
BEGIN
INSERT INTO hyk50_0001_copy(Fecha)
VALUES (new.Fecha)
END;$$
DELIMITER ;
That is, you probably want to insert only the row that was just created.

Can't create a trigger to insert concat value

i am trying to create a trigger to concatenate my table columns into a single column but i can't find the error.
code:
create trigger molecule_trigger After insert on molecule
For each row
begin
Update molecule
Set molecule_text= CONCAT(mid,',',ULCHEm_ID,',',IUPAC_name,',',Inchi,',',inchi_key,',',smiles,',',can_smiles,',',Molecular_formula,',',Molecular_weight,',',vendor,',',CAS,',',links,',',image);
end;
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 '' at line 6
You get this error, because you started a "multiple statements" block with begin, but the ; after your update statement terminates the create trigger statement before the end; statement.
You have to either change the delimiter
DELIMITER $$
create trigger molecule_trigger After insert on molecule
For each row
begin
Update molecule
Set molecule_text= CONCAT(mid,',',ULCHEm_ID,',',IUPAC_name,',',Inchi,',',inchi_key,',',smiles,',',can_smiles,',',Molecular_formula,',',Molecular_weight,',',vendor,',',CAS,',',links,',',image);
end $$
DELIMITER ;
Or you remove the begin and end.
create trigger molecule_trigger After insert on molecule
For each row
Update molecule
Set molecule_text= CONCAT(mid,',',ULCHEm_ID,',',IUPAC_name,',',Inchi,',',inchi_key,',',smiles,',',can_smiles,',',Molecular_formula,',',Molecular_weight,',',vendor,',',CAS,',',links,',',image);
Now, you have another problem. You're trying to do an action in the trigger on the same table as your trigger works on. This is not allowed. Change your trigger to this:
create trigger molecule_trigger BEFORE insert on molecule
For each row
SET NEW.molecule_text= CONCAT_WS(',', NEW.mid, NEW.ULCHEm_ID, NEW.IUPAC_name, NEW.Inchi, NEW.inchi_key, NEW.smiles, NEW.can_smiles, NEW.Molecular_formula, NEW.Molecular_weight, NEW.vendor, NEW.CAS, NEW.links, NEW.image);
Note though, that this sets the molecule_text only for the columns inserted. Your trigger updated the whole table each time a row is inserted. And if you insert 3 rows in one statement, your table gets updated 3 times. This is not what you want to do anyway :)

Creating trigger for multiple table insert operation

I have created table in Slq yog which was working fine. then i created same on mysql terminal.
this also executes with no error:
DELIMITER $$
CREATE TRIGGER trigger1 AFTER INSERT ON table2 FOR EACH ROW BEGIN
INSERT INTO record VALUES (id,NEW.sent);
INSERT INTO temp values (id,NEW.sent,NEW.pcount,NEW.ncount);
END
$$ DELIMITER ;
But after this when I enter show triggers or any other command it ask for -> no input
so I need to terminate mysql. Then when I check show triggers says Empty set.
Where is the mistake here?
DELIMITER $$
CREATE TRIGGER trigger1 AFTER INSERT ON table2 FOR EACH ROW BEGIN
INSERT INTO record VALUES (id,NEW.sent);
INSERT INTO temp values (id,NEW.sent,NEW.pcount,NEW.ncount);
END
$$
DELIMITER ;
new delimiter must be on another line

trigger problem in mySql database

I have this code (mySql 5.1.36)
CREATE TRIGGER problem_description AFTER INSERT ON problem_description
FOR EACH ROW BEGIN
INSERT INTO log SET Id=NEW.Id,user_name=NEW.user_name;
END;
and have this 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 '' at line 3
For some older phpMyadmin and MySQL you cannot use the standard trigger creation. My question:
http://stackoverflow.com/questions/4289502/trigger-to-track-changes-in-mysql-databse
i had the same issue. you need to remove the delimiter and begin/end statements. your trigger would look somehting like:
DROP TRIGGER IF EXISTS problem_descripter;
CREATE TRIGGER problem_descriptor AFTER insert ON problem_description
FOR EACH ROW
insert into log (Id, user_name)
values(NEW.Id, NEW.user_name);
When do you get this error message? When you create the trigger? You may need to use another delimiter than ; when you create your trigger.
delimiter //
create trigger problem_descriptor after insert on problem_description
for each row begin
insert into
log
set
`Id`=NEW.`Id`,
`user_name`=NEW.`user_name`;
end;
//
delimiter ;