How to get the last modified date of an mysql table - mysql

I would ask how to get the last modified date of an mysql table? For the last modified date of an mysql table, I mean the last time that the table is changed,for example,due to table insert, delete,update,truncate, etc.

As you can read in mysql documentation for The INFORMATION_SCHEMA TABLES Table,
you can get the update_time (and other information) with a simple query:
SELECT update_time
FROM information_schema.tables
WHERE table_name = 'table_name';

It gives you the last modified one.
SELECT UPDATE_TIME
FROM information_schema.tables WHERE
TABLE_SCHEMA = 'your_dbname' AND TABLE_NAME = 'your_tablename'

Related

MySQL get list of tables ending with specific name and it's (table's) comment

I have multiple tables in my multiple databases.
On different servers, i use MySQL / PostgreSQL / MS SQL.
I keep short table namesbut the comments given to the tables are with full explanation.
I want query that will show me tables ending with "com" and also the comment given to each table (table's comment).
In MySQL, I know:
SELECT table_name FROM information_schema.tables where table_name like "%com"
But this shows all tables from all databases.
For MySQL, check out following:
SELECT table_name FROM information_schema.tables;
will show all table names in all databases;
SELECT table_name,table_comment FROM information_schema.tables
will show all table names + comment in all databases;
interesting thing, you can fire
SELECT * FROM information_schema.tables;
to know what all info you can get of a table.
SELECT table_name,table_comment FROM information_schema.tables
where
table_schema = 'sifr_b';
will show all table names + comment in "sifr_b" database;
SELECT table_name,table_comment FROM information_schema.tables
where
table_schema = 'sifr_b' and
table_name like "%com";
will show those table names + comment in "sifr_b" database, that have table name ending with "com";

MySql - How to get datetime of last ALTER TABLE?

I'm looking for a way to get datetime of last ALTER TABLE (structure edit) of a table. I'm currently using MySql 5.6. Checking information_schema db I can get only the last generic edit of the table (INSERT, UPDATE, etc.). Thanks in advance.
Try this
SELECT
create_time
FROM
INFORMATION_SCHEMA.TABLES
WHERE
table_schema = 'databasename'
AND table_name = 'tablename';
Above query gives the last table structure edited datetime.

How to quickly retrieve the table with the most rows in MySQL?

The database is on a remote Linux server, I am using Windows.
I want to retrieve the table with the most rows in MySQL, but I am on a Windows client.
I have about 200 tables. I have to click their table name one by one to figure out the row count.
The databases has many tables, I can get their rows by executing
select count(*) from table
This will retrieve the rowcount one by one.
My Question
Is there a quick method to get the table with the most rows in MySQL workbench on Windows?
how about:
SELECT TABLE_NAME
,TABLE_ROWS
FROM INFORMATION_SCHEMA.TABLES
ORDER BY TABLE_ROWS DESC
SELECT MAX(TABLE_ROWS)
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'db_name'
=> This will return the maximum number of rows a table have.
SELECT table_name, MAX(TABLE_ROWS)
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'db_name'
=> This will return the maximum number of rows a table have with the table name
Quick Links
The INFORMATION_SCHEMA TABLES Table
Get record counts for all tables in MySQL database
SELECT TABLE_ROWS, TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = '{{schema_name}}'
ORDER BY TABLE_ROWS DESC
LIMIT 1;
This will tell you the table name with most number of rows. Replace schema_name with your database before executing query.

Finding column creation time in MySQL

I wonder if is it possible to get creation date of column in a table? I want to see the creation times of columns that added later.
So you can get this information for the table quite easily. You would query the information_schema for the create_time of the table.
For instance:
SELECT create_time FROM INFORMATION_SCHEMA.TABLES
WHERE table_schema = 'your_schema'
AND table_name = 'your_table'
Check here for more information:
https://dev.mysql.com/doc/refman/5.7/en/tables-table.html
For a specific column this is not as simple because none of the system tables (that is, nothing in the INFORMATION_SCHEMA database) exist that has that kind of information recorded anywhere. In other words, there is no native mechanism to put any timestamps on column changes.
Any time that:
one or more columns change in any row
a new row is added
an old is deleted
an ALTER TABLE of any kind
the UPDATE_TIME column in INFORMATION_SCHEMA.TABLES is updated.
You could find all changes in the last hour (or your selected interval) quite simply though:
SELECT CONCAT(TABLE_SCHEMA, '.', TABLE_NAME) AS Table, UPDATE_TIME AS Updated
FROM INFORMATION_SCHEMA.TABLES
WHERE DATE_SUB(NOW(), INTERVAL 1 HOUR) < UPDATE_TIME
AND TABLE_SCHEMA != 'INFORMATION_SCHEMA'
AND TABLE_TYPE = 'BASE TABLE';
I hope this helps.

How to get mysql last update/delete/insert datetime for a table?

In mysql, how can you get the datetime for the last time a certain table had a row which got deleted or inserted or updated?
Thanks
For a MyISAM table do this:
SELECT UPDATE_TIME FROM information_schema.tables
WHERE TABLE_SCHEMA = 'databasename' AND TABLE_NAME = 'tablename';
For innodeb, you're apparently out of luck, since there's been a long standing bug about this... see this question: How can I determine when an InnoDB table was last changed?