mysql错误1215(hy000)无法添加外键约束

时间:2018-03-29 23:11:51

标签: mysql foreign-keys

我不断收到此错误,我将InnoDB用于所有表,menuID和customerID,是各自表中的主键,数据类型看起来是相同的。 ERROR 1215(HY000):无法添加外键约束

对不起,如果我遗漏了一些简单的东西,我是mySQL的新手。

   create table customers(
customerID int not null auto_increment primary key,
LastName varchar(255) not null,
FirstName varchar(255) not null,
email varchar(255) not null,
password varchar(255),
phone varchar(255),
creditCard varchar(255),
address varchar(255),
time timestamp)
Engine=InnoDB;

 create table menu(
 menuID int not null auto_increment primary key,
 typeID int,
 itemName varchar(255),
 price varchar(255)) 
Engine=InnoDB;

create table orders(
orderID int not null,
customerID int not null,
menuID int not null,
PRIMARY KEY (orderID, customerID, menuID),
FOREIGN KEY (customerID) REFERENCES customers(customerID) on delete set null on update cascade,
foreign key (menuID) references menu(menuID) on delete set null on update cascade )
Engine=InnoDB;

1 个答案:

答案 0 :(得分:1)

您不能将ON DELETE SET NULL用于您声明为NOT NULL的外键列。

请参阅此答案,查看可能导致外键错误原因的长清单:MySQL Creating tables with Foreign Keys giving errno: 150