Auto update/fill a DATETIME column in MySQL using a trigger - mysql

I'm trying to auto update/fill datetime columns in my database.
I'm current running:
MYSQL server: 5.1.56-log
MySQL client version: mysqlnd 5.0.10
I've created two triggers based on the documentation (http://dev.mysql.com/doc/refman/5.0/en/create-trigger.html):
INSERT TRIGGER:
create trigger 'games_insert'
before insert
on games for each row
begin
set new.created = NOW();
set new.modified = NOW();
end;
MODIFICATION TRIGGER
create trigger 'games_update'
before update
on games for each row
begin
set new.modified = NOW();
end;
Both seem to be valid since you can only modify the new row prior to updating/inserting. The AFTER yields many more errors.
However, I keep getting the following syntax error and I'm not sure why:
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 ''games_insert' before insert on games for each row
begin set new.created =' at line 1
I'm interacting with the sql server through phpmyadmin and have set the delimiter to be $$

don't use "'" in trigger name
create trigger 'games_insert' (X)
create trigger games_insert (O)

Related

create trigger not working in mysql

i am trying to write a trigger which validates insert statements before inserting in the table
table structure:
This is the code that i wrote
DELIMITER
$$
CREATE TRIGGER check_date_format
INSERT BEFORE ON
job_histry FOR EACH ROW
BEGIN
SET #bool=NEW.end_date LIKE '--/--/----';
IF #bool THEN
INSERT
INTO
job_histry
VALUES(
NULL,
NEW.start_date,
NEW.end_date,
NEW.job_id,
NEW.department_id
) ;
END IF ;
END
$$
This is the error that i received:
#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 'INSERT BEFORE ON job_histry FOR EACH ROW BEGIN SET #bool=NEW.end_date LIKE '' at line 2
why is this happening?
I am using localhost xampp server.
You have the trigger type (INSERT) and time (BEFORE) in the wrong order.
It should read
BEFORE INSERT ON job_histry

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 mysql when creating trigger

I have a problem when creating a trigger in mysql i get 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 7
This is my first time creating triggers for mysql help
Here is the code:
CREATE TRIGGER insertAfterUser AFTER INSERT ON MEMBERS
FOR EACH ROW
BEGIN
INSERT INTO blagajna (MemberId,StanjeRacuna) VALUES(12,0.0);
END;
You need to set the delimiter
delimiter //
CREATE TRIGGER insertAfterUser AFTER INSERT ON MEMBERS
FOR EACH ROW
BEGIN
INSERT INTO blagajna (MemberId,StanjeRacuna) VALUES(12,0.0);
END;//
delimiter ;
CREATE TRIGGER `insertAfterUser ` BEFORE INSERT ON `members` FOR EACH ROW BEGIN INSERT INTO blagajna (MemberId,StanjeRacuna) VALUES('12','0.0'); END;

MySQL & trigger before insert

I need to create a trigger in my database.
The trigger must be able to analyse event before insert. In case of the last event already existing in the database has the same status, the trigger has to cancel the insert and update the last event.
I tried to create one but I can't add it in my database because of mysql errors:
#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
Here the trigger:
CREATE TRIGGER events_before_insert
BEFORE INSERT ON monitoringV2.events
FOR EACH ROW
BEGIN
DECLARE id_last_event;
DECLARE status_last_event;
SELECT id INTO id_last_event, status INTO status_last_event FROM events WHERE module_id=NEW.module_id ORDER BY serverDate DESC LIMIT 1;
IF NEW.status = status_last_event THEN
UPDATE events SET serverDate=NOW() WHERE id=id_last_event;
signal sqlstate '00000' set message_text = "Update instead of insert";
END IF;
END;
Here are information server:
PHP 5.4.6 VC9
Apache 2.4.2 VC9
MySQL 5.5.27
PhpMyAdmin 3.5.2.2
Thanks for your help
Regards,
Dorine M.
You will need to delimit your create statement:
delimiter //
CREATE...
...
END;
//
DELIMITER ;
and you will probably need to define a type for each variable declared, e.g.
DECLARE x INT;
You have to change DELIMITER before creating a trigger like this
DELIMITER $$
CREATE TRIGGER...
...
END;
$$
DELIMITER ;
You might not need to implement a trigger for this. The operation you are describing is formally known as UPSERT, i.e. update or insert.
This is how it's done in MySQL and you could also look at REPLACE.
Here is another question covering this.