Here are four ways to check whether or not a table exists in a MariaDB database.
The information_schema.TABLES
Table
The information_schema.TABLES
table contains information about the various non-TEMPORARY
tables (except tables from the Information Schema database) and views on the server.
Here’s an example of querying this table:
SELECT
TABLE_SCHEMA,
TABLE_NAME,
TABLE_TYPE
FROM
information_schema.TABLES
WHERE
TABLE_SCHEMA LIKE 'PetHouse' AND
TABLE_TYPE LIKE 'BASE TABLE' AND
TABLE_NAME = 'Pets';
Result:
+--------------+------------+------------+ | TABLE_SCHEMA | TABLE_NAME | TABLE_TYPE | +--------------+------------+------------+ | PetHouse | pets | BASE TABLE | +--------------+------------+------------+
Here, I returned the base table called Pets
from the database called PetHouse
. Querying this without filtering the results by TABLE_SCHEMA
returns base tables from all databases. Querying it without filtering by TABLE_TYPE
returns all table types.
If we don’t need all that info, we can do this:
SELECT EXISTS (
SELECT
TABLE_NAME
FROM
information_schema.TABLES
WHERE
TABLE_SCHEMA LIKE 'PetHouse' AND
TABLE_TYPE LIKE 'BASE TABLE' AND
TABLE_NAME = 'Pets'
);
Result:
1
Or we could get the count:
SELECT COUNT(TABLE_NAME)
FROM
information_schema.TABLES
WHERE
TABLE_SCHEMA LIKE 'PetHouse' AND
TABLE_TYPE LIKE 'BASE TABLE' AND
TABLE_NAME = 'Pets';
Result:
+-------------------+ | COUNT(TABLE_NAME) | +-------------------+ | 1 | +-------------------+
The SHOW TABLES
Command
The SHOW TABLES
command lists the non-TEMPORARY
tables, sequences and views in a given database. We can use the WHERE
clause to narrow it to a given type.
We can also use the FULL
modifier to return a second column that displays the type:
SHOW FULL TABLES
WHERE Table_Type LIKE 'BASE TABLE'
AND Tables_in_pethouse LIKE 'Pets';
Result:
+--------------------+------------+ | Tables_in_pethouse | Table_type | +--------------------+------------+ | Pets | BASE TABLE | +--------------------+------------+
In this case the database name is pethouse
, and so the first column is Tables_in_pethouse
.
The SHOW TABLE STATUS
Command
The SHOW TABLE STATUS
command is similar to the SHOW TABLES
command but provides more extensive information about each (non-TEMPORARY
) table.
Example:
SHOW TABLE STATUS
FROM PetHouse
WHERE Name = 'Pets';
Result:
+------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+--------------------+----------+----------------+---------+------------------+-----------+ | Name | Engine | Version | Row_format | Rows | Avg_row_length | Data_length | Max_data_length | Index_length | Data_free | Auto_increment | Create_time | Update_time | Check_time | Collation | Checksum | Create_options | Comment | Max_index_length | Temporary | +------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+--------------------+----------+----------------+---------+------------------+-----------+ | Pets | InnoDB | 10 | Dynamic | 8 | 2048 | 16384 | 0 | 32768 | 0 | NULL | 2021-04-01 15:42:43 | NULL | NULL | utf8mb4_general_ci | NULL | | | 0 | N | +------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+--------------------+----------+----------------+---------+------------------+-----------+
The mariadb-show
Client
Another way to do it is with the mariadb-show
utility.
To use this option, open a command line prompt/terminal window and run the following (replacing pethouse
with the database you’re interested in):
mariadb-show pethouse;
Result:
+---------------+ | Tables | +---------------+ | Owners | | PetTypes | | Pets | | vownercount | | vpetcount | | vpetsowners | | vpetstypes | | vpettypecount | +---------------+
This returns views and tables.
The output displays only the names of those databases, tables, or columns for which you have some privileges.
If no database is given then all matching databases are shown. If no table is given, then all matching tables in database are shown. If no column is given, then all matching columns and column types in table are shown.
The client can also be run as mysqlshow
:
mysqlshow pethouse;
This utility accepts quite a few options, such as --user
(so that you can pass the username), --password
(so that you can pass the password), etc.
See MariaDB’s documentation for a full list of options.
Check if a Table Already Exists Before Creating It
If you need to create the table if it doesn’t exist, you can use the IF NOT EXISTS
clause of the CREATE TABLE
statement. If the table doesn’t exist, it will be created. If it already exists, it won’t be created.
See How to Create a Table Only if it Doesn’t Exist in MariaDB for an example.