March 11, 2012

MySQL: How can I get the timestamp of the last insertion to the database

Question by user690182

How can I check when was the last time (timestamp) that I wrote in the database, irrespectively of the database table that I inserted into?

Answer by Michael Durrant

I wasn’t able to use information_schema.tables update_time as the column wasn’t being updated but for create_time this worked. It would work for update_time (if changed) if update_time gets updated which might be true in your setup.

select table_schema,table_name,max_time from information_schema.tables t1 JOIN 
 (select MAX(t2.create_time) AS max_time FROM 
  information_schema.tables t2 where  
  table_schema ='test') as t3  
on t1.create_time = t3.max_time;

Answer by Starx

This is basically what logging is all about.

On the CLI

  1. execute SET GLOBAL log_output = 'TABLE';
  2. execute SET GLOBAL general_log = 'ON';

Now, a table general_log inside mysql database will log all such actions on database.
Use phpMyadmin or similar to view these. You can query from their results very effectively.

...

Please fill the form - I will response as fast as I can!