Mysql #1072-键列';客户ID';不';不存在于表中

Mysql #1072-键列';客户ID';不';不存在于表中,mysql,sql,database,Mysql,Sql,Database,这是父表 CREATE TABLE Customer( Customer_ID INT(5) not null, CustName VARCHAR(50) not null, CustSurname VARCHAR(50) not null, CustEmail VARCHAR(100) unique not null, CustMobileNo INT(12) not null, HomeAddress VARCHAR(255) not null, Password VARCHAR(10) n

这是父表

CREATE TABLE Customer(
Customer_ID INT(5) not null,
CustName VARCHAR(50) not null,
CustSurname VARCHAR(50) not null,
CustEmail VARCHAR(100) unique not null,
CustMobileNo INT(12) not null,
HomeAddress VARCHAR(255) not null,
Password VARCHAR(10) not null,
constraint c_cuid_pk PRIMARY KEY (Customer_ID))
ENGINE = InnoDB;
当我尝试创建属性表时,它会显示

“#1072-表中不存在键列'Customer_ID'”

这是子表

CREATE TABLE Property(
Property_ID INT(5) not null,
PhouseNumber INT(4) not null,
PstreetName VARCHAR(50) not null,
PpostCode VARCHAR(7) not null,
Pcity VARCHAR(20) not null,
constraint p_phn_pk PRIMARY KEY (Property_ID),
constraint p_cuid_fk FOREIGN KEY (Customer_ID)
references IndividualCustomer(Customer_ID))
ENGINE = InnoDB;

在表
Property
中,必须定义一个名为
Customer\u ID
的列(它可以被命名为任何名称!),然后在其上定义
外键

您的代码当前试图将
间接客户(客户ID)
链接到
属性(客户ID)
,但
属性(客户ID)
不存在

声明约束只会链接列,但不会创建列

CREATE TABLE Property(
Property_ID INT(5) not null,
PhouseNumber INT(4) not null,
PstreetName VARCHAR(50) not null,
PpostCode VARCHAR(7) not null,
Pcity VARCHAR(20) not null,
Customer_ID INT(5) not null,
constraint p_phn_pk PRIMARY KEY (Property_ID),
constraint p_cuid_fk FOREIGN KEY (Customer_ID)
references IndividualCustomer(Customer_ID))
ENGINE = InnoDB;

首先,您应该在
属性
表中添加一列:
Customer\u ID
,以创建外键约束:

Property.Customer_ID → IndividualCustomer.Customer_ID
您不能对不存在的内容进行约束。 或者将表创建查询的++++部分更改为存在的列

constraint p_cuid_fk FOREIGN KEY (+++Customer_ID+++) --to be changed accordingly
references IndividualCustomer(Customer_ID)
示例:将
Customer\u ID
列添加到表声明中会得到以下代码

CREATE TABLE Property(
Property_ID INT(5) not null,
Customer_ID INT(5) not null,
PhouseNumber INT(4) not null,
PstreetName VARCHAR(50) not null,
PpostCode VARCHAR(7) not null,
Pcity VARCHAR(20) not null,
constraint p_phn_pk PRIMARY KEY (Property_ID),
constraint p_cuid_fk FOREIGN KEY (Customer_ID)
references IndividualCustomer(Customer_ID))
ENGINE = InnoDB;

您必须在表属性中添加客户ID。

您是否看到
RO\u属性
表中没有任何名为
Customer\u ID
的列?那么,你怎么能期望向一个不存在的列添加外键约束呢?这是怎么写的?我完全不知道创建/定义该列的代码我用属性表的代码将我的注释转换为答案。仅仅添加该列本身并不是你问题的答案/解决方案,因为该列中没有任何数据。表中有外键
Customer\u ID
列有意义吗MUUCCHHH@ramkumar:对新来者的建议:如果答案解决了您的问题,请单击大复选标记接受答案(✓) 在它旁边,也可以选择向上投票(向上投票要求至少15个信誉点)。如果您发现其他答案有帮助,请向上投票。接受和向上投票有助于未来的读者。请参阅[相关帮助中心文章][1][1]:
CREATE TABLE Property(
Property_ID INT(5) not null,
PhouseNumber INT(4) not null,
PstreetName VARCHAR(50) not null,
PpostCode VARCHAR(7) not null,
Pcity VARCHAR(20) not null,
Customer_ID INT(5) not null,
PRIMARY KEY (Property_ID),
KEY p_cuid_fk (Customer_ID),
constraint p_cuid_fk FOREIGN KEY (Customer_ID)
references Customer(Customer_ID))
ENGINE = InnoDB;