在sqlite中使用外键创建表

时间:2014-08-04 09:14:36

标签: ios sqlite

I am using sqlite tool of Mozilla Firefox to manage my database and i have to create a table in which i have to use foreign key . How to do that?
These are my tables,

Table Name:QuestionWithAnswer

Column_Name=Format:
Date=DATETIME(Primary Key),
Question=Text,
Answer=Text,
UserAnswer=Text,
isCorrext=Text.

Table Name:Question
Column_Name=Format:
Question_ID=Integer(Primary Key)
Question=Text


Table Name:Record
Column_Name=Format:
id=integer(Primary Key)
DatewithTime=DATETIME(foreign key from QuestionWithAnswer)
UserAnswer=TEXT
isCorrect=BOOl
Question_ID=integer(foreign key from Question)

以上是我的给定表格列,我想创建第三个表名称使用外键记录。 这是我的创建stmt

  TABLE Records
(
  id     INTEGER PRIMARY KEY, 
  DateWithTime   DATETIME, 
  UserAnswer TEXT,
  isCorrect TEXT,
  Question_ID TEXT

  FOREIGN KEY(DateWithTime) REFERENCES QuestionWithAnswer(Date),
  FOREIGN KEY(Question_ID) REFERENCES Question(question_ID),
);

但它给了我错误 " QLiteManager:可能的SQL语法错误:CREATE TABLE Records (   id INTEGER PRIMARY KEY,   DateWithTime DATETIME,   UserAnswer TEXT,   isCorrect TEXT,   Question_ID TEXT

FOREIGN KEY(DateWithTime)REFERENCES QuestionWithAnswer(Date),   FOREIGN KEY(Question_ID)REFERENCES问题(question_ID), ); [附近" FOREIGN":语法错误] 例外名称:NS_ERROR_FAILURE 异常消息:组件返回失败代码:0x80004005(NS_ERROR_FAILURE)[mozIStorageConnection.createStatement]"。

2 个答案:

答案 0 :(得分:1)

语法中的一些错误:

  • 在第一个,表约束之前添加FOREIGN KEY

  • 在结束,之前删除)

答案 1 :(得分:0)

定义属性后,添加以下代码

外键(colonne_name)参考parent_table_name(colonne_name)