我正在创建三个表,当尝试绑定(bind)主键和外键时,我收到错误消息“表中不存在键列“用户名””。
有人可以看一下我的代码并告诉我我做错了什么吗?我已经尝试删除数据库并修改表几次,但我仍然收到相同的消息。这是我的代码,提前感谢您的帮助!
create database testproject
use testproject
create table caller_info
(
caller_id int(11) unsigned auto_increment primary key not null,
first_name varchar(35) not null,
Last_name varchar(35) not null,
phone_number int(25) not null
);
create table caller_call_record
(
call_record_id int(11),
Call_Description varchar(50),
franchise_id int(10) not null,
email varchar(40) not null,
username varchar(25) primary key not null
);
create table caller_escalation
(
call_escalation_id int(11) unsigned auto_increment not null,
Second_Level varchar(5) not null,
caller_id int(11) not null,
PRIMARY KEY(call_escalation_id),
FOREIGN KEY(caller_id)
REFERENCES caller_info(caller_id),
FOREIGN KEY(username) REFERENCES caller_call_record(username)
);
最佳答案
正如所指出的,您的表 caller_escalation
需要一个名为 username
的列才能创建外键。
听起来,一旦您添加了该内容,您现在就会从 MySQL 收到无法添加外键约束
错误。出现这种错误时,首先想到的是您为表使用了错误的引擎。您很可能使用不支持外键引用的 MyISAM
- 为了使用它们,您需要将所有表上的引擎更改为 InnoDB
。
关于php - "Key column ' 用户名 ' doesn' t 存在于表中”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20342931/