2013-03-18 111 views
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; 

•什麼是錯誤的代碼?什麼導致這個錯誤?請幫忙。除了最後一個之外,所有其他表格,包括這個代碼的前兩個都是完美的。錯誤1005(HY000)|無法創建表

回答

2

你的最後2個外鍵不引用索引鍵在主表

編輯:他們不應該。 FinishedMaterialWeight和FinsihedMaterialUnits不是finishedMaterials表的唯一鍵。只需要FinishedMaterialID。

1

維基百科..

「外鍵是在關係表中的字段匹配另一個表的候選鍵。外鍵可以用來對照表」。

然而,

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

上面列,FinishedMaterialUnits和FinishedMaterialWeight,不在其表候選鍵。

我認爲你的設計有點缺陷,因爲這兩列沒有被SUPPOSED表中的每一行都是唯一的。例如,單位計數可以重複。

相關問題