Delete column from SQLite table Delete column from SQLite table sqlite sqlite

Delete column from SQLite table


Update: SQLite 2021-03-12 (3.35.0) now supports DROP COLUMN. The FAQ on the website is still outdated.


From: http://www.sqlite.org/faq.html:

(11) How do I add or delete columns from an existing table in SQLite.

SQLite has limited ALTER TABLE support that you can use to add acolumn to the end of a table or to change the name of a table. If youwant to make more complex changes in the structure of a table, youwill have to recreate the table. You can save existing data to atemporary table, drop the old table, create the new table, then copythe data back in from the temporary table.

For example, suppose you have a table named "t1" with columns names"a", "b", and "c" and that you want to delete column "c" from thistable. The following steps illustrate how this could be done:

BEGIN TRANSACTION;CREATE TEMPORARY TABLE t1_backup(a,b);INSERT INTO t1_backup SELECT a,b FROM t1;DROP TABLE t1;CREATE TABLE t1(a,b);INSERT INTO t1 SELECT a,b FROM t1_backup;DROP TABLE t1_backup;COMMIT;


Instead of dropping the backup table, just rename it...

BEGIN TRANSACTION;CREATE TABLE t1_backup(a,b);INSERT INTO t1_backup SELECT a,b FROM t1;DROP TABLE t1;ALTER TABLE t1_backup RENAME TO t1;COMMIT;


For simplicity, why not create the backup table from the select statement?

CREATE TABLE t1_backup AS SELECT a, b FROM t1;DROP TABLE t1;ALTER TABLE t1_backup RENAME TO t1;