add column to mysql table if it does not exist add column to mysql table if it does not exist mysql mysql

add column to mysql table if it does not exist


Here is a working solution (just tried out with MySQL 5.0 on Solaris):

DELIMITER $$DROP PROCEDURE IF EXISTS upgrade_database_1_0_to_2_0 $$CREATE PROCEDURE upgrade_database_1_0_to_2_0()BEGIN-- rename a table safelyIF NOT EXISTS( (SELECT * FROM information_schema.COLUMNS WHERE TABLE_SCHEMA=DATABASE()        AND TABLE_NAME='my_old_table_name') ) THEN    RENAME TABLE         my_old_table_name TO my_new_table_name,END IF;-- add a column safelyIF NOT EXISTS( (SELECT * FROM information_schema.COLUMNS WHERE TABLE_SCHEMA=DATABASE()        AND COLUMN_NAME='my_additional_column' AND TABLE_NAME='my_table_name') ) THEN    ALTER TABLE my_table_name ADD my_additional_column varchar(2048) NOT NULL DEFAULT '';END IF;END $$CALL upgrade_database_1_0_to_2_0() $$DELIMITER ;

On a first glance it probably looks more complicated than it should, but we have to deal with following problems here:

  • IF statements only work in stored procedures, not when run directly,e.g. in mysql client
  • more elegant and concise SHOW COLUMNS does not work in stored procedure so have to use INFORMATION_SCHEMA
  • the syntax for delimiting statements is strange in MySQL, so you have toredefine the delimiter to be able to create stored procedures. Do notforget to switch the delimiter back!
  • INFORMATION_SCHEMA is global for all databases, do not forget tofilter on TABLE_SCHEMA=DATABASE(). DATABASE() returns the name ofthe currently selected database.


If you are on MariaDB, no need to use stored procedures. Just use, for example:

ALTER TABLE table_name ADD COLUMN IF NOT EXISTS column_name tinyint(1) DEFAULT 0;

See here


Note that INFORMATION_SCHEMA isn't supported in MySQL prior to 5.0. Nor are stored procedures supported prior to 5.0, so if you need to support MySQL 4.1, this solution isn't good.

One solution used by frameworks that use database migrations is to record in your database a revision number for the schema. Just a table with a single column and single row, with an integer indicating which revision is current in effect. When you update the schema, increment the number.

Another solution would be to just try the ALTER TABLE ADD COLUMN command. It should throw an error if the column already exists.

ERROR 1060 (42S21): Duplicate column name 'newcolumnname'

Catch the error and disregard it in your upgrade script.