trigger problem in mySql database - mysql

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 ;

Related

Before trigger in mysql database

I am trying to create a trigger where if the no tickets are less than the no of adults then value 1 will be inserted in trig table.But, I am getting an error.The code is:
CREATE TRIGGER ch
BEFORE INSERT
ON packagebooking
IF(tick.tickets<packagebooking.Adult
AND tick.Package_id=packagebooking.Package_id)
Begin atomic
insert into trig values(1)
MySQL said: Documentation
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'IF(tick.tickets<packagebooking.Adult
AND tick.Package_id=packagebooking.Pa' at line 4.
What changes should I do?
Your syntax for trigger creation has an error, place the IF statement inside the FOR EACH ROW as shown below:
CREATE TRIGGER ch BEFORE INSERT
ON `database`.`table`
FOR EACH ROW BEGIN
IF(tick.tickets<packagebooking.Adult
AND tick.Package_id=packagebooking.Package_id)
THEN Begin atomic
insert into trig values(1)
END IF;
END;

Trigger on MySQL

I'm trying to create a trigger in MySQL using phpMyAdmin, but I'm getting and error and I can't detect the mistake. The main idea its to create a row in the tables stuff and config when a new user is insered.
The code:
CREATE TRIGGER create_stuff_and_config
AFTER INSERT ON user
FOR EACH ROW
BEGIN
insert into stuff(user_id) values (NEW.user_id);
insert into config(user_id) values(NEW.user_id);
END;
The 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 5
I've checked the MySQL doc but I see like I'm doing it well...
I've tried too: INSERT INTO stuff SET user_id = NEW.user_id;
Thank you!
Okay, the solution is to add delimiters...
DELIMITER //
CREATE TRIGGER create_stuff_and_config
AFTER INSERT ON user
FOR EACH ROW
BEGIN
insert into stuff(user_id) values (NEW.user_id);
insert into config(user_id) values(NEW.user_id);
END; //
DELIMITER ;

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

Syntax error in creating Trigger in mysql

m writing following lines for creating a trigger:
create trigger notify after insert on applications
for each row
begin
insert into notifications SET sno=1;
end;
but everytime i get following error:
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
line 4 is - insert into notifications SET sno=1
MySQL is confused about the delimiters. It thinks the first ; is the end of the TRIGGER declaration (which it is not). Use delimiter to change it temporarily, and later to change it back:
delimiter |
create trigger notify after insert on applications
for each row
begin
insert into notifications (sno) values (1);
end;
|
delimiter ;
More in the documentation.

mySQL Trigger returns error

I am trying to set up a trigger on a table to copy the contents of a row into another table.
I have the following:
CREATE TRIGGER story_deleted BEFORE DELETE ON stories
FOR EACH ROW BEGIN
INSERT INTO stories_backup SET story_id = OLD.story_id;
END;
This returns the following error though:
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
I can't work out where I'm going wrong with this. Any ideas?
Try changing the delimiter
DELIMITER $$
CREATE TRIGGER story_deleted BEFORE DELETE ON stories
FOR EACH ROW
BEGIN
INSERT INTO stories_backup SET story_id = OLD.story_id;
END $$
DELIMITER ;
and as far as your privileges go, run this query
SHOW GRANTS;
If SUPER is not there, you could
request your DBA to add that privilege for you
have your DBA create the trigger for you