In the footer of my page, I would like to add something like "last updated the xx/xx/200x" with this date being the last time a certain mySQL table has been updated.
What is the best way to do that? Is there a function to retrieve the last updated date? Should I access to the database every time I need this value?
Although there is an accepted answer I don't feel that it is the right one. It is the simplest way to achieve what is needed, but even if already enabled in InnoDB (actually docs tell you that you still should get NULL ...), if you read MySQL docs, even in current version (8.0) using UPDATE_TIME is not the right option, because:
If I understand correctly (can't verify it on a server right now), timestamp gets reset after server restart.
As for real (and, well, costly) solutions, you have Bill Karwin's solution with CURRENT_TIMESTAMP and I'd like to propose a different one, that is based on triggers (I'm using that one).
You start by creating a separate table (or maybe you have some other table that can be used for this purpose) which will work like a storage for global variables (here timestamps). You need to store two fields - table name (or whatever value you'd like to keep here as table id) and timestamp. After you have it, you should initialize it with this table id + starting date (NOW() is a good choice :) ).
Now, you move to tables you want to observe and add triggers AFTER INSERT/UPDATE/DELETE with this or similar procedure:
OS level analysis:
Find where the DB is stored on disk:
Check for most recent modifications
Should work on all database types.
I don't have information_schema database, using mysql version 4.1.16, so in this case you can query this:
It will return these columns:
As you can see there is a column called: "Update_time" that shows you the last update time for your_table.
I would create a trigger that catches all updates/inserts/deletes and write timestamp in custom table, something like tablename | timestamp
Just because I don't like the idea to read internal system tables of db server directly
The simplest thing would be to check the timestamp of the table files on the disk. For example, You can check under your data directory
This should give you the list of all tables with the table when it was last modified the oldest time, first.
For a list of recent table changes use this: