Changing the primary key of a table in SQLite

I have the following table:

[id,lang,data] 

If the primary key is id

I want to change the primary key to id,lang without losing the contents of the table. What request should I execute to change it

thanks

+6
source share
1 answer

According to the comment of @GarnerJosh, you cannot run a single command to change it - SQLite does not allow changing existing table structures. Instead, create a new table with a composite primary key and copy the data:

 CREATE TABLE my_table_copy( id INTEGER, lang INTEGER, data TEXT, PRIMARY KEY (id, lang) ); INSERT INTO my_table_copy (id, lang, data) SELECT id, lang, data FROM my_table; DROP TABLE my_table; ALTER TABLE my_table_copy RENAME TO my_table; 
+22
source

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


All Articles