Achyut
Achyut

Reputation: 31

Key reference and table reference don't match

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

Upvotes: 0

Views: 5367

Answers (1)

Bhaskar
Bhaskar

Reputation: 1948

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
);

Upvotes: 4

Related Questions