简体   繁体   中英

Create table with foreign key in 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)

Above are my given tables with columns and i want to create 3rd table names Record by using foreign key. This is my create 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),
);

but it is giving me error as " QLiteManager: Likely SQL syntax error: 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(question_ID), ); [ near "FOREIGN": syntax error ] Exception Name: NS_ERROR_FAILURE Exception Message: Component returned failure code: 0x80004005 (NS_ERROR_FAILURE) [mozIStorageConnection.createStatement] ".

Some errors in the syntax:

  • Add a , before the first FOREIGN KEY table constraint.

  • Remove the , before the closing ) paren.

after you define your attribut, add this following code

FOREIGN KEY (colonne_name) REFERENCES parent_table_name(colonne_name)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM