You can, of course, use your own foreign key links with MySQL (you do not need multiple tables). However, for any foreign key support you need to use the InnoDB engine . And I assume that you are using the MyISAM engine .
With InnoDB you can create a table similar to what you already have, including a foreign key for self-binding, for example:
CREATE TABLE `yourTable` ( `item_id` int(10) unsigned NOT NULL auto_increment, `parent_id` int(10) unsigned default NULL, `Name` varchar(50) NOT NULL, PRIMARY KEY (`item_id`), KEY `FK_parent_id` (`parent_id`), CONSTRAINT `FK_parent_id` FOREIGN KEY (`parent_id`) REFERENCES `yourTable` (`item_id`) ON DELETE CASCADE ON UPDATE CASCADE ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Then, when you issue a DELETE , for example:
DELETE FROM `yourTable` WHERE `item_id` = 1;
... it will remove every "child" row that has parent_id of 1 . If any of these "child" lines have their own children, they will also be deleted, etc. (Which means ON DELETE CASCADE ).
source share