Thursday, August 26, 2021

MySQL: How to create Column if not exists?

https://dba.stackexchange.com/questions/169458/mysql-how-to-create-column-if-not-exists SELECT count(*) INTO @exist FROM information_schema.columns WHERE table_schema = 'mydatabase' and COLUMN_NAME = 'mycolumn' AND table_name = 'mytable' LIMIT 1; set @query = IF(@exist <= 0, 'ALTER TABLE mydatabase.`mytable` ADD COLUMN `mycolumn` MEDIUMTEXT NULL', 'select \'Column Exists\' status'); prepare stmt from @query; EXECUTE stmt;