Home > Database > Mysql Tutorial > 数据库存储层次、树形结构的标准做法

数据库存储层次、树形结构的标准做法

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-07 15:55:39
Original
1444 people have browsed it

标准做法有3种: 1、最早的做法:节点id里包括完整路径(曾祖父id-爷id-父id-本id) 财务的会计科目编号就是这么做的 现在基本很少使用了 2、递归做法:父id,本id(更早、完整的关系提供递归才能得到) 目前比较普遍 好处是直观简单,增删方便 坏处是生成树

标准做法有3种:
1、最早的做法:节点id里包括完整路径(曾祖父id-爷id-父id-本id)
财务的会计科目编号就是这么做的
现在基本很少使用了

2、递归做法:父id,本id(更早、完整的关系提供递归才能得到)
目前比较普遍
好处是直观简单,增删方便
坏处是生成树需要递归

3、直接保存法:本id,根id,层次数,在根(子树)里的序号
好像没看到别人这么用的,我在自己的树形论坛离线阅读器里采用过
好处是避免了递归,生成树记录高效方便
坏处是增删节点,需要更新半个子树的节点记录

 

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template