Drop a MySQL Table after x hours?


You need to create event to drop table after x hours. The syntax is as follows −

CREATE EVENT yourEventName
ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL x HOUR
DO
DROP TABLE IF EXISTS yourTableName;

Let us first create a table −

mysql> create table DemoTable
(
   StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   StudnetFirstName varchar(20),
   StudentLastName varchar(20),
   StudnetAge int
);
Query OK, 0 rows affected (0.52 sec)

Now implement the above event in order to drop table after 2 hours −

mysql> CREATE EVENT drop_table_event_after2HoursDemo
ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 2 HOUR
DO
DROP TABLE IF EXISTS DemoTable;
Query OK, 0 rows affected (0.17 sec)

Now the table ‘DemoTable’ will drop after 2 hours.

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 30-Jul-2019

84 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements