I know there is plenty of answers related to my issue, but I could not get to the solution yet.
I created an event that must delete rows on a table (table_name) when 730 days pass since an specific date (spec_date).
DELIMITER $$
CREATE EVENT `delete_old_clients`
ON SCHEDULE EVERY 1 MINUTE STARTS '2022-10-13 00:00:00'
ON COMPLETION PRESERVE
DO BEGIN
delete from table_name
where datediff(now(), spec_date)>730;
END;$$
DELIMITER ;
GLOBAL event_scheduler is ON and processlist is working well:
5 event_scheduler localhost Daemon 53 Waiting for next activation
But for some reason rows are not being deleted. Am i missing sth?
Thank you in advance
Related
Hello is possible INSERT row to table with expiration date and when the expiration date is reached so row are automatically deleted? Or i only must create one column with expiration date and when sorting checking this value for ignore?
Your need looks more like an Event
IF you want not want to add expiration date column than
CREATE EVENT delete_event ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 1 DAY
ON COMPLETION PRESERVE
DO BEGIN
DELETE FROM Your_table WHERE date>= logic for expiration_date ;
END;
otherwise
DELIMITER $$
CREATE EVENT Delete ON SCHEDULE EVERY 1 DAY
DO
BEGIN
DELETE FROM TABLE WHERE `date_expires` < CURRENT_TIMESTAMP();
END$$
DELIMITER
;
NOTE that MySQL Event Scheduler need to be enabled on your server:
SET GLOBAL event_scheduler = ON;
You cannot do this directly in the database. You can do this by scheduling an event or job that does the delete. You can schedule an event for each deletion.
I think a better approach, though, is to use a view:
create view v_t as
select t.*
from t
where expiration_date > now();
This ensures that the row is not available at the instant the expiration date is no longer in the future. You can then remove the rows at your leisure. Deleting rows can put a load on the database, so you might want to wait until the load is lighter.
Hello I have this table and I want to do a query to be done daily
Select UID from the table where days<31
and update the table set IS_END= 'Y' where days>31 and IS_END ='N'
I have tried to use MYSQL EVENT function but it doesn't work with me.
MySQL Table photo
Hello Ahmed and Welcome to SO.
Actually you can do it via MySQL Event. First you need to enable the event scheduler by:
SET GLOBAL event_scheduler = ON;
Then you can add an event for your update query:
CREATE EVENT
ON SCHEDULE EVERY 1 DAY
STARTS '2018-10-29 00:00:00'
DO
UPDATE <table_name> SET IS_END='Y' WHERE days>31 and IS_END='N';
I'd like to delete a row by ID number from a MySQL database after a specific amount of time have passed since the row was added to the database.
How can I do it? I'm a total beginner :)
Thank you.
CREATE EVENT IF NOT EXISTS `dbName`.`eventName`
ON SCHEDULE
EVERY 1 DAY // or 1 HOUR
COMMENT 'Description'
DO
BEGIN
DELETE FROM `dbName`.`TableName` WHERE `DateCol` < NOW();
END
turn on the event scheduler before using events
SET GLOBAL event_scheduler = ON;
Sorry, don't really know much about this stuff!
I have a table with a few columns, one of them is called 'expirationday' and every row has its unix timestamp for this column.
I would like to know, if at all possible, how to delete a row when the time in the 'expirationday' column (for that row) is reached.
Please note that it does not need to delete rows accurately to the second, a couple of hours is fine.
script.sh
Create a shell script like this:
#!/bin/bash
mysql --user=[username] --password=[password] --database=[db name] --execute="DELETE FROM tbl_name WHERE expiration_date < NOW()
Create a cron job that executes the (it will run every 30 min)
type
crontab -e
Add:
0,30 * * * * /path/script.sh
It's worth looking at the MySQL event scheduler. You could schedule a job to run for example every half hour.
It could then call a DELETE statement directly or to call a MySQL Function or Procedure.
DELIMITER $$
CREATE
EVENT `deleteExpire`
ON SCHEDULE EVERY 1 HOUR STARTS '2015-01-01 00:00:00'
DO BEGIN
DELETE FROM mytable
WHERE expirationday < UNIX_TIMESTAMP();
END */$$
DELIMITER ;
Also see this for more example usage.
delimiter $$
CREATE TRIGGER carslibrary_trigger
AFTER insert ON carslibrary
FOR EACH ROW
BEGIN
insert into facerecord (carslibrary_ID) values (new.CarID);
END$$
delimiter;
i am trying to delete my trigger
Showing rows 0 - 0 (1 total, Query took 0.0087 sec)
SELECT trigger_name
FROM information_schema.triggers
WHERE event_object_table = 'carslibrary'
AND action_timing = 'AFTER'
AND event_manipulation = 'INSERT'
LIMIT 0 , 30
drop trigger carslibrary_trigger;
#1360 - Trigger does not exist
DROP trigger carslibrary_trigger;
my query returned a record, but it can't drop?
why i can't drop my trigger?
i also made a general query still returns 1 record
Showing rows 0 - 0 (1 total, Query took 0.0606 sec)
SELECT *
FROM TRIGGERS
LIMIT 0 , 30
What version of MySQL? You may have to include table name.
DROP TRIGGER carslibrary.carslibrary_trigger;
If anyone get this issue using MyISAM engine, check the TRN files in the data folder, in my case those were there, even though I deleted the trigger using command line.
Manually Deleting TRN files of the triggers that are throwing error 1360 will solve the issue.
if the error trigger does not exist with error code 1360 occurs,
simply execute the triggers from command line
mysql -u root -p
then mysql> execute the triggers
then check the information_schema in mysql
the triggers can be viewed there
just edit table data