Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DROP COLUMN IF EXISTS in MariaDB - syntax error

Tags:

sql

mariadb

Can someone explain me, what's wrong with my SQL query?

ALTER TABLE tableName DROP COLUMN IF EXISTS columnName;

MariaDB gives me this error:

#1064 - You have an error in your SQL syntax; check the manual that
corresponds to your MariaDB server version for the right syntax 
to use near 'IF EXISTS columnName' at line 1 

A tried a lot of syntax modification, but without positive result.

like image 863
janfitz Avatar asked Oct 21 '25 14:10

janfitz


2 Answers

I tested it on dbfiddle.uk (MariaDB 10.2 RC) and it works. Are you sure about your MariaDB version (SELECT VERSION();) and the presence of the command in that version? (I suppose it doesn't support until MARIADB 10)

CREATE TABLE T1 (ID INT, DESCR VARCHAR(20));
INSERT INTO T1 VALUES (1, 'Test');
SELECT * FROM T1;
ALTER TABLE T1 DROP COLUMN IF EXISTS XXX; /* no error */
SELECT * FROM T1;
ALTER TABLE T1 DROP COLUMN IF EXISTS DESCR; /* column dropped*/
SELECT * FROM T1;
like image 141
etsa Avatar answered Oct 23 '25 03:10

etsa


I did today ALTER TABLE table_name DROP IF EXISTS column_name; and it works :P

MariaDB 10.0

like image 25
ShinJii Avatar answered Oct 23 '25 03:10

ShinJii



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!