php - "Key column ' 用户名 ' doesn' t 存在于表中”

标签 php mysql sql database

我正在创建三个表,当尝试绑定(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/

相关文章:

php - 如果单词错误或拼写错误或希腊单词得到英语结果,我怎样才能得到结果

python - 尝试使用 django 设置 mysql 时出错

javascript - Sequelize 自定义验证 - 无法访问实体中的所有字段

sql - 来自不同数据库的表上的外键

php - 用 PHP 绘制 Google 图表

javascript - symfony框架中的Ajax调用

php - 从今天开始循环以从一组模板生成类次/轮类列表

mysql - SQL 选择内部选择

java - MySql连接太多

sql - 如何按照用户的期望排序和显示字母和数字的混合列表?