键引用和表引用不匹配

时间:2022-09-20 13:54:12

I am trying to create a foreign key, but I am getting this error. I don't understand why. The query is:

我正在尝试创建一个外键,但我收到此错误。我不明白为什么。查询是:

click here to see the screenshot of the query

单击此处查看查询的屏幕截图

1 个解决方案

#1


0  

It is because you have given a wrong reference. You need to reference the primary key of another table, not just the table. See this example-

这是因为你提供了错误的参考。您需要引用另一个表的主键,而不仅仅是表。看这个例子 -

CREATE TABLE parent (id INT NOT NULL,
PRIMARY KEY (id)
);

CREATE TABLE child (
id INT, 
parent_id INT,
PRIMARY KEY  (`id`),
FOREIGN KEY (parent_id) REFERENCES parent(id)
ON DELETE CASCADE
);

#1


0  

It is because you have given a wrong reference. You need to reference the primary key of another table, not just the table. See this example-

这是因为你提供了错误的参考。您需要引用另一个表的主键,而不仅仅是表。看这个例子 -

CREATE TABLE parent (id INT NOT NULL,
PRIMARY KEY (id)
);

CREATE TABLE child (
id INT, 
parent_id INT,
PRIMARY KEY  (`id`),
FOREIGN KEY (parent_id) REFERENCES parent(id)
ON DELETE CASCADE
);