How do I add a column if not exist in MySQL?
I have tried this, but it doesn't work anymore in the latest MySQL:
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;
The error:
[Err] 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'IF NOT EXISTS (
SELECT
NULL
FROM
INFORMATION_SCHEMA.COLUMNS
WHERE
' at line 1