SQLite add Primary Key

前端 未结 11 2406
你的背包
你的背包 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:23

    You can do it like this:

    CREATE TABLE mytable (
    field1 text,
    field2 text,
    field3 integer,
    PRIMARY KEY (field1, field2)
    );
    
    0 讨论(0)
  • 2020-11-28 06:26

    Use tool like DB Browser for SQLite, it allows to add PK, AI by simple right clicking on table -> modify.

    0 讨论(0)
  • 2020-11-28 06:27

    You can't modify SQLite tables in any significant way after they have been created. The accepted suggested solution is to create a new table with the correct requirements and copy your data into it, then drop the old table.

    here is the official documentation about this: http://sqlite.org/faq.html#q11

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-28 06:33

    According to the sqlite docs about table creation, using the create table as select produces a new table without constraints and without primary key.

    However, the documentation also says that primary keys and unique indexes are logically equivalent (see constraints section):

    In most cases, UNIQUE and PRIMARY KEY constraints are implemented by creating a unique index in the database. (The exceptions are INTEGER PRIMARY KEY and PRIMARY KEYs on WITHOUT ROWID tables.) Hence, the following schemas are logically equivalent:

    CREATE TABLE t1(a, b UNIQUE);
    
    CREATE TABLE t1(a, b PRIMARY KEY);
    
    CREATE TABLE t1(a, b);
    CREATE UNIQUE INDEX t1b ON t1(b); 
    

    So, even if you cannot alter your table definition through SQL alter syntax, you can get the same primary key effect through the use an unique index.

    Also, any table (except those created without the rowid syntax) have an inner integer column known as "rowid". According to the docs, you can use this inner column to retrieve/modify record tables.

    0 讨论(0)
  • 2020-11-28 06:34
    sqlite>  create table t(id int, col2 varchar(32), col3 varchar(8));
    sqlite>  insert into t values(1, 'he', 'ha');
    sqlite>
    sqlite>  create table t2(id int primary key, col2 varchar(32), col3 varchar(8));
    sqlite>  insert into t2 select * from t;
    sqlite> .schema
    CREATE TABLE t(id int, col2 varchar(32), col3 varchar(8));
    CREATE TABLE t2(id int primary key, col2 varchar(32), col3 varchar(8));
    sqlite> drop table t;
    sqlite> alter table t2 rename to t;
    sqlite> .schema
    CREATE TABLE IF NOT EXISTS "t"(id int primary key, col2 varchar(32), col3 varchar(8));
    sqlite>
    
    0 讨论(0)
提交回复
热议问题