2012-01-31 57 views
0

上自動增量查詢我有以下數據庫:我對SQLite數據庫

CREATE TABLE QUESTION (
    QUES_ID INTEGER AUTO INCREMENT, 
    TOPIC_ID INTEGER NOT NULL, 
    QUESTION_DESC TEXT NOT NULL, 
    CHOICE_A TEXT, 
    CHOICE_B TEXT, 
    CHOICE_C TEXT, 
    CHOICE_D TEXT, 
    PRIMARY KEY (QUES_ID,TOPIC_ID), 
    FOREIGN KEY (TOPIC_ID) REFERENCES TOPIC(TOPIC_ID) 
); 

當我插入信息這樣的...

INSERT INTO 
    QUESTION (TOPIC_ID,QUESTION_DESC,CHOICE_A,CHOICE_B,CHOICE_C,CHOICE_D) 
VALUES 
    (1,'Blue or blue?','red?','yellow?','black?','yellow again?'); 

我得到下面的結果,在ques_id沒有分配!

QUES_ID  TOPIC_ID QUESTION_DESC CHOICE_A CHOICE_B CHOICE_C CHOICE_D  
---------- --------- ------------- --------- --------- --------- ------------- 
      1   Blue or blue? red?  yellow? black? yellow again? 

回答

0

SQLite FAQ

A column declared INTEGER PRIMARY KEY will autoincrement.

所以,你的DDL語句應該看起來更像是:

CREATE TABLE QUESTION (
    QUES_ID INTEGER PRIMARY KEY, 
    TOPIC_ID INTEGER NOT NULL, 
    QUESTION_DESC TEXT NOT NULL, 
    CHOICE_A TEXT, 
    CHOICE_B TEXT, 
    CHOICE_C TEXT, 
    CHOICE_D TEXT, 
    -- the primary key must be QUES_ID 
    -- if you want it to auto-increment 
    -- so if you really want you can add 
    -- a unique constraint here 
    CONSTRAINT qt UNIQUE (QUES_ID, TOPIC_ID), 
    FOREIGN KEY (TOPIC_ID) REFERENCES TOPIC (TOPIC_ID) 
); 
+0

這不會剝奪我與主題表的合格協作嗎? – mister 2012-01-31 06:05:09

0

嘗試QUES_ID AUTO_INCREMENT

,真的,如果你總是希望它在那裏使它

QUES_ID MEDIUMINT NOT NULL AUTO_INCREMENT 
0

您需要: QUES_ID INTEGER PRIMARY KEY AUTOINCREMENT

0

嘗試這樣的插入數據:

INSERT INTO QUESTION values (1,'Blue or blue?','red?','yellow?','black?','yellow again?'); 

由於您沒有在您的查詢中提及自動增量列,您也沒有提及NOT NULL,因此它會跳過爲該列輸入數據。

+2

錯誤:表問題有7列,但都提供 6個值這個錯誤是我得到了什麼。 – mister 2012-01-31 06:04:40