syntax - Primary Key MySQL Error -
i'm trying make database , 1 of tables use composite primary keys. works fine until try make foreign key separate table using 2nd composite key. error given "error 1005 (hy000): can't create table '.kesharaproducts\production.frm' (errno: 150)"
here's code.
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;
the table above use composite primary key.
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;
table above used first of composite keys , works fine.
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;
but in table above, uses 2nd composite primary foreign key, gives out error. how fix this? these last 3 tables. other referred tables created. didn't post tables here, long. when remove "constraint foreign key (rawmatbatchid) references stocks(rawmatbatchid)" last table , add table gets added. not referral 2nd composite key...
table stocks
does not have 2 primary keys. has 1 , one. composite key, made 2 parts: (finishedgoodsbatchid, rawmatbatchid)
.
if want have foreign key
constraints reference table, should reference primary key (or unique key) of table. whole of it. so, in case whole composite primary key of stocks
, not part of it.
that makes foreign key transport
wrong, too. mysql allows have problems if leave this. foreign key should able somehow identify rows in parent table (finishedgoodsbatchid)
cannot (identify rows) alone.
so, 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 done in definitioon of production
table, too.
Comments
Post a Comment