在 SQLite 中将列数据从一个表复制到另一个表 [英] Copy column data from one table to another in SQLite

查看:76
本文介绍了在 SQLite 中将列数据从一个表复制到另一个表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表,其中需要一个递增的 _id 列.但是由于表有 3 个主键,我无法使用自动增量.这是在 SQLite 中,顺便说一句.该表已经填充了数据,因此我不能只是重新创建该表.

I have a table where I need to have a _id column which is incremented. However I cannot use autoincrement due to the table having 3 primary keys. This is in SQLite, btw. The table is already populated with data and therefore I cannot just recreate the table.

有没有办法在 _id 列中插入 1 到 148 的值?

Is there a way to insert values from 1 to 148 in the _id column?

我们已经尝试过这样的事情:

We've already tried something like this:

UPDATE table1
SET _id = (SELECT _id FROM table2)

table2 是一个临时表,其中包含从 1 到 148 的值.但是这只是将所有 _id 值更新为1"..

table2 is a temp table which contains the values from 1 to 148. However this just updates all _id values to '1'..

有关如何解决此问题的任何建议?

Any suggestions on how to solve this?

推荐答案

不确定我理解.但让我试试.

Not certain I understand. But let me try.

  • table1 有一个主键,包括三列.
  • 其中一列名为_id".
  • 列_id"是一个整数,但是不是自动递增整数.
  • 您想插入 1 到 148 之间的值进入 table1._id.

在 SQLite 中,如果主键中的其他两列中的任何一列被定义为NOT NULL",那么您就不能这样做.您必须同时为主键的所有NOT NULL"组件插入值.但如果它们可以为空,您可以将值插入 table1._id.只是

In SQLite, if either of the other two columns in the primary key are defined as "NOT NULL", then you can't do that. You have to insert values for all the "NOT NULL" components of the primary key at the same time. But if they're nullable, you can insert values into table1._id. Just

insert into table1 (_id)
select _id from table2;

这会将新行插入到 table1 中,除 _id 之外的所有列的值为空.

This will insert new rows into table1 with null values for all columns except _id.

这篇关于在 SQLite 中将列数据从一个表复制到另一个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆