SQLite add Primary Key

前端 未结 11 2443
你的背包
你的背包 2020-11-28 06:07

I created a table in Sqlite by using the CREATE TABLE AS syntax to create a table based on a SELECT statement. Now this table has no primary key b

11条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-28 06:27

    As long as you are using CREATE TABLE, if you are creating the primary key on a single field, you can use:

    CREATE TABLE mytable (
    field1 TEXT,
    field2 INTEGER PRIMARY KEY,
    field3 BLOB,
    );
    

    With CREATE TABLE, you can also always use the following approach to create a primary key on one or multiple fields:

    CREATE TABLE mytable (
    field1 TEXT,
    field2 INTEGER,
    field3 BLOB,
    PRIMARY KEY (field2, field1)
    );
    

    Reference: http://www.sqlite.org/lang_createtable.html

    This answer does not address table alteration.

提交回复
热议问题