我收到ERROR 1215(HY000):无法添加外键约束错误: 外键(locationID)引用位置(locationID)。当我发表评论时,查询没有问题
我似乎无法找到我检查拼写错误的问题,notepad ++在两个语句中都突出显示“Location”和“locationID”。他们也是varchar(3)
。
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)
) Engine = InnoDB;
create table Prod_Location
(
warehouseID varchar(3),
locationID varchar(3),
productNum int,
quantityOnHand int,
Foreign Key (warehouseID) references Warehouse(warehouseID),
Foreign Key (productNum) references Product(productNum),
Foreign Key (locationID) references Location(locationID),
Primary Key (warehouseID, locationID, productNum)
) Engine = InnoDB;
谢谢。
编辑: 完整代码
create database WareMart30119267;
use WareMart30119267;
create table Department (
dptNumber int Auto_Increment,
dptName varchar(20),
Primary Key (dptNumber))
Engine = InnoDB;
create table Product (
productNum int Auto_Increment,
description varchar(30),
packSize int,
Price Decimal(10,2),
dptNumber int,
Primary Key (productNum),
Foreign Key (dptNumber) references Department(dptNumber))
Engine = InnoDB;
create table CLient (
clientNum int Auto_Increment,
clientName varchar(40),
Primary Key (clientNum))
Engine = InnoDB;
create table Client_Address (
clientNum int Auto_Increment,
addressType varchar(1),
street varchar(20),
city varchar(3),
state varchar(3),
postcode varchar(4),
Primary Key (clientNum, addressType),
Foreign Key (clientNum) references Client(clientNum))
Engine = InnoDB;
create table Stock_Request (
requestNum int Auto_Increment,
requestDate date,
clientNum int,
Primary Key (requestNum),
Foreign Key (clientNum) references Client(clientNum))
Engine = InnoDB;
create table Request_List (
requestNum int,
productNum int,
qtyRequested int,
Primary Key (requestNum, productNum),
Foreign Key (requestNum) references Stock_Request(requestNum),
Foreign Key (productNum) references Product(productNum))
Engine = InnoDB;
create table Warehouse (
warehouseID varchar(3),
street varchar(20),
city varchar(15),
state varchar(3),
postcode varchar(4),
managerID int,
Primary Key (warehouseID))
Engine = InnoDB;
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))
Engine = InnoDB;
create table Employee (
staffID int Auto_Increment,
surname varchar(20),
firstName varchar(15),
dob date,
street varchar(20),
city varchar(15),
state varchar(3),
postcode varchar(4),
salary Decimal(19,4),
warehouseID varchar(3),
supervisedBy int,
Primary Key (staffID),
Foreign Key (supervisedBy) references Employee(staffID))
Engine = InnoDB;
/*Add Foreign Keys to Warehouse and Employee */
alter table Warehouse
add Foreign Key (managerID) references Employee(staffID);
alter table Employee
add Foreign Key (warehouseID) references Warehouse(warehouseID);
create table Prod_Location (
warehouseID varchar(3),
locationID varchar(3),
productNum int,
quantityOnHand int,
Foreign Key (warehouseID) references Warehouse(warehouseID),
Foreign Key (productNum) references Product(productNum),
Foreign Key (locationID) references Location(locationID),
Primary Key (warehouseID,locationID,productNum))
Engine = InnoDB;
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, warehouseID) references Location(locationID, warehouseID),
Foreign Key (productNum) references Product(productNum),
Foreign Key (requestNum) references Stock_Request(requestNum))
Engine = InnoDB;
我现在觉得愚蠢,答案是在主键中移动仓库ID和locationID所以它就像这样
create table Location (
warehouseID varchar(3),
locationID varchar(3),
Aisle int,
Shelf int,
Bin int,
capacity Double,
Primary Key (locationID, warehouseID),
Foreign Key (warehouseID) references Warehouse(warehouseID))
Engine = InnoDB;
我不知道为什么订单有所不同,但它修正了错误。
答案 0 :(得分:0)
location
上的主键被定义为复合键:
Primary Key (warehouseID,locationID),
虽然您试图通过其中一个组件将Prod_Location中的此引用作为外键:
Foreign Key (locationID) references Location(locationID)
您需要将外键更改为与引用键的所有字段合成:
Foreign Key (warehouseID, locationID) references Location(warehouseID, locationID)
或者,或者,将位置的主键更改为简单,例如,只是locationID
修改完整架构发布后
我很抱歉 - 复合键的顺序很重要 - 您需要在外键引用中保持主键定义的相同顺序,即(warehouseID, locationID)
。
还需要对Location
中的Picking_List
的复合外键执行此操作。
此外,由于clientNum
是表Client_Address
中的外键返回客户端,因此它本身不应声明为Auto_Increment
,因为它必须与{{1}保持同步并在代码中明确分配。
复合外键的一个常见问题是直接引用和连接到终极组件键表的诱惑,而不是通过复合键引用链接表。我认为Client
和Prod_Location
之间通过warehouseId
的直接关系可能就是一个例子。
答案 1 :(得分:0)
从Pro_Location表中删除主键:
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))
Engine = InnoDB;
create table Prod_Location (
warehouseID varchar(3),
locationID varchar(3),
productNum int,
quantityOnHand int,
Foreign Key (warehouseID) references Warehouse (warehouseID),
Foreign Key (productNum) references Product (productNum),
Foreign Key (locationID) references Location (locationID)
Engine = InnoDB;
答案 2 :(得分:0)
我现在觉得愚蠢,答案是在主键中移动仓库ID和locationID所以它就像这样
create table Location (
warehouseID varchar(3),
locationID varchar(3),
Aisle int,
Shelf int,
Bin int,
capacity Double,
Primary Key (locationID, warehouseID),
Foreign Key (warehouseID) references Warehouse(warehouseID))
Engine = InnoDB;
我不知道为什么订单有所不同但它修正了错误。