Changing a unique table field

I have a table that has a unique key with two restrictions in it. I want to change the table so that I can remove one of the restrictions in a unique field.

My current db schema is:

CREATE TABLE testtable(test1 TEXT, test2 TEXT, test3 TEXT, test4 TEXT DEFAULT FALSE,UNIQUE (test1,test2))

I already have this table filled with data, and you do not want to drop and recreate this table in my next version. Is there any way to change the unique table field. I have not set a limit for my table.

I want my database schema to be the same as after the upgrade.

CREATE TABLE testtable(test1 TEXT, test2 TEXT, test3 TEXT, test4 TEXT DEFAULT FALSE,UNIQUE (test1))

+3
source share
1 answer

ALTER TABLE DROP CONSTRAINT .... sqlite RENAME TABLE ADD COLUMN ALTER TABLE ().

, , :

CREATE TABLE testtable2(
   test1 TEXT, test2 TEXT, test3 TEXT, test4 TEXT DEFAULT FALSE,
   UNIQUE (test1)
);

:

INSERT INTO testtable2 SELECT * FROM testtable;

:

DROP TABLE testtable;

, , :

ALTER TABLE testtable2 RENAME TO testtable;

UPDATE:

, . , :

test1       test2       test3       test4     
----------  ----------  ----------  ----------
a           a           100         1         
a           b           200         2         
a           c           300         3         

INSERT INTO testtable2 SELECT * FROM testtable; , test1 :

SQL error: column test1 is not unique
+7

Source: https://habr.com/ru/post/1764695/


All Articles