Copy column data from one table to another in 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 th开发者_如何学JAVAe table.
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 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 has a primary key consisting of three columns.
- One of the columns is named "_id".
- The column "_id" is an integer, but not an automatically incremented integer.
- You want to insert values 1 to 148 into 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;
This will insert new rows into table1 with null values for all columns except _id.
精彩评论