表中不存在键列 [英] Key column doesn't exist in table

查看:97
本文介绍了表中不存在键列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法添加引用另一个表的外键字段.

I'm having trouble adding a foreign key field that references another table.

首先,我按以下方式创建了用户表:

First I created the users table as so:

CREATE TABLE users (
user_id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
userName VARCHAR(256) NOT NULL,
userEmail VARCHAR (256) NOT NULL,
userPwd VARCHAR(256) NOT NULL,
);

然后我希望测验表具有一个外键,该外键引用第一个表中的user_id.

then I'd like the quizzes table to have a foreign key that references the user_id from the first table

CREATE TABLE quizzes (
quizId INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
quizName VARCHAR(128) NOT NULL,
quizMax SMALLINT(6) NOT NULL,
FOREIGN KEY (user_id) REFERENCES users (user_id)
);

这将引发错误:表中不存在键列'user_id'".

This is throwing the error: 'Key column 'user_id' doesn't exist in table.

其他答案建议检查数据库是否为InnoDB,我确实这样做了.

Other answers advised to check that DB is InnoDB, which I did, and it is.

当用户表中明确存在user_id时,无法理解为什么它告诉我user_id不存在.

Can't understand why it's telling me that user_id doesn't exist, when it clearly does exist in the users table.

推荐答案

首先要检查表 user 的创建是否成功,因为最后一列有额外的,"!

Firstly check if table user is created successfully, due to the additional ',' on last column!

第二,表测验中未定义您在 FOREIGN KEY(user_id)中引用的列,您需要在测验表格.

Secondly, the column you reffered in FOREIGN KEY(user_id) is not defined in table quizzes, you need to add this column in quizzes table.

这篇关于表中不存在键列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆