由于某种原因,我需要在两个表之间创建外键的帮助,我一再被告知客户编号不存在,
这是第一张桌子的代码
SET FOREIGN_KEY_CHECKS = 0;
DROP TABLE IF EXISTS Customer;
CREATE TABLE Customer
(
CustomerNumber int NOT NULL,
CustomerName varchar(255),
CustomerAddress varchar(255),
CustomerPhoneNumber varchar(255),
JoinDate varchar(255),
PetName varchar(255),
PayScheme varchar(255),
PremiumPayDate varchar(255),
PRIMARY KEY (CustomerNumber)
);
这是第二个的代码
DROP TABLE IF EXISTS Policies;
CREATE TABLE Policies
(
PolicyID int NOT NULL,
PolicyNumber int NOT NULL,
PetType varchar(255),
EffectiveDate varchar(255),
PRIMARY KEY (PolicyID),
CONSTRAINT fk_CustomerNumber
FOREIGN KEY (CustomerNumber)
REFERENCES Customer(CustomerNumber)
);
我一再被告知关键列客户编号不存在
最佳答案
Policies
表缺少customerNumber
列。您需要在声明foreign key
约束之前添加它。尝试这个:
DROP TABLE IF EXISTS Policies;
CREATE TABLE Policies
(
PolicyID int NOT NULL,
PolicyNumber int NOT NULL,
PetType varchar(255),
CustomerNumber int NOT NULL, --Added here
EffectiveDate varchar(255),
PRIMARY KEY (PolicyID),
CONSTRAINT fk_CustomerNumber
FOREIGN KEY (CustomerNumber)
REFERENCES Customer(CustomerNumber)
);
关于mysql - 无法创建外键MySQL工作台,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41491009/