How to create Column in MySQL only if one does not exist?

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
  • If the query below returns a result then it means the column exists, otherwise you can go ahead and create the column

Note: For MariaDB 10.219 onwards IF EXISTS is now available for ALTER TABLE … ADD/MODIFY COLUMN

Menu