How to create a mysql add column if it does not exist?
I am trying to create a Column for my table only if it does not exist. I have researched a lot but I could not find any solution yet. Is this really possible to conditionally create a Column ?
To create a mysql add column if not exists
MySQL ALTER TABLE does not have IF EXISTS specification.
You can do the following through using a stored proc or a programming language if this is something that you'll need to do on a regular basis:
Pseudocode:
Find if the column exists using the SQL below:
SELECT column_name FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA=[Database Name] AND TABLE_NAME=[Table Name];
If the above query returns a result then it means the column exists, otherwise you can go ahead and create the column.