我正在尝试创建一个数据库,并且我的一个表使用复合主键。这工作正常,直到我尝试使用第二个复合键为单独的表创建外键。给出的错误是“ERROR 1005 (HY000): Can't create table '.kesharaproducts\production.frm' (errno: 150)”
这是代码。
create table stocks(
rawMatBatchID VARCHAR(4) NOT NULL,
finishedGoodsBatchID VARCHAR(4) NOT NULL,
rawMaterialID VARCHAR(4) NOT NULL,
finishedMaterialID VARCHAR(4) NOT NULL,
supplierID VARCHAR(5) NOT NULL,
rawMaterialType VARCHAR(10) NOT NULL,
finishedMaterialType VARCHAR(10) NOT NULL,
rawMatWeight VARCHAR(5) NOT NULL,
finishedGoodsWeightKg INT(5) NOT NULL,
finishedGoodsWeightG INT(5) NOT NULL,
finishedGoodsUnits INT(5) NOT NULL,
finishedGoodsDate VARCHAR(15) NOT NULL,
rawMatDate VARCHAR(15) NOT NULL,
PRIMARY KEY (finishedGoodsBatchID, rawMatBatchID),
CONSTRAINT FOREIGN KEY (finishedMaterialID) REFERENCES finishedMaterials(finishedMaterialID),
CONSTRAINT FOREIGN KEY (supplierID) REFERENCES supplierDetails(supplierID),
CONSTRAINT FOREIGN KEY (rawMaterialID) REFERENCES rawMaterials(rawMaterialID))ENGINE=INNODB;
上表使用复合主键。
create table transport(
transportID VARCHAR(4) NOT NULL,
vehicleID VARCHAR(4) NOT NULL,
finishedGoodsBatchID VARCHAR(4) NOT NULL,
finishedGoodsUnits INT(5) NOT NULL,
finishedGoodsWeightKg INT(5),
finishedGoodsWeightG INT(5),
transportDate VARCHAR(15),
CONSTRAINT PRIMARY KEY (transportID),
CONSTRAINT FOREIGN KEY (vehicleID) REFERENCES vehicles(vehicleID),
CONSTRAINT FOREIGN KEY (finishedGoodsBatchID) REFERENCES stocks(finishedGoodsBatchID)
)ENGINE=INNODB;
上表使用了第一个复合键并且工作正常。
create table production(
productionBatchID VARCHAR(4) NOT NULL,
finishedMaterialID VARCHAR(4) NOT NULL,
rawMaterialID VARCHAR(4) NOT NULL,
productionDate VARCHAR(15),
rawMatBatchID VARCHAR(4),
initialWeight INT(5),
beforeWeight INT(5),
afterWeight INT(5),
finalWeight INT(5),
packingWeight INT(5),
noOfUnits INT(5),
wastage INT(5),
CONSTRAINT PRIMARY KEY (productionBatchID),
CONSTRAINT FOREIGN KEY (finishedMaterialID) REFERENCES finishedMaterials(finishedMaterialID),
CONSTRAINT FOREIGN KEY (rawMaterialID) REFERENCES rawMaterials(rawMaterialID),
CONSTRAINT FOREIGN KEY (rawMatBatchID) REFERENCES stocks(rawMatBatchID))ENGINE=INNODB;
但是在上表中,它使用第二个复合主作为外键,它给出了错误。如何解决这个问题?这是最后三个表。创建所有其他引用的表。我没有在这里发布所有表格,太长了。当我从最后一个表中删除“CONSTRAINT FOREIGN KEY (rawMatBatchID) REFERENCES stock(rawMatBatchID)”并添加它被添加的表时。但不是对第二个复合键的引用......