2

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).

AlainD
  • 5,413
  • 6
  • 45
  • 99
  • 1
    The [documentation of ALTER TABLE](https://sqlite.org/lang_altertable.html) does not mention `IF EXISTS`. – Anon Coward Feb 18 '22 at 23:23
  • True, which probably means that the answer is still "no". I note that the documentation on drop table (https://sqlite.org/lang_droptable.html) does mention `IF EXISTS`. – AlainD Feb 18 '22 at 23:26

1 Answers1

3

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.

AlainD
  • 5,413
  • 6
  • 45
  • 99