MySQL Trigger Is Not Inserting Date/Time String Into Column - mysql

Here's my trigger. Updating a row in event does not set a date/time string in sf_timestamp. New to triggers so I am not sure how to debug.
DELIMITER $$
CREATE DEFINER = `root`#`localhost` TRIGGER `event_stamp`
BEFORE UPDATE ON `event`
FOR EACH ROW
BEGIN
SET NEW.sf_timestamp = DATE_FORMAT(NOW(),'%Y-%m-%d %T');
END;;
DELIMITER $$
Suggestions?

When you define a DELIMITER, you need to keep that same delimiter until you are done with the block you are trying to declare. In this case, you are trying to create a new trigger and you need MySQL to interpret the whole block with multiple lines as one and ending them with the default ;
But when you are done, you have to END the block with the DELIMITER you have set earlier as following:
DELIMITER $$
CREATE DEFINER = `root`#`localhost` TRIGGER `event_stamp`
BEFORE UPDATE ON `event`
FOR EACH ROW
BEGIN
SET NEW.sf_timestamp = DATE_FORMAT(NOW(),'%Y-%m-%d %T');
END$$
DELIMITER ;

Unless your update statement includes a value for sf_timestamp, that field likely doesn't have a value to be formatted. If you are attempting to update the timestamp to represent the time when the record is updated, why not just use the current time?
DELIMITER $$
CREATE DEFINER = `root`#`localhost` TRIGGER `event_stamp` BEFORE UPDATE ON `event`
FOR EACH ROW
BEGIN
SET NEW.sf_timestamp = now();
END;;
DELIMITER $$

DELIMITER $$
CREATE DEFINER = `root`#`localhost` TRIGGER `event_stamp`
BEFORE UPDATE ON `event`
FOR EACH ROW
BEGIN
SET NEW.sf_timestamp = DATE_FORMAT(NOW(),'%Y-%m-%d %T');
END$$
DELIMITER ;
Followed #pensums suggestion and this worked for me

Related

Problems creating TRIGGER in MySQL

I am first time using trigger concept in my MYSQL workbench like below query,
First I created people table by using below query,
CREATE TABLE people (age INT, name varchar(150));
Then I used below query for trigger
DELIMITER
CREATE TRIGGER agecheck BEFORE INSERT ON raptor1_5.people FOR EACH ROW IF NEW.age < 0 THEN SET NEW.age = 0; END IF;
DELIMITER ;
But agecheck trigger not creating to people table. Its does not show any error message when I run this query.
Here is my table images,
Updated : based on the answer
Try with this.
USE `raptor1_5`;
DELIMITER $$
DROP TRIGGER IF EXISTS raptor1_5.agecheck$$
USE `raptor1_5`$$
CREATE TRIGGER agecheck BEFORE INSERT ON people
FOR EACH ROW
BEGIN
IF NEW.age < 0 THEN
SET NEW.age = 0;
END IF;
END$$
DELIMITER ;
The standard trigger syntax should be as
DELIMITER //
CREATE TRIGGER agecheck BEFORE INSERT ON raptor1_5.people
FOR EACH ROW
BEGIN
IF NEW.age < 0 THEN
SET NEW.age = 0;
END IF;
END;//
DELIMITER ;
The above is when you run on mysql cli, however in phpmyadmin or workbench check if you have an option to set the delimiter if yes choose that and remove the delimiter part from the above.

Trigger not checking if condition

I have created a trigger , and this trigger updating the field value , but not checking the if condition ,
DELIMITER $$
create trigger `njsystem`.`test` BEFORE UPDATE on `njsystem`.`tbl_users`
for each row
BEGIN
IF (NEW.user_failed_logins > 3) THEN
UPDATE tbl_users SET user_active = 0;
END IF;
END; $$
DELIMITER ;
What I understand from your problem statement is that, you wanted to set user_active to 0 if user_failed_logins count goes above 3. Here is solution for that, you can also change values using new.
DELIMITER $$
create trigger `test` BEFORE UPDATE on `tbl_users`
for each row
BEGIN
IF (NEW.user_failed_logins > 3) THEN
SET NEW.user_active = 0;
END IF;
END; $$
DELIMITER ;

MYSQL trigger error 1064

I would like to check if a record is being created with a user id, if not then generate one. I can't figure out what I'm doing wrong?
delimiter $$
DROP TRIGGER IF EXISTS init_uuid_users;
CREATE TRIGGER init_uuid_users BEFORE INSERT ON `users`
FOR EACH ROW BEGIN
IF (NEW.username IS NULL) THEN
SET NEW.username = UUID();
END IF;
END;
$$
delimiter ;
DROP TRIGGER IF EXISTS init_uuid_users;
put it before delimiter.
And remove ; after END.

How can I avoid an infinite loop of these trigger updates recursing?

I want to have the following 3 triggers:
#START TRIGGER
delimiter $$
CREATE TRIGGER `lastedited` AFTER UPDATE ON `eng-jap`
FOR EACH ROW
BEGIN
UPDATE `mrhowtos_main`.`eng`
SET `english` = new.eng WHERE `english` = old.eng;
UPDATE `mrhowtos_main`.`jap`
SET `japanese` = new.jap WHERE `japanese` = old.jap;
END;
$$
#START TRIGGER
delimiter $$
CREATE TRIGGER `lastedited2` AFTER UPDATE ON `eng`
FOR EACH ROW
BEGIN
UPDATE `mrhowtos_main`.`eng-jap`
SET `eng-jap`.`eng` = new.english WHERE `eng-jap`.`eng` = old.english;
END;
$$
#START TRIGGER
delimiter $$
CREATE TRIGGER `lastedited3` AFTER UPDATE ON `jap`
FOR EACH ROW
BEGIN
UPDATE `mrhowtos_main`.`eng-jap`
SET `eng-jap`.`jap` = new.japanese WHERE `eng-jap`.`jap` = old.japanese;
END;
$$
but before i even create them i see that there will be this infinite looping of the triggers as soon as any of the tables get updated. Basically i do not want the triggers UPDATE to "trigger" the other triggers. How do I do that?

sql trigger is not working help me out

delimiter $$
drop TRIGGER if EXISTS upflttyprateTrig
create TRIGGER upflttyprateTrig
AFTER UPDATE ON flttyprate
FOR EACH ROW BEGIN
INSERT INTO `histflttyprate` (
`flttyprate_Id`,
`flttyprate_Flttyp_Id_Fk`,
`flttyprate_Date_Eff`,
`flttyprate_Date_Ineff`,
`flttyprate_IFR`,
`flttyprate_Single`,
`flttyprate_Multi`,
`flttyprate_Rate_Per_Hr`,
`flttyprate_Night_Surchage`,
`flttyprate_Status`,
`flttyprate_Token`)
VALUES (
NEW.flttyprate_Id,
NEW.flttyprate_Flttyp_Id_Fk,
NEW.flttyprate_Date_Eff,
NEW.flttyprate_Date_Ineff,
NEW.flttyprate_IFR,
NEW.flttyprate_Single,
NEW.flttyprate_Multi,
NEW.flttyprate_Rate_Per_Hr,
NEW.flttyprate_Night_Surchage,
NEW.flttyprate_Status,
NEW.flttyprate_Token);
END$$
is not working why what is actual syntax mysqlversion 5.0
The 'IF EXISTS' clause is not supported in DROP TRIGGER statement.
And add a delimiter after 'DROP TRIGGER...' statement.
EDIT
Read the information about trigger's existance from information_schema.triggers table; then drop trigger. Do it in the application or write a stored procedure.
Example:
DELIMITER $$
CREATE PROCEDURE drop_trigger()
BEGIN
SET #exist = NULL;
SELECT 1
INTO
#exist
FROM
information_schema.triggers
WHERE
trigger_schema = 'test'
AND trigger_name = 'trigger1';
IF #exist IS NOT NULL THEN
DROP TRIGGER test.trigger1;
END IF;
END
$$
DELIMITER ;