MySQL、PHP 和 PDO 中的高效后代记录删除
从给定记录 ID(表递归地指向自身)中删除后代记录的策略是什么?具体来说,我正在使用 PDO、PHP 和 MySQL 5.0+。
想象一个包含以下列的类别表:
- 如果
- 。
- idparent_idcategory_name
ID 为 0,则它是根类别 请注意,该 id 不是主键——可以有很多根类别。
想象一下它有几层深,比如食物和庇护所根类别,然后是这些类别的子类别,还有这些类别的子类别,依此类推。这些是后代。例如,如果有人要删除蔬菜,那么您可以预期食物和庇护所将作为根类别保留下来,但胡萝卜将消失,豆类也会消失。豪宅和小屋也会被留下,因为它们来自另一棵树。得到它?
编辑:我的错——忘记了一列——parent_id。这一点非常关键。
What's the strategy for removing descendant records from a given record ID where the table points back to itself recursively? Specifically I'm using PDO, PHP, and MySQL 5.0+.
Imagine a categories table with these columns:
- id
- parent_id
- category_name
If the ID is 0, then it's a root category. That id is not a primary key, mind you -- there can be many root categories.
Imagine it's several layers deep, like Food and Shelter root categories, and then children of those, and children of those, and so on. These are the descendants. If someone were to, say, delete Vegetables, then you could expect that Food and Shelter would be left behind as root categories, but Carrots would be gone, as would Beans. Mansions and Cabins would also be left behind because they are from another tree. Get it?
EDIT: My bad -- forgot a column -- parent_id. This is pretty critical.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
然而,在您的场景中可能不可以选择,用于存储分层数据的嵌套集模型可以使您所描述的操作非常高效。
另外这篇文章可能有用:
http://mikehillyer.com/articles/managing -hierarchical-data-in-mysql/
Probably not on option in your scenario, however, the nested set model for storing hierarchical data can make operations like the one you described very efficient.
Also this article might be useful:
http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/
一个简单的级联引用完整性应该可以做到这一点 - 使用 ON DELETE CASCADE 声明您的外键。如果您对
parent_id
建立索引,它甚至应该相当高效(无论如何,这在 MySQL 中似乎是必需的;其他 DBMS 通常允许无索引 FK)。例如:
那么当你执行...
...不仅'Vegetables',而且'Carrots'和'Beans'都会被删除。
这甚至可以递归地工作,所以...
...删除第一级的“食物”,第二级的“蔬菜”,第三级的“胡萝卜”和“豆类”。
A simple cascading referential integrity should do it - declare your FOREIGN KEY with ON DELETE CASCADE. And if you index the
parent_id
, it should even be fairly efficient (this seems to be required in MySQL anyway; other DBMSes typically allow the index-less FK).For example:
Then when you execute...
...not only 'Vegetables', but also 'Carrots' and 'Beans' will be deleted.
This even works recursively, so...
...deletes 'Food' at the first level, 'Vegetables' at the second and 'Carrots' and 'Beans' at the third.
尽管嵌套集合模型更强大,但有时以下带有递归的示例就足够了。
Although the nested set model is more powerful, sometimes the following example with recursion can be good enough.