Is there any way to change a column of an existing table to "auto increment"?

0

I created a table in Sqlite3, however I forgot to add AUTO INCREMENT to column id .

I tried to use ALTER TABLE to solve the problem, but I still could not.

In SQLITE, is it allowed to define a column as auto increment , after the table has already been created?

If it does not, what is the solution to make my field id as auto increment ?

My current schema is:

CREATE TABLE users (id INTEGER, name STRING);
    
asked by anonymous 04.01.2017 / 12:06

1 answer

2

To create a field with auto increment, first it is necessary that the data type of the column is INTEGER.

See the command below:

CREATE TABLE [tabela] (
 [ID] INTEGER PRIMARY KEY AUTOINCREMENT
);

To change a field to another type in sqlite, so I researched is not possible using an alter table > > alter column, it would be necessary to juggle creating a backup table, then re-creating the main table, then repopulating the main table with the data that was written to the backup table.

Or if you prefer, you can use a visual tool to make the ID field change to AutoIncrement.

I use the SQLite Expert Personal 4.x tool and it does it wonderfully well.

Download: link

    
04.01.2017 / 14:08