sql - 从 SQLite 表中删除列

标签 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) 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;

关于sql - 从 SQLite 表中删除列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5938048/

相关文章:

sql - 是否可以在 SQLite 数据库中一次插入多行?

java - Android - 数据库插入未插入

database - 升级或迁移后是否有验证数据库模式是否正确的好方法?

python - 使用 python 创建 Mysql 数据库 - 不断收到 1064 错误(...在 '%s' 附近使用正确的语法)

mysql - 如何将结果集拆分成更小的子集?

java - 将以下内容从 Java 转换为 Scala 时,为什么会收到 "will always yield true"警告?

sqlite - SQLite中的.journal文件

sql - 有没有办法根据其中一列值添加表约束?

mysql - SQL ORDER BY 在 GROUP BY 之前

sql - 具有日期窗口保存时间的 MAX 解析函数