【问题标题】:Create table with foreign key in sqlite在 sqlite 中使用外键创建表
【发布时间】:2014-09-26 17:25:26
【问题描述】:
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 整数主键, 日期时间日期时间, 用户回答文本, 是正确的文本, Question_ID TEXT

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

【问题讨论】:

  • 只需执行正确的CREATE TABLE 语句。有什么问题?

标签: ios sqlite


【解决方案1】:

语法中的一些错误:

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

  • 在结束 ) 括号之前删除 ,

【讨论】:

    【解决方案2】:

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

    外键 (colonne_name) REFERENCES parent_table_name(colonne_name)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-29
      • 1970-01-01
      • 2012-12-04
      相关资源
      最近更新 更多