简体   繁体   中英

MySQL SHOW TEMPORARY TABLES;

I'm learning SQL with MySQL 5.7. And I wanna enumerate all tables include temporary tables!

But when I query SHOW TABLES; It shows only non-temporary tables.

How can I list all tables?

I've not found a direct answer to this - as far as I can see there is no way (currently MySQL 8.0.31) of listing all temporary tables on your connection.

You can test for a table's existence using:

SELECT 1 FROM my_table WHERE 0;

If you don't get an error, the table exists in some form (TEMPORARY, BASE TABLE or VIEW). To check if it's temporary you can use:

SHOW TABLES IN my_db
WHERE Tables_in_my_db = "my_table"

(returns the table name if it exists and not temporary or use 'FULL TABLES' to return the table type as well) or

SELECT table_type
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = "my_db" AND TABLE_NAME = "my_table";

(returns the table type if it exists and not temporary)

So a table is temporary if the first test doesn't return an error but it is NOT listed using one of the other tests.

This doesn't help to enumerate the temporary tables (I believe this can be done - see other answers - if the table is INNODB but not generally for MyISAM for example), but it does enable you to identify a table as temporary or otherwise if you know the name.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM