Table stocks
does not have 2 primary keys. It has one and only one. It is a composite key, made from 2 parts: (finishedGoodsBatchID, rawMatBatchID)
.
If you want to have FOREIGN KEY
constraints that reference a table, you should reference a primary key (or a unique key) of that table. The whole of it. So, in this case the whole composite primary key of stocks
and not a part of it.
That makes the foreign key from transport
wrong, too. MySQL allows it but you will have problems if you leave it like this. A foreign key should be able to somehow identify rows in the parent table but (finishedGoodsBatchID)
cannot (identify rows) alone.
So, the transport
definition should be:
CREATE TABLE transport(
transportID VARCHAR(4) NOT NULL,
vehicleID VARCHAR(4) NOT NULL,
finishedGoodsBatchID VARCHAR(4) NOT NULL,
rawMatBatchID VARCHAR(4) NOT NULL, -- added
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 -- changed
FOREIGN KEY (finishedGoodsBatchID, rawMatBatchID)
REFERENCES stocks (finishedGoodsBatchID, rawMatBatchID)
) ENGINE=INNODB;
A similar change should be done in the definitioon of the production
table, too.