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

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

    You can dump the content to a new table:

    CREATE TABLE failed_banks_id (id integer primary key autoincrement, name text, city text, state text, zip integer, acquired_by text, close_date date, updated_date date);
    
    INSERT INTO failed_banks_id(name, city, state, zip, acquired_by,close_date, updated_date)
    SELECT name, city, state, zip, acquired_by,close_date, updated_date
    FROM failed_banks;
    

    And rename the table:

    DROP TABLE failed_banks;
    ALTER TABLE failed_banks_id RENAME TO failed_banks;
    
    0 讨论(0)
  • 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.

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

    Yes, you can make a column which is autoincrement. Modify the table and add a column. Keep in mind that it is of type INTEGER Primary Key.

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

    Yes Do you have phpmyadmin installed? I believe if you go to the 'structure' tab and look along the right columnn (where the field types are listed) - I think you can change a setting there to make it autoincrement. There is also a SQL query that will do the same thing.

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

    Simplest way — Just export and re-import

    It is possible, and relatively easy. Export the database as an sql file. Alter the SQL file and re-import:

      sqlite3 mydata.db .dump > /tmp/backup.sql
      vi /tmp/backup.sql
      mv mydata.db mydata.db.old
      sqlite3 mydata.db
      sqlite>.read /tmp/backup.sql
    
    0 讨论(0)
  • 2020-12-13 13:48

    Background:

    The new key will be unique over all keys currently in the table, but it might overlap with keys that have been previously deleted from the table. To create keys that are unique over the lifetime of the table, add the AUTOINCREMENT keyword to the INTEGER PRIMARY KEY declaration.

    http://www.sqlite.org/faq.html#q1

    SQLite limitations:

    SQLite supports a limited subset of ALTER TABLE. The ALTER TABLE command in SQLite allows the user to rename a table or to add a new column to an existing table. It is not possible to rename a column, remove a column, or add or remove constraints from a table.

    http://www.sqlite.org/lang_altertable.html

    Hack seems to exist:

    It appears that you can set

    PRAGMA writable_schema=ON;

    Then do a manual UPDATE of the sqlite_master table to insert an "id INTEGER PRIMARY KEY" into the SQL for the table definition. I tried it and it seems to work. But it is dangerous. If you mess up, you corrupt the database file.

    http://www.mail-archive.com/sqlite-users@sqlite.org/msg26987.html

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