简体   繁体   English

如果没有记录,则设置事件以删除表

[英]Set the event to drop the table if no records exist

I am trying to create a table bus with 2 events. 我正在尝试创建具有2个事件的表bus One AutoDelete to maintain the data in the bus table (This one works) and the other one to delete the table if no records exist in the table. 一个自动AutoDelete用于维护总线表中的数据(此方法有效),另一个自动删除表(如果表中不存在记录)。 AutoDelete I dont know how can I set the condition as SELECT COUNT(*) FROM bus in the event DropBusTable to get it to work since I dont have time as the other one I have condition. AutoDelete我不知道如何在DropBusTable事件DropBusTable条件设置为SELECT COUNT(*) FROM bus使其工作,因为我没有时间与其他条件一样。

I appreciate any help. 感谢您的帮助。

   stt.execute("CREATE TABLE IF NOT EXISTS bus"
            + "(id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,"
            + "mac VARCHAR(30) NOT NULL UNIQUE,"
            + "route int(11) NOT NULL,"
            + "latitude FLOAT(10,6) NOT NULL,"
            + "longitude FLOAT(10,6) NOT NULL,"
            + "created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP)");

    stt.execute("CREATE EVENT IF NOT EXISTS  AutoDelete "
            + "ON SCHEDULE EVERY 3 MINUTE "
            + "DO "
            + "DELETE FROM bus WHERE created_at < (NOW() - INTERVAL 3 MINUTE)");

    // I tried this statement here but it does not work.
    stt.execute("CREATE EVENT IF NOT EXISTS  DropBusTable "
            + "Do "
            + "DROP TABLE IF EXISTS  bus");

You can reset the counter with: 您可以使用以下方法重置计数器:

ALTER TABLE <table_name> AUTO_INCREMENT = 1

by adding it to your existing task: 通过将其添加到您现有的任务中:

  stt.execute("CREATE EVENT IF NOT EXISTS  AutoDelete "
            + "ON SCHEDULE EVERY 3 MINUTE "
            + "DO BEGIN"
            + "DELETE FROM bus WHERE created_at < (NOW() - INTERVAL 3  MINUTE);"
            + "ALTER TABLE bus AUTO_INCREMENT = 1 ;"
            + "END;");

See this article for more info. 有关更多信息,请参见本文

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM