Drop a MySQL Table after x hours?

MySQLMySQLi Database

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.

raja
Published on 29-Apr-2019 07:07:49
Advertisements