如何在 sqlite 2 数据库表中添加列

发布于 2024-07-26 15:50:48 字数 86 浏览 6 评论 0原文

如何向具有与其关联的数据和索引的现有 sqlite 2 数据库表添加附加列。

似乎sqlite2 中不提供alter table SQL?

How do I add an additional column to an existing sqlite 2 database table that have data and indexes associated with it.

It seems like the alter table SQL for that is not available in sqlite2?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

つ可否回来 2024-08-02 15:50:48

来自SqLite FAQ

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

例如,假设您有一个名为“t1”的表,其中包含列名称“a”、“b”和“c”,并且您想要从此表中删除列“c”。 以下步骤说明了如何完成此操作:

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;

From the SqLite FAQ:

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;
冷情妓 2024-08-02 15:50:48

可能不是——sqlite3 已经发布有一段时间了。 您必须按照您想要的方式使用该表创建另一个数据库,并将数据复制到其中。 或者,创建一个新表,复制数据,删除原始表并替换为新表。

It might not be -- sqlite3 has been out for a while. You'll have to create another DB with the table the way you want it and copy the data to it. Or, create a new table, copy the data, drop the original and replace with the new one.

花落人断肠 2024-08-02 15:50:48

不要忘记 DROP TABLE 也会删除关联的触发器。

在修改表之前,使用命令

.scheme TABLE_TO_MODIFY

转储所有将被删除的当前语句,包括需要重新创建的触发器。

干杯,

埃利克森

Don't forget that DROP TABLE drops also associated TRIGGERs.

Before you are going to modify the table use the command

.scheme TABLE_TO_MODIFY

that will dump all the current statements that will be dropped including the TRIGGERs that you need to re-create.

Cheers,

Elixon

岁月如刀 2024-08-02 15:50:48

你不知道。 不支持对表进行重大修改。
您需要做的是:

  1. 将数据复制到临时表 删除旧表
  2. 创建新表
  3. 将原始表中的数据复制到它
  4. 创建新表
  5. 将数据从临时表复制到新表

You don't. Non trivial modification of tables is not supported.
What you would need to do is:

  1. Copy the data to a temp table drop the old
  2. create the new table
  3. Copy data from your original table to it
  4. Create the new table
  5. Copy the data from temp into the new table
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文