有趣的树/分层数据结构问题 [英] Interesting tree/hierarchical data structure problem

查看:35
本文介绍了有趣的树/分层数据结构问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大学有不同的方式来组织他们的部门.一些学校去 School ->术语 ->部门.其他的则介于两者之间,最长的是 School ->Sub_Campus ->程序 ->术语 ->司 ->部门.

Colleges have different ways of organizing their departments. Some schools go School -> Term -> Department. Others have steps in between, with the longest being School -> Sub_Campus -> Program -> Term -> Division -> Department.

SchoolTermDepartment 是唯一始终存在于学校部门树"中的部门.这些类别的顺序永远不会改变,我给你的第二个例子是最长的.每一步都是 1:N 的关系.

School, Term, and Department are the only ones that always exist in a school's "tree" of departments. The order of these categories never changes, with the second example I gave you being the longest. Every step down is a 1:N relationship.

现在,我不确定如何设置表之间的关系.例如,Term 中有哪些列?它的父级可以是 ProgramSub_CampusSchool.哪个要看学校的制度.我可以设想将 Term 表设置为具有所有这些的外键(所有这些都将默认为 NULL),但我不确定这是规范的在这里做事的方式.

Now, I'm not sure how to set up the relationships between the tables. For example, what columns are in Term? Its parent could be a Program, Sub_Campus, or School. Which one it is depends on the school's system. I could conceive of setting up the Term table to have foreign keys for all of those (which all would default to NULL), but I'm not sure this is the canonical way of doing things here.

推荐答案

我建议你最好使用一个通用的表格,例如包含 id 字段和自引用 parent 字段的实体.

I suggest you better use a general table, called e.g. Entity which would contain id field and a self-referencing parent field.

每个相关表将包含一个指向实体 ID (1:1) 的字段.在某种程度上,每个表都是实体表的子表.

Each relevant table would contain a field pointing to Entity's id (1:1). In a way each table would be a child of the Entity table.

这篇关于有趣的树/分层数据结构问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆