HiveBrain v1.2.0
Get Started
← Back to all entries
snippetsqlMajor

MySQL: How to create Column if not exists?

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
createcolumnexistsmysqlhownot

Problem

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 Column ?

Solution

Actually exists now for Maria DB 10.219

ALTER TABLE test ADD COLUMN IF NOT EXISTS column_a VARCHAR(255);


Bonus, it works for MODIFY as well

ALTER TABLE test MODIFY IF EXISTS column_a VARCHAR(255);

Code Snippets

ALTER TABLE test ADD COLUMN IF NOT EXISTS column_a VARCHAR(255);
ALTER TABLE test MODIFY IF EXISTS column_a VARCHAR(255);

Context

StackExchange Database Administrators Q#169458, answer score: 49

Revisions (0)

No revisions yet.