Insert row for X time - mysql

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

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.

How to delete a database row in MySQL?

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;

SQL: How can I delete a row when a unix time is reached?

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.

How to make delayed DELETE SQL query?

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.

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