Mysql Alter table if column not Exists

Mysql Alter table if column not Exists

Example:
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` Varchar(100) NOT NULL default '';

END IF;

Post a Comment

0 Comments