Menu

How to Check Whether a Table Exists in MySQL

Check for a MySQL table with SHOW TABLES or INFORMATION_SCHEMA, or create it safely with IF NOT EXISTS—without assuming the existing schema matches.

Posted on By
On this page

You can check whether a MySQL table exists with SHOW TABLES or INFORMATION_SCHEMA.TABLES. If the goal is to run a creation script more than once, CREATE TABLE IF NOT EXISTS is usually simpler. It suppresses the “table exists” error, but it does not verify that an existing table has the expected columns or indexes. See the MySQL CREATE TABLE, SHOW TABLES, and INFORMATION_SCHEMA.TABLES references.

Use CREATE TABLE IF NOT EXISTS when creating a table

Add IF NOT EXISTS to a creation statement when the table may already exist:

CREATE TABLE IF NOT EXISTS appdb.orders (
    order_id BIGINT PRIMARY KEY,
    customer_id BIGINT NOT NULL,
    created_at DATETIME NOT NULL
);

If appdb.orders already exists, MySQL leaves it in place. MySQL does not compare the existing table definition with the columns and indexes in this statement. To inspect the actual definition, use SHOW CREATE TABLE.

Check interactively with SHOW TABLES

To see whether a specific table name appears in a database, use SHOW TABLES with LIKE:

SHOW TABLES FROM appdb LIKE 'orders';

If a row is returned, a table with that name is visible in the selected schema. Use this method when exploring a database from the MySQL client or an interactive SQL tool.

Check programmatically with INFORMATION_SCHEMA

Query INFORMATION_SCHEMA.TABLES when you need a 1 or 0 result or want to combine the check with other SQL:

SELECT EXISTS (
    SELECT 1
    FROM INFORMATION_SCHEMA.TABLES
    WHERE TABLE_SCHEMA = 'appdb'
      AND TABLE_NAME = 'orders'
      AND TABLE_TYPE = 'BASE TABLE'
) AS table_exists;

The schema and table names are compared separately. Keep the TABLE_TYPE condition if you mean a base table rather than a view. To check the currently selected database, use TABLE_SCHEMA = DATABASE(); this returns no match if the connection has not selected a database.

The results visible through INFORMATION_SCHEMA depend on the account’s privileges. If the query returns 0, verify the schema name and the account’s access before assuming the table is absent.

Choose the method for the job

  • Use CREATE TABLE IF NOT EXISTS for repeatable setup scripts when keeping an existing definition is acceptable.
  • Use SHOW TABLES for a quick manual check.
  • Use INFORMATION_SCHEMA.TABLES for conditional logic or a queryable result.

A separate “check, then create” sequence can race with another connection that creates the same table between those steps. IF NOT EXISTS handles the duplicate-name condition, but schema migrations should still compare and update table definitions explicitly. For the resulting error when a plain CREATE TABLE finds an existing table, see MySQL Error 1050: Table Already Exists.