Search code examples
mysqlcsvsql-insertmysqlimportdatabase-trigger

MySQL trigger to drop and create table upon insert (through mysqlimport)


I am importing with mysqlimport (batch file) records to a specific table every 5 minutes, in order to get in this table online guests on my website.

I currently send 2 .sql executed on a Windows batch file to a remote database: 1 SQL command to drop and create table and then a second one with mysqlimport to populate table with CSV.

I was wondering if a better way would not be to send a mysqlimport command from my batch file and have a trigger on database upon insert on this specific DB to drop it and recreate it before inserting new records, in order to avoid a gap (no records) currently happening between the moment when table is dropped, recreated and then populated with the CSV, which happens a lot.

Any idea how to implement such a trigger?

Structure of the table online_players table:

+-------------------+--------------+------+-----+---------+-------+
| Agent             | varchar(255) | YES  |     | NULL    |       |
| Name              | varchar(255) | YES  |     | NULL    |       |
| Alias             | varchar(255) | YES  |     | NULL    |       |
| Begin_Date        | varchar(100) | YES  |     | NULL    |       |
| LastBalanceUpdate | varchar(100) | YES  |     | NULL    |       |
| Session_minutes   | varchar(100) | YES  |     | NULL    |       |
| Balance           | varchar(100) | YES  |     | NULL    |       |
| Product           | varchar(100) | YES  |     | NULL    |       |
+-------------------+--------------+------+-----+---------+-------+

Solution

  • I solved the issue of droping / creating table before each insert by using the -d parameter in mysqlimport which deletes all rows before importing csv. Thanks vmachan for your help!