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

Asked

Viewed 784 times

0

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

I tried to use ALTER TABLE to solve the problem, but I haven’t been able to.

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

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

My scheme currently is:

CREATE TABLE users (id INTEGER, name STRING);
  • 1

    Wallace see if this helps: http://stackoverflow.com/questions/631060/can-i-alter-a-column-in-an-sqlite-table-autoincrement-after-creation

  • I don’t know much about the command line. I sometimes appeal to this application: http://sqlitebrowser.org/

  • @Zooboomafoo the tip is good, I will take a look yes. Until then, I always fiddled in SQLITE via command line.

1 answer

2

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

See the command below:

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

To change a field to another type in sqlite, so searching is not possible using an alter table >> alter column command, you would need to juggle creating a backup table, then recreating the main table, then repopulating the main table with the data that was recorded in the backup table.

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

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

Download: http://www.sqliteexpert.com/download.html

Browser other questions tagged

You are not signed in. Login or sign up in order to post.