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.

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

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