从 SQLite 表中删除列

2021-12-08 00:00:00 sql sqlite ddl

我有一个问题:我需要从我的 SQLite 数据库中删除一列.我写了这个查询

alter table table_name drop column column_name

但它不起作用.请帮帮我.

解决方案

更新:SQLite 2021-03-12 (3.35.0) 现在支持 DROP COLUMN.网站上的常见问题解答仍然过时.


来自:http://www.sqlite.org/faq.html:><块引用>

(11) 如何在 SQLite 中的现有表中添加或删除列.

SQLite 具有有限的 ALTER TABLE 支持,您可以使用它来添加列到表的末尾或更改表的名称.如果你想要对表的结构进行更复杂的更改,您将不得不重新创建表.您可以将现有数据保存到临时表,删除旧表,创建新表,然后复制数据从临时表返回.

例如,假设您有一个名为t1"的表.带列名a"、b"和c"并且您要删除列c";由此桌子.以下步骤说明了如何做到这一点:

开始交易;创建临时表 t1_backup(a,b);INSERT INTO t1_backup SELECT a,b FROM t1;删除表 t1;创建表 t1(a,b);INSERT INTO t1 SELECT a,b FROM t1_backup;删除表 t1_backup;犯罪;

I have a problem: I need to delete a column from my SQLite database. I wrote this query

alter table table_name drop column column_name 

but it does not work. Please help me.

解决方案

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 a column to the end of a table or to change the name of a table. If you want to make more complex changes in the structure of a table, you will have to recreate the table. You can save existing data to a temporary table, drop the old table, create the new table, then copy the 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 this table. 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;

相关文章