我在做什么不正确?试图创造sqlfiddle 这些表不起作用给人如何在mysql中添加外键到表中
Cannot add foreign key constraint
create table product (
pid int NOT NULL,
name varchar(10),
PRIMARY KEY (pid)
);
create table trans (
tid int NOT NULL ,
productId int NOT NULL,
userId int NOT NULL,
PRIMARY KEY (tid),
FOREIGN KEY (productId) REFERENCES product(pid),
FOREIGN KEY (userId) REFERENCES user1(uid)
);
create table user1 (
uid int NOT NULL ,
location varchar(22),
PRIMARY KEY (uid)
);
您需要创建'product'和'user1'表,然后才能通过外键引用它们。将'trans'表移动到最后一个位置。 –