之前有一个问题,这个代码已经解决并导致表工作但现在我收到错误1005并且由于fk而无法创建表
这是第一个表的代码
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),
CustomerNumber int NOT NULL,
PetName varchar(255),
EffectiveDate varchar(255),
PRIMARY KEY (PolicyID),
CONSTRAINT fk_CustomerNumber
FOREIGN KEY (CustomerNumber)
REFERENCES Customer(CustomerNumber),
CONSTRAINT fk_PetName
FOREIGN KEY (PetName)
REFERENCES Customer(PetName)
);
错误代码
19:05:38CREATE TABLE Policies ( PolicyID int NOT NULL, PolicyNumber int NOT NULL, PetType varchar(255), CustomerNumber int NOT NULL, PetName varchar(255), EffectiveDate varchar(255), PRIMARY KEY (PolicyID), CONSTRAINT fk_CustomerNumber FOREIGN KEY (CustomerNumber) REFERENCES Customer(CustomerNumber), CONSTRAINT fk_PetName FOREIGN KEY (PetName) REFERENCES Customer(PetName) ) Error Code: 1005. Can't create table 'cis22723684.Policies' (errno: 150) 0.031 sec
我已经解决了问题,请参阅代码编辑。
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, PetName)
);
DROP TABLE IF EXISTS Policies;
CREATE TABLE Policies
(
PolicyID int NOT NULL,
PolicyNumber int NOT NULL,
PetType varchar(255),
CustomerNumber int NOT NULL,
PetName varchar(255),
EffectiveDate varchar(255),
PRIMARY KEY (PolicyID),
CONSTRAINT fk_CustomerNumber
FOREIGN KEY (CustomerNumber, PetName)
REFERENCES Customer(CustomerNumber, PetName)
);
它似乎使用pk作为复合并在一个约束下包含pks
然而,当我尝试创建一个新的fk时,尽管保留了使用的代码,我仍然收到与之前相同的错误代码
第一张表
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, PetName)
);
第二张表
DROP TABLE IF EXISTS Policies;
CREATE TABLE Policies
(
PolicyID int NOT NULL,
PolicyNumber int NOT NULL,
PetType varchar(255),
CustomerNumber int NOT NULL,
PetName varchar(255),
EffectiveDate varchar(255),
PRIMARY KEY (PolicyID),
CONSTRAINT fk_CustomerNumber_PetName
FOREIGN KEY (CustomerNumber, PetName)
REFERENCES Customer(CustomerNumber, PetName)
);
第三表
DROP TABLE IF EXISTS Claims;
CREATE TABLE Claims
(
ClaimsID int NOT NULL,
AmountForReimbursement varchar(255),
PolicyID varchar(255),
PRIMARY KEY (ClaimsID),
CONSTRAINT fk_PolicyID
FOREIGN KEY (PolicyID)
REFERENCES Policies(PolicyID)
);
是一个简单的问题,int NOT NULL被识别为varchar,我需要一些睡眠:')
答案 0 :(得分:0)
问题出现在下面的第二个约束中,您尝试引用非键列PetName
这是不可能的,而且它违反了规范化概念。您的Customer
表仅将主键定义为PRIMARY KEY (CustomerNumber)
。
CONSTRAINT fk_PetName
FOREIGN KEY (PetName)
REFERENCES Customer(PetName)
从CREATE
语句中删除上面提到的FK约束定义,它可以正常工作。不要相信,然后看到这个小提琴:http://sqlfiddle.com/#!9/86a927
根据您的编辑:您的FK约束定义必须如下所示。请参阅修改过的小提琴:http://sqlfiddle.com/#!9/3a019
CONSTRAINT fk_CustomerNumber_PetName
FOREIGN KEY (CustomerNumber, PetName)
REFERENCES Customer(CustomerNumber, PetName)