Sorry if this is a noob question as I am one right now but
I am seem to be having a problem on the mysql trigger. I am trying to create a trigger that will automatically updates the data dateValid with an interval of 5 minutes from the dateCreated's data after insert.
DROP TRIGGERIF EXISTS `restrictionotp`;TRIGGER `add_interval` beforeINSERT
on `date` FOR each row
UPDATE date
SET datevalid=date_add(new.datecreated, interval 5 minute)
WHERE dateid=new.dateid
The code above was successfully been created but once I try to insert a data on the said table this error shows up
Can't update table 'verification' in stored function/trigger because
it is already used by statement which invoked this stored function/trigger
Thanks for answering my question
Just set the new value in the trigger. The database will then insert the modified value instead of the original.
DELIMITER //
CREATE TRIGGER Add_Interval
BEFORE INSERT ON verification
FOR EACH ROW
BEGIN
SET new.datevalid = new.datecreated + INTERVAL 5 MINUTE;
END;
//
DELIMITER ;
Related
Firstly let me say I'm new to and just getting my head around MySQL and finding date manipulation has its challenges. It seems to me it should be possible to have three columns:
column A contains date member joined 2020-01-12 for example, mapped from a form.
column B contains the length of membership in years 1 or 5, currently entered manually
Then calculate expiry 'date A'+ 'integer B' Year inserted in column C on member creation or update
It's OK to do manually but I feel it should be something automatic.
If anyone can give me a start or point to a tutorial that might help I'd be grateful.
In MySql you can use DATE_ADD() funciton:
SELECT columnA, columnB, DATE_ADD(columnA, INTERVAL columnB YEAR) AS EXPIRY;
Use this web page as reference.
You could define triggers BEFORE INSERT and/or BEFORE UPDATE which will do the job for you:
BEFORE INSERT trigger:
DROP TRIGGER IF EXISTS before_insert;
DELIMITER $$
CREATE TRIGGER before_insert
BEFORE INSERT ON `my_table`
FOR EACH ROW
BEGIN
SET NEW.`valid` = DATE_ADD(NEW.`date`, INTERVAL NEW.`membership` YEAR);
END$$
DELIMITER ;
BEFORE UPDATE trigger:
DROP TRIGGER IF EXISTS before_update;
DELIMITER $$
CREATE TRIGGER before_update
BEFORE UPDATE ON `my_table`
FOR EACH ROW
BEGIN
SET NEW.`valid` = DATE_ADD(NEW.`date`, INTERVAL NEW.`membership` YEAR);
END$$
DELIMITER ;
It is quite enough to insert both date and membership values during insert (obviously) or to update membership value on already inserted record(s).
Your Column C is the valid column in both queries.
Error: Can't update table 'tbl' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
Trigger before insert code:
CREATE DEFINER=`root`#`localhost` TRIGGER `opcdls`.`tblTrigger`
BEFORE INSERT ON `tbl` FOR EACH ROW
BEGIN
DELETE FROM tbl
WHERE ProxyLoggingDate <= DATE_SUB(NOW(), INTERVAL 7 WEEK);
END
I want to create a trigger where before each insert check each row and delete the old.
But when I try to add a ro on this table it gives me below error
Can't update table 'tbl' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
What does this error mean?
Thanks
Instead a trigger i create a event and it is working properly.
SET GLOBAL event_scheduler = ON;
CREATE EVENT IF NOT EXISTS tbl
ON SCHEDULE EVERY 1 MINUTE STARTS NOW()
DO DELETE FROM tbl WHERE ProxyLoggingDate <= DATE_SUB(NOW(), INTERVAL 7 WEEK);
I have a MySQL table with the following data
id INT
time_stamp timestamp
status INT
When inserting, the time_stamp attribute will always be current_timestamp, once the current timestamp is more than 48 hrs of the value inserted in the table, i need to change the status to 0.
I created the following trigger:
DELIMITER $$
CREATE TRIGGER new_table_AINS
BEFORE INSERT ON new_table
FOR EACH ROW begin
DECLARE now_value timestamp;
SET #now_value = current_timestamp;
UPDATE new_table SET new.status = 0 where #now_value = new.time_stamp + interval 48 hour;
END$$
But when I try to insert a new row, I get Error Code 1442 in MySQL
Error Code: 1442. Can't update table 'new_table' in stored
function/trigger because it is already used by statement which invoked
this stored function/trigger. Please help me.
Thanks a lot!
In a "before" trigger, you cannot re-modify the table that you are triggering on. However, I don't think you need a trigger. Instead, use a view:
create view v_ new_table_AINS as
select . . .,
(case when current_timestamp >= time_stamp + interval 48 hour then 0
else status
end)
from new_table_AINS;
At your leisure, you can then run a job that updates the status to zero for old timestamps.
Well, I have researched and have not found a solution to the following problem 'SQL Error (1442): Can not update table' messages' in stored function / trigger because it is already used by statement Which invoked this stored function / trigger. `
My trigger is created, only when I run the INSERT on the table of messages, this error is thrown, my trigger is
DELIMITER $$
DROP TRIGGER IF EXISTS `onMessage` ;
CREATE TRIGGER `onMessage` BEFORE INSERT ON `messages` FOR EACH ROW
BEGIN
UPDATE `users` SET `users`.`messages` = ( `users`.`messages` + 1 )
WHERE `users`.`uid` = NEW.uid ;
DELETE FROM `messages` WHERE `date` < ( NOW( ) - INTERVAL 1 MINUTE ) ;
END ;
$$
DELIMITER ;
This is a restriction in the use of triggers.
From the MySql documentations:
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.
Correct, for MySQL anyway
You can't write to the table associated with a trigger in that trigger. It generates all manner of inconsistencies, not least because MySQL triggers are processed row-by-row (RBAR)
In this case, I'd use a stored procedure to deal with the INSERT on messages
It is not possible to update a table for which the trigger is created in the trigger since Mysql does some locking stuff on that table. Therefore you can't insert/update/delete rows of the same table because then the trigger would called again and again, ending up in a recursion.
If you want to create a trigger on the table which will update itself (perfoming a deletion in this case), make sure you use the NEW.column_name to refer to the row after it’s updated.
Ok, I've started writing my first trigger in mysql, it doesn't give an errors, but it doesn't work either...
DELIMITER $$
DROP TRIGGER `cc`.`update_expires_date_trig`$$
CREATE TRIGGER `update_expires_date_trig` BEFORE INSERT ON `credit_test_acc`
FOR EACH ROW BEGIN
UPDATE credit_test_acc SET date_expires_acc = DATE_ADD(CURDATE(), INTERVAL 6 MONTH) WHERE type_acc = 'init'
END;
$$
DELIMITER ;
I have 2 problems:
Can't update table 'credit_test_acc' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
Is the trigger as defined going to update JUST the JUST inserted row, or EVERY row in the database?
As far as I know, it must be rewritten like this to work as you expect it to work:
DELIMITER $$
DROP TRIGGER `cc`.`update_expires_date_trig`$$
CREATE TRIGGER `update_expires_date_trig` BEFORE INSERT ON `credit_test_acc`
FOR EACH ROW BEGIN
SET NEW.date_expires_acc = DATE_ADD(CURDATE(), INTERVAL 6 MONTH)
END;
$$
DELIMITER ;
Where NEW refers to the row that is about to be inserted into the table. You didn't give any explanation as to what role 'type_acc' might play here (I can think of more than one way it could be interpreted), so I've left that out. If it is what I think it is, you can apply it like this:
IF NEW.type_acc = 'init' THEN # do whatever you want here
A Trigger cannot change the table that triggered it.
Either directly or indirectly.
You can only change values in a BEFORE trigger by SET new.field = newvalue.
And this can only effect the 'current' row that pulled the trigger (so to speak).