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

后端 未结 11 1826
野趣味
野趣味 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:42

    While the Sqlite site gives you an example how to do it with a table with only a three fields, it gets nasty with one of 30 fields. Given you have a table called OldTable with many fields, the first of which is "ID" filled with integers. Make a copy of your database for backup. Using the command program dot commands,

        .output Oldtable.txt
        .dump Oldtable
        Drop Table Oldtable;
    

    Open Oldtable.txt in Microsoft Word or a grep like text editor. Find and Replace your Integer field elements with NULL.(You may need to adjust this to fit your fields). Edit the Create Table line so the field that was defined as Integer is now INTEGER PRIMARY KEY AUTOINCREMENT. Save as NewTable.txt

    Back in the command program dot

       .read NewTable.txt
    

    Done. ID is now autoincrement.

提交回复
热议问题