SQLite multi-Primary Key on a Table, one of them is Auto Increment

前端 未结 4 1185
难免孤独
难免孤独 2020-12-08 15:48

I have multiple (composite) primary keys on a table and one of them will be auto increment. However, interestingly SQLite allows usage of AUTOINCREMENT keyword

4条回答
  •  無奈伤痛
    2020-12-08 16:19

    UNIQUE INDEX alone doesn't have the same effect as PRIMARY KEY. A unique index will allow a NULL; a primary key constraint won't. You're better off declaring both those constraints.

    CREATE TABLE ticket (
         id INTEGER PRIMARY KEY AUTOINCREMENT,
         seat TEXT NOT NULL, 
         payment INTEGER,
         UNIQUE (id, seat));
    

    You should also think hard about whether you really need to accept NULL payments.

提交回复
热议问题