I basically want to convert a table from mysql to sqlite with the following scheme:
create table items (
id integer auto_increment,
version integer default 0,
primary key (id, version)
);
Essentially, I want ID to auto increment whenever I insert anything into the table, with VERSION starting off at 0, but still allow multiple items with the same ID as long as VERSION is different.
I'm trying to replicate this behavior with Sqlite however, I can't seem to get table creation working. It seems like you are only allowed one column as autoincrement and it has to be the primary key. If I make ID the primary key, then I can't use VERSION as part of the key. However, if I make a multi-column key (ID, VERSION) , then I can't get ID to auto increment.
Is there a workaround or perhaps a better way of designing my table?
I was thinking of the following solution:
table 1:
create table items {
id integer primary autoincrement,
version integer}
table 2:
create table item_version {
id integer,
version integer,
primary key (id, version)
}
When i add a new item, i add it to items and have ID auto increment. however, if i ever have a new version of the same id, i add it to item_version. basically, i use item_version to hold everything but items to just to generate unique ids.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…