Can't create a trigger to insert concat value - mysql

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 :)

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.

Setting up a trigger in PHPMyAdmin

I'm trying to comprehend triggers, and I think I fully understand them, but I haven't been able to implement any of them. I want this code to delete a user with the name "test". So if anyone updates their name to "test" the user should be deleted.
My example code:
CREATE TRIGGER `my_trigger`
BEFORE UPDATE ON `my_db` FOR EACH ROW
BEGIN
DELETE FROM my_table WHERE `username` = 'test';
END
My 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 4
I can't figure out why the delete statement is giving me an error. Any ideas?
Here is the syntaxically correct SQL:
DELIMITER ;
DROP TRIGGER IF EXISTS `my_trigger`;
DELIMITER $$
CREATE TRIGGER `my_trigger`
BEFORE UPDATE ON `my_table` FOR EACH ROW
BEGIN
DELETE FROM my_table WHERE `username` = 'test';
END$$
DELIMITER;
But it won't work, because you can't delete from the table, you are updating:
A trigger can access both old and new data in its own table. A trigger
can also affect other tables, but it is not permitted to modify a
table that is already being used (for reading or writing) by the
statement that invoked the function or trigger.
http://dev.mysql.com/doc/refman/5.5/en/faqs-triggers.html#qandaitem-B-5-1-9
If you want a simple example, try this:
DELIMITER ;
DROP TRIGGER IF EXISTS `my_trigger`;
DELIMITER $$
CREATE TRIGGER `my_trigger`
BEFORE UPDATE ON `my_table` FOR EACH ROW
BEGIN
SET NEW.`username` = 'aaa';
END$$
DELIMITER;
This will always set 'aaa' as the user name when updating.
It's possible to associated trigger only with a table.
Also within a stored function or trigger, it is not permitted to modify a table that is already being used (for reading or writing) by the statement that invoked the function or trigger.
Restrictions on Stored Programs

MySQL Trigger syntax error unexpected end of input

this is the query that i am using
create trigger trig1 after insert on participant for each row
begin
insert into team(sap) select sap from participant order by ID desc limit 1,1
end;
it is supposed to copy the sap field from the participant table into the sap field of the team table after a new row is inserted into the participant table
the engine shows me an unexpected end of input error at the end of "end"
i've tried numerous methods to rework the query but i keep getting the same error
what am i doing wrong?
thanks
You are using trigger than no need to run a query on same table to get latest sap value, you can directly get that value using new.sap.
Problem in your query, In your query you haven't put semicolon(;) after INSERT..SELECT query and END keyword.
This will work for you:
DELIMITER $$
DROP TRIGGER /*!50032 IF EXISTS */ `trig1`$$
CREATE
TRIGGER `trig1` AFTER INSERT ON `participant`
FOR EACH ROW BEGIN
INSERT INTO team(sap)
VALUES(new.sap);
END;
$$
DELIMITER ;

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 ;