Alter Table if Column Not Exist in MySQL

Alter Table if Column Not Exist in MySQL

To alter the table if the column does not exist:

IF NOT EXISTS( SELECT NULL
            FROM INFORMATION_SCHEMA.COLUMNS
           WHERE table_name = 'tableName'
             AND table_schema = 'database_name'
             AND column_name = 'columnName')  THEN

  ALTER TABLE `TableName` ADD `ColumnName` int(1) NOT NULL default '0';

END IF;
Please follow and like us:
Content Protection by DMCA.com