问题
had an issue before with this code which was resolved and caused the table to work but now i am receiving error 1005 and the table cannot be created due to the fk
here is the code for the first table
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)
);
and here is the code for the second table
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)
);
error code is
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
i have resolved the issue see code edited.
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)
);
it appeared to be using the pk as composite and including the pks under one constraint
however when i attempt to create a new fk i am receiving the same error code as before despite keeping the code used
first table
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)
);
second table
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)
);
third table
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)
);
was a simple matter of the int NOT NULL being identified as varchar, i need some sleep :')
回答1:
Problem is in the second constraint as below where you are trying to reference a non-key column PetName
which is not possible and it's against normalization concept. Your Customer
table defines only primary key as PRIMARY KEY (CustomerNumber)
.
CONSTRAINT fk_PetName
FOREIGN KEY (PetName)
REFERENCES Customer(PetName)
Remove the above mentioned FK constraint definition from your CREATE
statement and it works just fine. Don't believe then see this fiddle: http://sqlfiddle.com/#!9/86a927
Per your edit: Your FK constraint definition has to be like below. See the modified fiddle: http://sqlfiddle.com/#!9/3a019
CONSTRAINT fk_CustomerNumber_PetName
FOREIGN KEY (CustomerNumber, PetName)
REFERENCES Customer(CustomerNumber, PetName)
来源:https://stackoverflow.com/questions/41492501/mysql-foreign-key-causing-table-to-drop