How to make delayed DELETE SQL query? - mysql

I need to delete rows from table 14 days after the DELETE query has been made. Is it possible to do this using MySQL?

You can use the built-in MySQL Event Scheduler to schedule a query or a stored routine to run at an arbitrary point in time:
This is an example of a minimal CREATE EVENT statement:
CREATE EVENT myevent
ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 1 HOUR
DO
UPDATE myschema.mytable SET mycol = mycol + 1;
Please mind, the Event Scheduler is disabled by default.

Related

MYSQL: Create row in table with expiration date

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.

Executing an SQL query every hour

How can i make the following query execute each hour automatically?
SELECT count(*)
FROM user_tab_columns
WHERE table_name='MYTABLE'
Usually in a DBMS exist some schedule of instrument. In Oracle the instrument is DBMS_SCHEDULER.
MySQL same have scheduler. You should check global variable event_scheduler=1. Then you can create schedule:
CREATE EVENT 'new_event'
ON SCHEDULE EVERY 1 HOUR STARTS CURRENT_TIMESTAMP
ON COMPLETION NOT PRESERVE
ENABLE
COMMENT '' DO
call new_proc();
where new_proc() is procedure with your query.

Is it possible to "decay" SQL information?

Let's say I have rows of information in an SQL database.
I want to have rows of information that are older than 30 days to be automatically removed.
Is this possible?
Additional information:
I am using the SQL date function provided to collect the dates.
This problem has two aspects:
How to schedule
Actual SQL Statement
How to Schedule
You have multiple options, but all of this revolves around whether you will schedule, or whether you will have a stored procedure triggered.
Three options
Use MySql event scheduling: http://dev.mysql.com/doc/refman/5.1/en/events-overview.html
Schedule through an operating system (CRON, SCHEDULED TASK) the execution of an app using SQL (either PHP script, java etc)
Have a trigger that is executes a stored procedure every time a change is made to a table.
I would propose options 1 and 2 are the best.
SQL Statement
The actual SQL statement is quite easy provided you have a field (e.g. dateField) that represents insertDate...
delete from myTable where insertDate < DATE_SUB(NOW(), INTERVAL 1 MONTH);
You can easily generate this value using the NOW() function within an insert statement, for example:
insert into myTable values (NOW(), 'value1', 'value2', ... , 'valueN');
Scheduling Using Event Scheduler at End of Day
If you have the CREATE EVENT privilege this will work through PHP MyAdmin.
See also: http://www.sitepoint.com/how-to-create-mysql-events/
CREATE EVENT `clean_up2`
ON SCHEDULE EVERY 1 DAY STARTS CURRENT_DATE
DO
delete from `data` where updated_on < DATE_SUB(NOW(), INTERVAL 1 MONTH);
END;
Yes. In SQL Server you could create a stored procedure to perform the delete, and create a SQL Agent job that runs every day (or a schedule of your choosing)

Creation of event in MySQL

Can any one help me how to create an event in MySQL so that it has to be executed for every 10 seconds?
I have tried with the following code but it is inserting only once but when i see whether the event is running of not in process list it is in running state
create event e_insert on schedule at 'timestamp' + interval 1 second do insert into 'table_name' values (now());
You should write -
ON SCHEDULE EVERY '10' SECOND
instead of on schedule at...
Using the Event Scheduler.
Edit:
CREATE EVENT event1
ON SCHEDULE EVERY '10' SECOND
STARTS CURRENT_TIMESTAMP
ON COMPLETION PRESERVE
DO
BEGIN
INSERT INTO...
END
set global event_scheduler = 1;
check this here

Insert row for X time

I have a problem:
How I can insert a row and delete it X time?
I think something like INSERT the value whit a time stamp and add a task to delete it.
But I don't have idea how to add MySQL task and if here is a better method.
you need to use event scheduler from MySQL:
the basic about MySQL scheduler is:
CREATE EVENT myevent
ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 1 HOUR
DO
UPDATE myschema.mytable SET mycol = mycol + 1;
Source: http://dev.mysql.com/doc/refman/5.1/en/create-event.html
tutorial: http://www.sitepoint.com/how-to-create-mysql-events/
Guide: http://dev.mysql.com/doc/refman/5.1/en/events.html