Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3665388
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T01:45:19+00:00 2026-05-19T01:45:19+00:00

I have a MySQL table like this: CREATE TABLE categories ( ID INT NOT

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T01:45:20+00:00Added an answer on May 19, 2026 at 1:45 am

    Works for me.

    #Server version: 5.1.42-community MySQL Community Server (GPL)
    create table lists(
       id int not null
      ,parent int
      ,primary key(id)
      ,foreign key(parent) references lists(id) on delete cascade
    ) Engine=InnoDb;
    
    insert into lists(id, parent) values(1, null);
    insert into lists(id, parent) values(2, 1);
    insert into lists(id, parent) values(3, 2);
    insert into lists(id, parent) values(4, 3);
    
    mysql> select * from lists;
    +----+--------+
    | id | parent |
    +----+--------+
    |  1 |   NULL |
    |  2 |      1 |
    |  3 |      2 |
    |  4 |      3 |
    +----+--------+
    4 rows in set (0.00 sec)
    
    mysql>
    mysql> delete from lists where id = 1;
    Query OK, 1 row affected (0.02 sec)
    
    mysql>
    mysql> select * from lists;
    Empty set (0.00 sec)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple MySQL table like this: create table `users`( `id` int(5) NOT
I have a table defined like this: CREATE TABLE mytable (id INT NOT NULL
I have mysql database structure like below: CREATE TABLE test ( id int(11) NOT
I have a mysql table like this (sql): CREATE TABLE IF NOT EXISTS silver_and_pgm
I have a MySQL table like this CREATE TABLE IF NOT EXISTS `vals` (
I have a MySQL table: CREATE TABLE documents ( id INT NOT NULL AUTO_INCREMENT,
Suppose I have a table (MySQL) like this: CREATE TABLE sessions ( session_id INT
I have a table like this: create table `test` ( `id` bigint(20) NOT NULL
I have a table in mysql like this Name Result T1_09_03_2010 fail T2_09_03_2010 pass
I have a table defined like this: create table users ( id int(10), age

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.