Why I get error trigger? - mysql

I tried to create Mysql trigger by query:
CREATE TRIGGER `Delete Video` AFTER DELETE ON video
FOR EACH ROW
BEGIN
DELETE FROM usersalbumsvideo where id_video = OLD.idVideo;
DELETE FROM categorytovideo where idVideo = OLD.idVideo;
END
But I get 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
Tried also to swap id_video = OLD.idVideo, anda an one DELETE

I'm guessing that the problem is your delimiter, looking at this page I think that your query should be:
delimiter //
CREATE TRIGGER `Delete Video` AFTER DELETE ON video
FOR EACH ROW
BEGIN
DELETE FROM usersalbumsvideo where id_video = OLD.idVideo;
DELETE FROM categorytovideo where idVideo = OLD.idVideo;
END;//
delimiter ;

Related

Can I update just added row in MySQL with trigger based on condition?

I need to update the salary of a newly added employee in the same table if condition is satisfied. SQL keeps throwing errors about syntax, if I use delimiter change it looks like this:
delimiter //
CREATE TRIGGER fixSalary
AFTER INSERT ON emp
FOR EACH ROW
BEGIN
IF NEW.salary < 50000 THEN
UPDATE emp
SET salary = 5000
WHERE eno = NEW.eno;
END IF;
END;//
delimiter ;
But it throws an 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
'delimiter //
create trigger fixSalary
after insert on emp
for each row
begin
if ' at line 1"
I tried removing the delimiter:
CREATE TRIGGER fixSalary
AFTER INSERT ON emp
FOR EACH ROW
BEGIN
IF NEW.salary < 50000 THEN
UPDATE emp
SET salary = 5000
WHERE eno = NEW.eno;
END IF;
END;
which resulted in another 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 8
I've tried all kinds of semicolons combinations with no luck. Can anyone help me to understand what's going on?

MySQL syntax Error on Create Trigger

Here is my trigger, I am getting the MySQL syntax error. I wanted to reduce the balance from sms_index table sms_count column value.
CREATE TRIGGER sms_log_update AFTER UPDATE ON sms_index
FOR EACH ROW
BEGIN
IF NEW.approved_status = '1' THEN
UPDATE sms_package SET balance = (balance - OLD.sms_count) WHERE group_id = OLD.ins_group_id;
END IF;
END;
Error Message:
#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
Your code looks correct, except for the possible problem of the delimiter. Try the following:
DELIMITER //
CREATE TRIGGER sms_log_update AFTER UPDATE ON sms_index
FOR EACH ROW
BEGIN
IF NEW.approved_status = '1' THEN
UPDATE sms_package SET balance = (balance - OLD.sms_count)
WHERE group_id = OLD.ins_group_id;
END IF;
END;//
DELIMITER ;
From the documentation:
However, just as for stored routines, 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.

Error 1064 on Mysql trigger

I'm writing a trigger in MySQL to take log of table updates. The log table is called individuo_storico and the target table is called individuo. When individuo is updated, I want to check if IDQualifica and IDLivello are changed, if yes a record in individuo_storico is inserted.
I write down this code but I get a #1064 error, where's the syntax error?
use ore;
create trigger individuo_update after update on individuo
for each row
begin
if ( NEW.IDLivello <> OLD.IDLivello or NEW.IDQualifica <> OLD.IDQualifica) then
insert into individuo_storico(IDIndividuo, IDQualifica, IDLivello)
values (NEW.IDIndividuo, NEW.IDQualifica, NEW.IDLivello);
end if;
end;
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
Please wrap your trigger creation with the monikers necessary so the db engine does not choke on it. So three areas:
1) line 1
2) right after the end;
3) and then a reset to a default delimiter.
Same concept is typical with stored proc creations.
DELIMITER $$
create trigger individuo_update after update on individuo
for each row
begin
if ( NEW.IDLivello <> OLD.IDLivello or NEW.IDQualifica <> OLD.IDQualifica) then
insert into individuo_storico(IDIndividuo, IDQualifica, IDLivello)
values (NEW.IDIndividuo, NEW.IDQualifica, NEW.IDLivello);
end if;
end;$$
DELIMITER ;

SQL syntax error in trigger

Can someone tell me what this sql does not work? Where is the syntax error?
SET NEW.`modified` = CURTIME() WHERE `id` = NEW.id;
Error:
One or more errors have occured while processing your request:
The following query has failed:
CREATE DEFINER=`root`#`localhost` TRIGGER `event_name`
BEFORE UPDATE ON `clients`
FOR EACH ROW
SET NEW.`modified` = CURTIME() WHERE `id` = NEW.id;
MySQL said: #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 'WHERE id = NEW.id' at line 1
Image shows the error
http://s23.postimg.org/rhsf2x1tn/screenshot.png
You don't need the where condition to specify the row being changed.
For every row being updated, you set the specific column value. See the example below as well as the quoted reference.
Quoting/Referencing from here:
DELIMITER |
CREATE TRIGGER event_name BEFORE UPDATE ON clients
FOR EACH ROW
BEGIN
SET NEW.date_modify = NOW();
END;
|
DELIMITER ;

sql error on creating a trigger

I typed this code into mysql and it gave an error:
CREATE TRIGGER delete_vote_topic BEFORE DELETE ON vote_topic
FOR EACH ROW BEGIN
DELETE FROM vote_option WHERE vote_topic_id = OLD.id;
DELETE FROM vote_option_votes WHERE vote_topic_id = OLD.id;
END;
the error was:
#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
Anybody knows what I'm doing wrong?
Try using a delimiter
delimiter |
CREATE TRIGGER delete_vote_topic BEFORE DELETE ON vote_topic
FOR EACH ROW
BEGIN
DELETE FROM vote_option
WHERE vote_topic_id = OLD.id;
DELETE FROM vote_option_votes
WHERE vote_topic_id = OLD.id;
END;
|