MySQL: ALTER TABLE if column not exists

The Solution to MySQL: ALTER TABLE if column not exists is


Use the following in a stored procedure:

IF NOT EXISTS( SELECT NULL
            FROM INFORMATION_SCHEMA.COLUMNS
           WHERE table_name = 'tablename'
             AND table_schema = 'db_name'
             AND column_name = 'columnname')  THEN

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

END IF;

~ Answered on 2015-04-03 08:33:49


Most Viewed Questions: