简体   繁体   中英

How do I update UNIQUE values in my case using sqlite3?

I have a table:

CREATE TABLE IF NOT EXISTS "{subject}" (
          id INTEGER PRIMARY KEY,
          text TEXT,
          fileid TEXT,
          date TEXT,
          author TEXT);

I want to set increase all id values by one:

UPDATE "{subject}" SET id = id + 1

and this gives me UNIQUE constraint failed error.

What should I do if I need to keep uniqueness while increasing values this way?

Update: I want it basically like this:

-1, 0, 1
update()
0, 1, 2

Maybe I should copy the table without UNIQUE constraint, update values, then copy it back?

May be strange but you can try: first update id=id+100 and then update id=id-99 so no break of constraint. You must use a number/number-1 big enough to prevent constraint to happen. – Ptit Xav 36 mins ago

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM