我一直收到此错误,我对所有表,菜单ID和客户ID使用InnoDB,它们是各自表中的主键,并且数据类型似乎相同。
错误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;
最佳答案
声明ON DELETE SET NULL
的外键列不能使用NOT NULL
。
请参阅此答案以获取一长串检查清单,以检查可能导致外键错误的原因:MySQL Creating tables with Foreign Keys giving errno: 150
关于mysql - mysql错误1215(hy000)无法添加外键约束,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49566330/