Is there a way to get a list of MySQL databases ordered by creation date or last update date using a command?

Try this , if you want by update date you can order it by update_time
SELECT
table_schema,
MAX(create_time) create_time,
MAX(update_time) update_time
FROM information_schema.tables
Group by TABLE_SCHEMA
Order by create_time desc

Information_schema database contains meta data about databases:
SELECT distinct table_schema
FROM information_schema.tables
Order by update_time

If you want to get list of database name from information_schema
order by latest created date then check below code:
SELECT table_schema, MAX(create_time) create_time, MAX(update_time) update_time
FROM information_schema.tables WHERE create_time IS NOT NULL
GROUP BY TABLE_SCHEMA
ORDER BY create_time DESC;
Result from above query, I see that there is some result with null value for create_time column. I assume that these records are may be created by default at mysql setup so make some pretty change.
Add WHERE create_time IS NOT NULL
for filter above null create_time records:
SELECT table_schema, MAX(create_time) create_time, MAX(update_time) update_time
FROM information_schema.tables WHERE create_time IS NOT NULL
GROUP BY TABLE_SCHEMA
ORDER BY create_time DESC;
NOTE: You can use DISTINCT
, if you want only result with table_schema and don’t want to use MAX()
and GROUP BY
.
For this you need to use below code:
SELECT DISTINCT table_schema
FROM information_schema.tables
ORDER BY update_time
Let me know if there is any query from this.
I hope this help.

considering the following schema and query
Schema
create table xInnodb
( i int
); -- ie: engine=INNODB
create table xMyISAM
( i int
)engine=MyISAM;
insert xInnodb (i) values (1);
insert xMyISAM (i) values (1);
-- or even update statements after the fact
Query
SELECT table_schema,
table_name,
engine,
create_time,
update_time
FROM information_schema.tables
where TABLE_SCHEMA ='so_gibberish' -- my database name
and table_type='BASE TABLE' -- don't show views
Order by create_time desc;
+--------------+------------+--------+---------------------+---------------------+
| table_schema | table_name | engine | create_time | update_time |
+--------------+------------+--------+---------------------+---------------------+
| so_gibberish | xmyisam | MyISAM | 2015-12-12 00:03:18 | 2015-12-12 00:10:54 |
| so_gibberish | xinnodb | InnoDB | 2015-12-12 00:03:17 | NULL |
+--------------+------------+--------+---------------------+---------------------+
You will find that innodb tables will have update_time
as NULL
This can be read about in the Manual page entitled The INFORMATION_SCHEMA TABLES Table:
Beginning with MySQL 5.7.2, UPDATE_TIME displays a timestamp value for the last UPDATE, INSERT, or DELETE performed on InnoDB tables that are not partitioned. Previously, UPDATE_TIME displayed a NULL value for InnoDB tables.

Try this to show all databases and create times...
SELECT table_schema, create_time FROM INFORMATION_SCHEMA.TABLES;
Note: I haven't tested this

Có thể bạn quan tâm
