AUTO-DELETE ROW 1 Day After Inserted (MySQL) - mysql

How to create event Auto-Delete some row 1 day after inserting that row?
I have 3 fields:
id varchar
name varchar
timestamp current_timestamp()

Please take a look at Event: https://dev.mysql.com/doc/refman/5.7/en/events-syntax.html
in your case:
you can try this one:
-- create a timer to update this table automatically
DROP EVENT IF EXISTS `et_update_your_trigger_name`;
CREATE EVENT `et_update_your_trigger_name` ON SCHEDULE EVERY 1 MINUTE
STARTS '2010-01-01 00:00:00'
DO
DELETE FROM `DB_NAME`.`table_name` where DATEDIFF(now(),`timestamp`) > 1;
ALTER EVENT `et_update_your_trigger_name` ON COMPLETION PRESERVE ENABLE;
this sql code create a trigger, and execute every minutes.

Here's your scripts.
Insert into table1 (id, name, timestamp) values (1, 'test', now())
after insert
Delete from table1 WHERE timestamp < now() - interval 1 day;

Please try like this:
DELETE FROM table1
WHERE date < DATE_SUB(NOW(), INTERVAL 1 DAY)

Related

Automatically update columns values after 6 days

I created a new table in MySQL, I want to make changes in ( column1 and column2 ) columns values automatically in sql.
I tried this code, but their is no changes in table after 6 days.
CREATE EVENT weekly
ON SCHEDULE
EVERY 6 days
STARTS DATE(NOW()) + INTERVAL (6 DAYS(NOW())+1) DAY
ON COMPLETION PRESERVE
DO
UPDATE INTO `MyTable` (`value1`, `value2`)
SELECT `column1` , `column2`)
WHERE `column1` >= NOW() - INTERVAL 6*24 HOUR;
Your UPDATE statement is not correct. It should be like below -
UPDATE `MyTable`
SET `column1` = ???
,`column2` = ???
WHERE `column1` >= NOW() - INTERVAL 6*24 HOUR;
Replace ??? with the the value you want to update in col1 and col2
Not sure how you want to update the value for column1 and 2. But the event should look like this:
DELIMITER //
CREATE EVENT IF NOT EXISTS daily_update
ON SCHEDULE
EVERY 1 DAY STARTS DATE(NOW())
ON COMPLETION PRESERVE
COMMENT 'Update myTable after 6 days'
DO
BEGIN
UPDATE MyTable
SET column1='value1',
column2='value2'
WHERE column1>= (NOW() - INTERVAL 6*24 HOUR);
END
//

How to validate an inserted record before a delete on a Mysql Event

I have the following MySql event that works perfect but I would like to validate if the record was inserted before the delete just to make sure that I won't loose the record.
This is the MySql event:
DELIMITER |
CREATE
EVENT IF NOT EXISTS respaldoRegRawInformeAperturaLocal
ON SCHEDULE EVERY 1 DAY STARTS '2017-08-03 22:00:00'
DO BEGIN
-- INSERT INTO BACKUP TABLE
INSERT INTO regRawInformeAperturaLocalBACKUP (regRawInformeAperturaLocalId, rawInformeAperturaLocal, done, date, deviceId)
-- GET DATA
SELECT regRawInformeAperturaLocalId, rawInformeAperturaLocal, done, date, deviceId
FROM regRawInformeAperturaLocal
WHERE date < DATE_SUB(NOW(), INTERVAL 5 MONTH);
-- DELETE DATA FROM ORIGINAL TABLE
DELETE FROM regRawInformeAperturaLocal WHERE date < DATE_SUB(NOW(), INTERVAL 5 MONTH);
END|
DELIMITER ;
Is there any way to validate if the records were inserted before the delete?

Syntax error with MySql Event

I'm trying to create this event but I can't find the syntax error.
CREATE
EVENT respaldoRegRawInformeAperturaLocal
ON SCHEDULE EVERY 1 DAY STARTS '2017-08-03 22:00:00'
DO BEGIN
-- INSERT INTO BACKUP TABLE
INSERT INTO regRawInformeAperturaLocalBACKUP (regRawInformeAperturaLocalId, rawInformeAperturaLocal, done, date, deviceId)
-- GET DATA
SELECT regRawInformeAperturaLocalId, rawInformeAperturaLocal, done, date, deviceId
FROM regRawInformeAperturaLocal
WHERE regRawInformeAperturaLocal.date < DATE_SUB(NOW(), INTERVAL 5 MONTH);
-- DELETE DATA FROM ORIGINAL TABLE
DELETE FROM regRawInformeAperturaLocal WHERE regRawInformeAperturaLocal.date < DATE_SUB(NOW(), INTERVAL 5 MONTH);
END;
[CODE EDIT 1]
CREATE
EVENT respaldoRegRawInformeAperturaLocal
ON SCHEDULE EVERY 1 DAY STARTS '2017-08-03 22:00:00'
DO BEGIN
-- INSERT INTO BACKUP TABLE
INSERT INTO regRawInformeAperturaLocalBACKUP (regRawInformeAperturaLocalId, rawInformeAperturaLocal, done, date, deviceId)
-- GET DATA
SELECT regRawInformeAperturaLocalId, rawInformeAperturaLocal, done, date, deviceId
FROM regRawInformeAperturaLocal
WHERE date < DATE_SUB(NOW(), INTERVAL 5 MONTH);
-- DELETE DATA FROM ORIGINAL TABLE
DELETE FROM regRawInformeAperturaLocal WHERE date < DATE_SUB(NOW(), INTERVAL 5 MONTH);
END;
[EDIT]
This is the syntax error message:
[Err] 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 11
I could make it work:
I have to add the DELIMITER and change the syntaxis from END; to END|
Anyway here's the code:
DELIMITER |
CREATE
EVENT IF NOT EXISTS respaldoRegRawInformeAperturaLocal
ON SCHEDULE EVERY 1 DAY STARTS '2017-08-03 22:00:00'
DO BEGIN
-- INSERT INTO BACKUP TABLE
INSERT INTO regRawInformeAperturaLocalBACKUP (regRawInformeAperturaLocalId, rawInformeAperturaLocal, done, date, deviceId)
-- GET DATA
SELECT regRawInformeAperturaLocalId, rawInformeAperturaLocal, done, date, deviceId
FROM regRawInformeAperturaLocal
WHERE date < DATE_SUB(NOW(), INTERVAL 5 MONTH);
-- DELETE DATA FROM ORIGINAL TABLE
DELETE FROM regRawInformeAperturaLocal WHERE date < DATE_SUB(NOW(), INTERVAL 5 MONTH);
END|
DELIMITER ;
Now I have to test it but I could create it so I guess It's fine.
[EDIT]
The event works like a charm.

phpmyadmin: database events

I'm trying to move records from one table (upcoming) to another table (today) when the date of the record (from upcoming) equals today's date. How would I go about doing so using recurring events in phpmyadmin?
You certainly can use a MySQL event that runs at (actually shortly after) midnight each day. You can do whatever you want in that event. Here's an example.
CREATE EVENT `midnight_event`
ON SCHEDULE
EVERY 1 DAY STARTS '2015-10-10 00:00:00'
ON COMPLETION PRESERVE
DISABLE ON SLAVE
DO BEGIN
START TRANSACTION;
INSERT
INTO todays_table
VALUES (col, col, col, col)
SELECT col, col, col
FROM pending_table
WHERE time_stamp >= CURDATE()
AND time_stamp < CURDATE() + INTERVAL 1 DAY;
DELETE
FROM pending_table
WHERE time_stamp >= CURDATE()
AND time_stamp < CURDATE() + INTERVAL 1 DAY;
COMMIT;
END
Others have commented about the wisdom of this sort of data design, with rows moving from table to table. There's considerable merit in their comments. If you want a "table" with today's events in it, consider a view. This one will look just like the table you maintain with the event I mentioned.
CREATE OR REPLACE VIEW todays_table AS
SELECT *
FROM pending_table
WHERE time_stamp >= CURDATE()
AND time_stamp < CURDATE() + INTERVAL 1 DAY

Create Trigger to delete rows that are >90 days old

Trying to create a trigger that will delete any record that is 90 days old. I used a trigger statement from stackoverflow that I have found and changed the statement a bit, but in my MySQL Workbench, I am getting a syntax error. I cannot seem to figure what is wrong.
Below is my query:
create trigger user_connections_dump after insert on user_connections
for each row
begin
delete from connection_time where Date('2014-06-09') > Date('now','-90 days')
end;
Your need looks more like an Event than a Trigger.
CREATE EVENT IF NOT EXISTS `Clean_Older_Than_90_days_logs`
ON SCHEDULE
EVERY 1 DAY_HOUR
COMMENT 'Clean up log connections at 1 AM.'
DO
DELETE FROM log
WHERE log_date < DATE_SUB(NOW(), INTERVAL 90 DAY)
References:
MySQL Event Scheduler on a specific time everyday
CREATE TRIGGER user_connections_dump
AFTER INSERT ON user_connections
FOR EACH ROW
DELETE FROM log
WHERE log_date < DATE_SUB(NOW(), INTERVAL 90 DAY)
You should be comparing the date column in the log table, not a literal date. Then you use DATE_SUB to subtract dates, not the Date function.
CREATE EVENT IF NOT EXISTS `Delete_Older_Than_90_Days`
ON SCHEDULE EVERY 1 DAY
STARTS STR_TO_DATE(DATE_FORMAT(NOW(),'%Y%m%d 0100'),'%Y%m%d %H%i') + INTERVAL 1 DAY
DO
DELETE LOW_PRIORITY FROM log WHERE log_date < DATE_SUB(NOW(),INTERVAL 90 DAY)