Modify SQLite table: delete column
Question:
Try to delete a column from a SQLite database table using the following query:
ALTER TABLE table_name DROP COLUMN column_name;
However, there was no success. What's the solution?
Answer:
In versions prior to SQLite 3.35.0 (2021-03-12), directly deleting columns is not supported. To make such changes, a more sophisticated approach is required:
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;
Update:
SQLite 3.35.0 and later now directly supports the DROP COLUMN clause, making it easier to drop columns from tables.
The above is the detailed content of How Can I Drop a Column from a SQLite Table?. For more information, please follow other related articles on the PHP Chinese website!