添加外键约束失败

时间:2018-11-23 08:05:36

标签: mysql database foreign-keys

所以我正在创建数据库,这是我一直在上一张表上遇到的错误

  

错误1822(HY000):无法添加外键约束。引用表“ ITEMS”中约束“ invoice_ibfk_3”的索引缺失

我仍然不确定我到底要去哪里

这是我其余的SQL代码:

    mysql> create table CUSTOMER(
    -> Customer_ID int AUTO_INCREMENT, 
    -> Customer_FName varchar(20),
    -> Customer_LName varchar(20), 
    -> Address varchar(20), 
    -> Phone_No varchar(20), 
    -> primary key(Customer_ID));
Query OK, 0 rows affected (0.06 sec)

mysql> create table FLORIST(
    -> Florist_ID varchar(10), 
    -> FName varchar(20),
    -> LName varchar(20),
    -> Contact_No varchar(20),
    -> Username varchar(20),
    -> Password varchar(100),  
    -> primary key(Florist_ID));
Query OK, 0 rows affected (0.02 sec)

mysql> create table ITEMS(
    -> Item_ID int AUTO_INCREMENT, 
    -> Price int,
    -> Event varchar(20),
    -> Name varchar(20), 
    -> Stock int,
    -> primary key(Item_ID, Event));
Query OK, 0 rows affected (0.05 sec)

mysql> create table ORDERS(
    -> Order_ID int AUTO_INCREMENT, 
    -> Customer_ID int, 
    -> Florist_ID varchar(10), 
    -> Order_Date date, 
    -> Due_Date date, 
    -> primary key(Order_ID), 
    -> foreign key(Customer_ID) references CUSTOMER(Customer_ID) on delete cascade on update cascade, 
    -> foreign key(Florist_ID) references FLORIST(Florist_ID) on delete cascade on update cascade);

这是有问题的最后一张桌子:

mysql> create table INVOICE(
    -> Order_ID int, 
    -> Item_ID int, 
    -> Quantity int, 
    -> Price_Per_Item int, 
    -> Event varchar(20), 
    -> foreign key(Order_ID) references ORDERS(Order_ID) on delete cascade on update cascade, 
    -> foreign key(Item_ID) references ITEMS(Item_ID) on delete cascade on update cascade, 
    -> foreign key(Event) references ITEMS(Event) on delete cascade on update cascade);

1 个答案:

答案 0 :(得分:0)

在ITEMS.Item_ID上创建单独的索引(您目前在Item_ID和事件上具有复合索引)