create table LOCATION (
warehouseID varchar(3),
locationID varchar(3),
aisle int, shelf int,
bin int,
capacity double,
primary key(warehouseID,locationID),
foreign key(warehouseID) references WAREHOUSE(warehouseID) )
create table PROD_LOCATION (
warehouseID varchar(3),
locationID varchar(3),
productNum int,
quantityOnHand int,
primary key(warehouseID,locationID,productNum),
foreign key(warehouseID) references WAREHOUSE(warehouseID),
foreign key(locationID) references LOCATION(locationID),
foreign key(productNum) references PRODUCT(productNum) )
create table PICKING_LIST (
warehouseID varchar(3),
locationID varchar(3),
productNum int,
requestNum int,
quantityPicked int,
datePicked date,
pickerStaffID int,
primary key(warehouseID,locationID,productNum,requestNum),
foreign key(warehouseID) references WAREHOUSE(warehouseID),
foreign key(locationID) references LOCATION(locationID),
foreign key(productNum) references PRODUCT(productNum),
foreign key(requestNum) references STOCK_REQUEST(requestNum),
foreign key(pickerStaffID) references EMPLOYEE(staffID) )
不断出现错误#1215,但如果我删除了有关为LOCATION表创建外键的行,则代码可以正常工作。
答案 0 :(得分:0)
假设您已经将表定义为WAREHOUSE
,我认为问题是
WAREHOUSE(warehouseID)
和LOCATION(warehouseID)
最有可能的是,您已将WAREHOUSE表定义为(样本)
create table WAREHOUSE(warehouseID int not null primary key);
在您的LOCATION表中,warehouseID
属于varchar(3)
确保在两个表中引用列具有相同的数据类型
如果我稍微改变你的表结构,它可以正常工作
create table LOCATION (
warehouseID int,
fk_warehouseid int,
locationID varchar(3),
aisle int, shelf int,
bin int,
capacity double,
primary key(warehouseID,locationID),
foreign key(warehouseID) references WAREHOUSE(warehouseID) );
答案 1 :(得分:0)
LOCATION表有一个复合主键,因此对LOCATION表的外键引用应该包含主键中的两列。您只使用locationID定义。您是否可以使用locationID唯一标识位置表,然后更改主键。
否则,更改foreignkey引用也包括仓库ID,你会没事的。