I have a MySQL table like this:
CREATE TABLE categories (
ID INT NOT NULL,
Name VARCHAR(100) NULL,
Parent INT NULL,
PRIMARY KEY (ID)
) Engine=InnoDB
I’d like to ensure the deletion of all children whenever a parent gets deleted. At first, I wanted to do it by adding a foreign key like that to the table:
ALTER TABLE categories ADD CONSTRAINT FOREIGN KEY Parent(Parent)
REFERENCES categories(ID) ON DELETE CASCADE
This doesn’t work. I’ve also tried internal relations, but without success.
Parents and their children are linked with a recursive PHP function. Is there a way in MySQL to achieve the goal, or it should be done using PHP?
Works for me.