title: PHP, MySQL, categories, and subcategories – the deletion
body: <p>I have a MySQL table like this:</p>
  
  <pre><code>CREATE TABLE categories (
      ID INT NOT NULL,
      Name VARCHAR(100) NULL,
      Parent INT NULL,
      PRIMARY KEY (ID)
  ) Engine=InnoDB
  </code></pre>
  
  <p>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:</p>
  
  <pre><code>ALTER TABLE categories ADD CONSTRAINT FOREIGN KEY Parent(Parent) 
  REFERENCES categories(ID) ON DELETE CASCADE
  </code></pre>
  
  <p>This doesn't work. I've also tried internal relations, but without success.</p>
  
  <p>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?</p>