The MySQL Column-Existence Check That Sent Me Through Thirty Answers

Editorial illustration.

Originally published 16 June 2023; rewritten and reviewed on 15 October 2026.

Back when I first wrote this note in 2023, I had clicked through more than thirty StackOverflow answers trying to solve something that sounded trivial: check whether a column exists before running ALTER TABLE ... ADD COLUMN in MySQL. Half the answers wrapped everything in a verbose stored procedure, and the other half failed with syntax errors.

Part of the confusion came from a classic dialect trap. In MariaDB (10.0+), ALTER TABLE orders ADD COLUMN IF NOT EXISTS external_reference VARCHAR(64) works cleanly out of the box. In MySQL 5.7 (and even MySQL 8.0), ADD COLUMN IF NOT EXISTS is not valid syntax—it throws ERROR 1064 (42000) immediately. If you copy a MariaDB snippet into a MySQL 5.7 migration script, the deployment breaks.

Querying information_schema Directly

When you need a clean, read-only check for whether a column already exists in MySQL 5.7, information_schema.columns is the reliable source of truth:

SELECT COUNT(*) AS matching_columns
FROM information_schema.columns
WHERE table_schema = DATABASE()
  AND table_name = 'orders'
  AND column_name = 'external_reference';

If your connection does not have a default database selected, replace DATABASE() with the explicit schema name (for example, 'production_shop'). If you are running this query from application code—say, a PHP or C# setup script—bind table_name and column_name as query parameters rather than concatenating strings.

Handling Conditional DDL in Pure SQL vs. Migration Tools

If your project uses a proper migration tool like Flyway, Liquibase, Laravel Migrations, or Entity Framework Core, you generally should not need IF NOT EXISTS checks at all. Each migration runs once and is recorded in a schema history table.

However, when maintaining legacy PHP apps or shared-hosting deployments where SQL scripts are sometimes re-run manually, you may still need an idempotent SQL script. In MySQL 5.7, you cannot use an IF block at the top level outside a stored routine, so the standard workaround is a short stored procedure with PREPARE and EXECUTE:

DROP PROCEDURE IF EXISTS add_external_reference_column;
DELIMITER $$
CREATE PROCEDURE add_external_reference_column()
BEGIN
    IF NOT EXISTS (
        SELECT 1
        FROM information_schema.columns
        WHERE table_schema = DATABASE()
          AND table_name = 'orders'
          AND column_name = 'external_reference'
    ) THEN
        ALTER TABLE orders
            ADD COLUMN external_reference VARCHAR(64) NULL;
    END IF;
END$$
DELIMITER ;

CALL add_external_reference_column();
DROP PROCEDURE IF EXISTS add_external_reference_column;

Keep two caveats in mind before using this pattern. First, a metadata check followed by ALTER TABLE is not atomic across concurrent deployment workers—always serialize migrations so two processes do not race on the same table. Second, avoid dynamically interpolating user-supplied table or column names into ALTER TABLE strings; keep identifiers fixed in reviewed migration files. For exact clause rules on that version, check the MySQL 5.7 ALTER TABLE reference.

Checking Existence Is Only Half the Check

A column name existing does not guarantee the schema matches what your code expects. I have seen deployments fail because a column already existed on staging with INT instead of VARCHAR(64), NOT NULL without a default, or utf8 instead of utf8mb4.

When inspecting information_schema.columns, it is worth selecting DATA_TYPE, COLUMN_TYPE, IS_NULLABLE, and COLLATION_NAME too. Turning a quick one-off check into a repeatable, verified migration saves far more time than hunting through another thirty forum answers later.