MySQLまたはMariaDBのデータベース内のテーブル数を確認する方法をお伝えします。

サンプルデータベースを次のように準備します。


-- データベース作成
CREATE DATABASE sample_database;
(out)
-- データベース指定
use sample_database;
(out)
-- テーブル作成
CREATE TABLE sample01 (col11 INT, col12 CHAR(10));
CREATE TABLE sample02 (col21 INT, col22 CHAR(10), col23 DOUBLE);

データベース内のテーブルに関する情報はinformation_schema.tablesにあります。


SHOW FULL COLUMNS FROM information_schema.tables;
(out)
(out)+-----------------+---------------------+-----------------+------+-----+---------+-------+------------+---------+
(out)| Field           | Type                | Collation       | Null | Key | Default | Extra | Privileges | Comment |
(out)+-----------------+---------------------+-----------------+------+-----+---------+-------+------------+---------+
(out)| TABLE_CATALOG   | varchar(512)        | utf8_general_ci | NO   |     |         |       | select     |         |
(out)| TABLE_SCHEMA    | varchar(64)         | utf8_general_ci | NO   |     |         |       | select     |         |
(out)| TABLE_NAME      | varchar(64)         | utf8_general_ci | NO   |     |         |       | select     |         |
(out)| TABLE_TYPE      | varchar(64)         | utf8_general_ci | NO   |     |         |       | select     |         |
(out)| ENGINE          | varchar(64)         | utf8_general_ci | YES  |     | NULL    |       | select     |         |
(out)| VERSION         | bigint(21) unsigned | NULL            | YES  |     | NULL    |       | select     |         |
(out)| ROW_FORMAT      | varchar(10)         | utf8_general_ci | YES  |     | NULL    |       | select     |         |
(out)| TABLE_ROWS      | bigint(21) unsigned | NULL            | YES  |     | NULL    |       | select     |         |
(out)| AVG_ROW_LENGTH  | bigint(21) unsigned | NULL            | YES  |     | NULL    |       | select     |         |
(out)| DATA_LENGTH     | bigint(21) unsigned | NULL            | YES  |     | NULL    |       | select     |         |
(out)| MAX_DATA_LENGTH | bigint(21) unsigned | NULL            | YES  |     | NULL    |       | select     |         |
(out)| INDEX_LENGTH    | bigint(21) unsigned | NULL            | YES  |     | NULL    |       | select     |         |
(out)| DATA_FREE       | bigint(21) unsigned | NULL            | YES  |     | NULL    |       | select     |         |
(out)| AUTO_INCREMENT  | bigint(21) unsigned | NULL            | YES  |     | NULL    |       | select     |         |
(out)| CREATE_TIME     | datetime            | NULL            | YES  |     | NULL    |       | select     |         |
(out)| UPDATE_TIME     | datetime            | NULL            | YES  |     | NULL    |       | select     |         |
(out)| CHECK_TIME      | datetime            | NULL            | YES  |     | NULL    |       | select     |         |
(out)| TABLE_COLLATION | varchar(32)         | utf8_general_ci | YES  |     | NULL    |       | select     |         |
(out)| CHECKSUM        | bigint(21) unsigned | NULL            | YES  |     | NULL    |       | select     |         |
(out)| CREATE_OPTIONS  | varchar(2048)       | utf8_general_ci | YES  |     | NULL    |       | select     |         |
(out)| TABLE_COMMENT   | varchar(2048)       | utf8_general_ci | NO   |     |         |       | select     |         |
(out)+-----------------+---------------------+-----------------+------+-----+---------+-------+------------+---------+

そのinformation_schema.tablesを用いて、sample_databaseデータベース内のテーブル数を確認するSQLコードは次になります。


SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = 'sample_database';
(out)
(out)+----------+
(out)| COUNT(*) |
(out)+----------+
(out)|        2 |
(out)+----------+
(out)1 row in set (0.00 sec)
MySQL データベース内のテーブル数を確認する方法