Mysql

表錯誤中不存在鍵列“FK_UserQuestion”

  • January 6, 2017

我使用了以下語法並在最後不斷收到以下錯誤“表中不存在鍵列‘FK_UserQuestion’”

CREATE DATABASE QA;

CREATE TABLE QA.Users
(
UserID int AUTO_INCREMENT PRIMARY KEY,
fName varchar(150),
lname varchar(150),
isadmin bit
) engine = INNODB;

CREATE TABLE QA.Question
(
QuestionID int AUTO_INCREMENT PRIMARY KEY,
UserID int,
Question varchar(65535),
FOREIGN KEY (FK_UserQuestion, UserID)     
   REFERENCES QA.Users (UserID) 
) engine = INNODB;

CREATE TABLE QA.Answer
(
AnswerID int AUTO_INCREMENT PRIMARY KEY,
QuestionID int,
UserID int,
Answer BLOB(65535),
IsSolver bit,
FOREIGN KEY (User_ID, UserID)
   REFERENCES QA.Users(UserID)
FOREIGN KEY ( FK_AnswerQuestion,QuestionID)     
   REFERENCES QA.Question (QuestionID) 
) engine = INNODB;

我試圖推出“QA”。從每個表中,並嘗試執行以分號分隔的語句,每輪也是,但沒有解決。嘗試了很多東西,但我所做的一切都不適用於MySQL Workbench 6.3 CE

您的外鍵格式錯誤。語法應該是:

FOREIGN KEY <name> (column_1, ..., column_n)
REFERENCES <parent table> (column_1, ..., columns_n)

在你的情況下:

FOREIGN KEY FK_UserQuestion (UserID)
REFERENCES QA.Users (UserID)

是的

CREATE TABLE QA.Question
( QuestionID int AUTO_INCREMENT PRIMARY KEY
, UserID int
, Question varchar(65535)
, FOREIGN KEY FK_UserQuestion (UserID)     
 REFERENCES QA.Users (UserID) 
) engine = INNODB;

還應該對 QA.Answer 進行類似的更改

引用自:https://dba.stackexchange.com/questions/160191