0
create table finishedMaterials(
FinishedMaterialID VARCHAR(4) NOT NULL,
FinishedMaterialType VARCHAR(10) NOT NULL,
FinishedMaterialWeight INT(5) NOT NULL,
FinishedMaterialUnits INT(5) NOT NULL,
CONSTRAINT PRIMARY KEY (FinishedMaterialID))ENGINE=INNODB;

create table vehicles(
VehicleID VARCHAR(4) NOT NULL,
VehicleNumber VARCHAR(8),
VehicleType VARCHAR(10) NOT NULL,
VehicleCondition VARCHAR(10) NOT NULL,
VehicleRemarks VARCHAR(100),
CONSTRAINT PRIMARY KEY (VehicleID))ENGINE=INNODB;

create table transport(
TransportID VARCHAR(4) NOT NULL,
VehicleID VARCHAR(4) NOT NULL,
FinishedMaterialID VARCHAR(4) NOT NULL,
FinishedMaterialUnits INT(5) NOT NULL,
FinishedMaterialWeight INT(5) NOT NULL,
CONSTRAINT PRIMARY KEY (TransportID),
CONSTRAINT FOREIGN KEY (VehicleID) REFERENCES vehicles(VehicleID),
CONSTRAINT FOREIGN KEY (FinishedMaterialID) REFERENCES finishedMaterials(FinishedMaterialID),
CONSTRAINT FOREIGN KEY (FinishedMaterialUnits) REFERENCES finishedMaterials(FinishedMaterialUnits),
CONSTRAINT FOREIGN KEY (FinishedMaterialWeight) REFERENCES finishedMaterials(FinishedMaterialWeight))ENGINE=INNODB;

• 这段代码有什么问题?是什么导致了这个错误?请帮忙。除了最后一个之外,所有其他表格,包括此代码的前 2 个,都可以正常工作。

4

2 回答 2

2

您的最后 2 个外键不引用主表中的索引键

编辑:他们不应该。FinishedMaterialWeight 和 FinsihedMaterialUnits 不是 finishedMaterials 表的唯一键。只需要 FinishedMaterialID。

于 2013-03-18T14:53:00.923 回答
1

来自维基百科。。

“外键是关系表中与另一个表的候选键匹配的字段。外键可用于交叉引用表。”

然而,

CONSTRAINT FOREIGN KEY (FinishedMaterialUnits) REFERENCES finishedMaterials(FinishedMaterialUnits),
CONSTRAINT FOREIGN KEY (FinishedMaterialWeight) REFERENCES finishedMaterials(FinishedMaterialWeight))ENGINE=INNODB;

上面的列 FinishedMaterialUnits 和 FinishedMaterialWeight 不是其表中的候选键。

我认为您的设计有点缺陷,因为这两列不应该对表中的每一行都是唯一的。例如,单位计数可以重复。

于 2013-03-18T14:53:57.143 回答