简体   繁体   中英

Add unique column to already exist sqlite table

I use SQLite database in Android app, and i want to know how to add a unique column to already created table. by this i mean that the exist table uniqness determine by 2 values and i want to edit the table uniqueness to be determined by 3 values (the 2 exist and 1 more).

is it possible?

The writable_schema trick does not work for UNIQUE because the internal index would need to be changed.

The only way to make this change is with a temporary copy of the table:

CREATE TABLE NewTable(
    [...],
    UNIQUE(Col1, Col2, Col3)
);
INSERT INTO NewTable SELECT * FROM MyTable;
DROP TABLE MyTable;
ALTER TABLE NewTable RENAME TO MyTable;

(This should be wrapped in a transaction; Android's onUpdate does this automatically.)

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