Home >Database >Mysql Tutorial >How Can I Retrieve MySQL Table Names Using a SELECT Statement?

How Can I Retrieve MySQL Table Names Using a SELECT Statement?

Barbara Streisand
Barbara StreisandOriginal
2024-12-11 06:52:10191browse

How Can I Retrieve MySQL Table Names Using a SELECT Statement?

Obtain Table Names with the SELECT Statement in MySQL

MySQL's SHOW TABLES command provides a convenient method to list all tables within a database. However, to insert these table names into another table directly, an alternative approach is required.

The question arises: is there a standard SELECT statement that can retrieve table names? The answer lies in the information_schema.tables system table.

To retrieve the table names of all databases, execute the following query:

SELECT table_name FROM information_schema.tables;

To narrow down the search to a specific database named 'my_database', use:

SELECT table_name FROM information_schema.tables
WHERE table_schema = 'my_database';

To insert the retrieved table names into another table, execute:

INSERT INTO table_name
    SELECT table_name FROM information_schema.tables
        WHERE table_schema = 'my_database';

For comprehensive documentation, refer to: http://dev.mysql.com/doc/refman/5.0/en/information-schema.html

The above is the detailed content of How Can I Retrieve MySQL Table Names Using a SELECT Statement?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn