Can I alter a column in an sqlite table to AUTOINCREMENT after creation?

后端 未结 11 1795
野趣味
野趣味 2020-12-13 13:08

Can I make a field AUTOINCREMENT after made a table? For example, if you create a table like this:

create table person(id integer primary key, n         


        
相关标签:
11条回答
  • 2020-12-13 13:48

    Simple Answer is as below,

    CREATE TABLE [TEST] (
      [ID] INTEGER PRIMARY KEY AUTOINCREMENT, 
      [NAME] VARCHAR(100));
    

    and you are done.

    0 讨论(0)
  • 2020-12-13 13:50

    you can alter the table, altering the column definition

    0 讨论(0)
  • 2020-12-13 13:53

    You can do it with SQLite Expert Personal 4:

    1) Select the table and then go to "Design" tab > "Columns" tab.

    2) Click "Add" and select the new column name, and type INTEGER and Not Null > Ok.

    3) Go to "Primary Key" tab in "Desgin tab". Click "Add" and select the column you just created. Check the "Autoincrement" box.

    4) Click "Apply" on the right bottom part of the window.

    If you go back to the "Data" tab, you will see your new column with the autogenerated numbers in it.

    0 讨论(0)
  • 2020-12-13 13:55

    From the SQLite Faq

    Short answer: A column declared INTEGER PRIMARY KEY will autoincrement

    So when you create the table, declare the column as INTEGER PRIMARY KEY and the column will autoincrement with each new insert.

    Or you use the SQL statment ALTER to change the column type to an INTEGER PRIMARY KEY after the fact, but if your creating the tables yourself, it's best to do it in the initial creation statement.

    0 讨论(0)
  • 2020-12-13 13:57

    You cannot alter columns on a SQLite table after it has been created. You also cannot alter a table to add an integer primary key to it.

    You have to add the integer primary key when you create the table.

    0 讨论(0)
提交回复
热议问题