Mysql 错误1215(HY000):无法添加外键约束,I';我检查了语法和拼写错误

Mysql 错误1215(HY000):无法添加外键约束,I';我检查了语法和拼写错误,mysql,sql,Mysql,Sql,我收到错误1215(HY000):无法添加外键约束错误: 外键(locationID)引用位置(locationID)。当我把它注释掉时,查询就顺利通过了 我似乎找不到我检查过的拼写错误问题,记事本++在两条语句上都突出显示了“Location”和“locationID”。它们也是varchar(3) 多谢各位 编辑: 完整代码 create database WareMart30119267; use WareMart30119267; create table Department (

我收到错误1215(HY000):无法添加外键约束错误: 外键(locationID)引用位置(locationID)。当我把它注释掉时,查询就顺利通过了

我似乎找不到我检查过的拼写错误问题,记事本++在两条语句上都突出显示了“Location”和“locationID”。它们也是
varchar(3)

多谢各位

编辑: 完整代码

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;

我现在觉得很愚蠢,答案是在主键中移动warehouseID和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;
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;

我不知道为什么订单会产生差异,但它修复了错误。

您在
位置上的主键定义为复合键:

Primary Key (warehouseID,locationID),
鉴于您正试图通过其一个组件在Prod_位置将其作为外键引用:

Foreign Key (locationID) references Location(locationID)
您需要将外键更改为与引用键的所有字段组合:

Foreign Key (warehouseID, locationID) references Location(warehouseID, locationID)
或者,将Location的主键更改为simple,例如just
locationID

在发布完整架构后编辑
很抱歉-复合密钥的顺序很重要-您需要保持外键引用中主键定义的相同顺序,即
(warehouseID,locationID)

拣选列表
中的
位置
的复合外键也需要这样做

此外,由于
clientNum
是返回给客户机的表
Client\u Address
中的外键,因此它本身不应声明为
Auto\u Increment
,因为它必须与
Client
保持同步,并在代码中显式分配

复合外键的一个常见问题是直接引用并连接到最终组件键表,而不是通过复合键连接到链接表。我认为,
Prod_Location
warehouseId
通过
外键(warehouseId)引用仓库(warehouseId)
之间的直接关系可能就是一个例子


从Pro_位置表中删除主键:

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;

我现在觉得很愚蠢,答案是在主键中移动warehouseID和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;
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;

我不知道为什么订单会有不同,但它修复了错误。

感谢您的帮助,我添加了您的外键,但它仍然会给我相同的错误,因此我尝试注释掉外键(warehouseID)引用仓库(warehouseID),但运气不佳,我无法更改主键。它必须是复合键,我们得到了一个ER图和我们必须遵循的数据库模式。我在原始帖子中搞错并交换了键的顺序。很抱歉。谢谢你的回答,很遗憾我们无法更改数据库的结构。我们必须使用讲师提供给我们的那个。我在讲座上讲了clientNum,他说它不应该是自动递增的,他在作业中犯了一个错误,谢谢。你在标题中有一个拼写错误,就在你说你检查了拼写错误的地方。你在使用什么RDBMS?请指定你所有的三个表结构。为什么在ID为时使用varchar(3)(warehouseID,locationID)?这就是我们必须使用的,我无法更改任何我已建立完整sql的数据类型。