Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DROP COLUMN IF EXISTS in SQLite?

Tags:

sql

sqlite

Using SQLite 3.37.2. The following queries work:

DROP TABLE IF EXISTS MyTable;
CREATE TABLE IF NOT EXISTS...;

But a similar query on a column generates an error:

ALTER TABLE MyTable DROP COLUMN IF EXISTS MyField;

Remove IF EXISTS and the query works fine.

Does SQLite support IF EXISTS and IF NOT EXISTS for adding/dropping columns? This related question appears to answer "no", though that was back in 2010. Perhaps something has changed in the latest version(s).

like image 262
AlainD Avatar asked Oct 23 '25 15:10

AlainD


1 Answers

This recent SQLite forum question on the same subject is answered by the primary SQLite developer, Richard Hipp. Based on the Richard's answer, and the observation from @AnonCoward, the following is true:

As of v3.37.2 (January 2022), SQLite does NOT support either of the following:

ALTER TABLE ... ADD COLUMN IF NOT EXISTS ...;
ALTER TABLE ... DROP COLUMN IF EXISTS ...;

UPDATE

There is currently a prototype implementation of this functionality in SQLite. The git check-in can be viewed here. Not slated for release in the upcoming v3.38.0 release, but sometime in the future.

like image 193
AlainD Avatar answered Oct 25 '25 07:10

AlainD