dousi5358
2009-09-11 18:56
浏览 117
已采纳

如何在MySQL中实现级联数据的级联删除?

I'm working on a project that has categories/subcategories. The database table for this is unique, with the following structure:

CREATE TABLE IF NOT EXISTS `categories` (
  `id` int(11) NOT NULL auto_increment,
  `publish` tinyint(1) NOT NULL default '0',
  `parent_id` int(11) NOT NULL default '0',
  `description` text NOT NULL,
  `name` varchar(255) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

So, in case the category is a "base" one, the parent_id is zero, and if the category has a parent, it herds the parent id. What I want to know is this: I need to delete everything above and related with a category when choosing that option, a cascade-like deletion, but I only have this table (no foreign keys). How do I do that? (Without a large amount of queries.)

图片转代码服务由CSDN问答提供 功能建议

我正在开发一个具有类别/子类别的项目。 这个数据库表是唯一的,具有以下结构:</ p>

  CREATE TABLE IF NOT NOT EXISTS`category`(
`id`int(11)NOT NULL auto_increment,\  n`public` tinyint(1)NOT NULL默认'0',
`parent_id`int(11)NOT NULL默认'0',
`description`文本NOT NULL,
`name` varchar(255)NOT  NULL,
 PRIMARY KEY(`id`)
)ENGINE = MyISAM DEFAULT CHARSET = latin1 AUTO_INCREMENT = 1; 
 </ code> </ pre> 
 
 

因此,如果类别是 “base”为1,parent_id为零,如果类别为父类,则为父级id。 我想知道的是:在选择该选项时,我需要删除上面的所有内容并与类别相关,类似级联删除,但我只有这个表(没有外键)。 我怎么做? (没有大量查询。)</ p> </ div>

2条回答 默认 最新

相关推荐 更多相似问题