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.
Related
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;
I have a database containing the name and the associated birthday. I have set an event scheduler to periodically check the database for bithdays within two days from the current date. This is my code-
DELIMITER $$ CREATE EVENT `Reminder` ON SCHEDULE
EVERY 1 Day
ON COMPLETION PRESERVE
ENABLE
DO BEGIN
select * from reminder where date_format(date_sub(Birthday,interval 2 day),'%m-%d') = Date_Format(curdate(),'%m-%d');
END $$
DELIMITER ;
Apparently the sql query does not display any information. I would like to display the result of the query in an interface, whenever the query returns at least one row.
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.
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.
I am creating a room reservation in php.
I have two tables:
table RESERVE
reserve_id
room_id
room_reserve_qty
checkout
table ROOM
room_id
room_qty
when the "checkout" date reached the date today,that row in the reserve table will be deleted.Actually I already have the event for that :
DROP EVENT `auto_delete_chckout`;
CREATE DEFINER=`root`#`localhost`
EVENT `auto_delete_chckout`
ON SCHEDULE EVERY 1 MINUTE STARTS '2013-01-26 13:09:15'
ON COMPLETION NOT PRESERVE ENABLE DO
DELETE FROM reserve WHERE checkout <= CURDATE()
so my question is :
If a reservation is being deleted , its room_reserve_qty must be added in the room_qty in room table.
before you delete the records on table reserve, you need to update the records of table room first so you will know which room_id will be updated, eg
UPDATE room a
INNER JOIN reserve b
ON a.room_id = b.room_id
SET a.room_qty = a.room_qty + b.room_reserve_qty
WHERE b.checkout <= CURDATE();
DELETE FROM reserve WHERE checkout <= CURDATE();
full code:
DROP EVENT `auto_delete_chckout`;
DELIMITER $$
CREATE EVENT `auto_delete_chckout`
ON SCHEDULE EVERY 1 MINUTE STARTS '2013-01-26 13:09:15'
ON COMPLETION NOT PRESERVE ENABLE
DO
BEGIN
UPDATE room a
INNER JOIN reserve b
ON a.room_id = b.room_id
SET a.room_qty = a.room_qty + b.room_reserve_qty
WHERE b.checkout <= CURDATE();
DELETE FROM reserve WHERE checkout <= CURDATE();
END $$
DELIMITER ;
UPDATE
Since you mentioned that you are new, Event, I think, causes to much work on the server. A TRIGGER might be the other way. A Trigger is basically a block of code that is fired every time an event (before or after) is happening on the table, eg: INSERT, UPDATE and DELETE.
First of all you probably don't need to fire your event every minute, since you check only for a date part. You might want to do that once a day.
Secondly I would put updating room_qty logic in a trigger. That way no matter how you delete your reservation, your room_qty will be correct.
Your trigger might look like this
DELIMITER $$
CREATE TRIGGER reserve_before_delete BEFORE DELETE ON reserve
FOR EACH ROW BEGIN
UPDATE room
SET room_qty = room_qty + OLD.room_reserve_qty
WHERE room_id = OLD.room_id;
END;
$$
DELIMITER ;
Your DELETE code stays intact when you use a trigger.