有没有办法在单个命令中列出所有带有表的数据库?
期待:
mysql> SHOW DATABASES, TABLES;
DB1 table1
table2
DB2 table1
table2
table3
table4
table5
DB3 table1
table2
Run Code Online (Sandbox Code Playgroud)
使用存储在 中的信息INFORMATION_SCHEMA:
SELECT table_schema, table_name
FROM INFORMATION_SCHEMA.tables
ORDER BY table_schema, table_name;
Run Code Online (Sandbox Code Playgroud)
table_schema 是数据库名称。
还显示没有任何表的数据库:
SELECT s.schema_name, t.table_name
FROM INFORMATION_SCHEMA.schemata AS s
LEFT JOIN INFORMATION_SCHEMA.tables AS t
ON t.table_schema = s.schema_name
-- optional, to hide system databases and tables
-- WHERE s.schema_name NOT IN ('information_schema', 'mysql', 'performance_schema')
ORDER BY schema_name, table_name ;
Run Code Online (Sandbox Code Playgroud)
文档链接:INFORMATION_SCHEMA.tables, INFORMATION_SCHEMA.schemata