简体   繁体   English

DB2中的树和表

[英]trees and tables in DB2

I would like your help about this graph which I have.I would like to find a way how write a script to create a table named COMPS to represent this component/subcomponent tree structure, as an edge list for the graph. 我想为您提供有关此图的帮助。我想找到一种方法来编写脚本以创建一个名为COMPS的表来表示此组件/子组件树结构,作为该图的边列表。 I can use these names for the columns of the table COMPONENT, SUBCOMPONENT, SUBCOMPCOUNT. 我可以将这些名称用于表COMPONENT,SUBCOMPONENT,SUBCOMPCOUNT的列。 Any ideas? 有任何想法吗?

Components of type b occur in more than one place in the structure of component a. 类型b的组件出现在组件a的结构中的多个位置。 But the structure of component b is the same, regardless of its position Component d also occurs in various different locations, but all objects of type d are the same kind of object 但是组件b的结构相同,无论其位置如何组件d也会出现在各个不同的位置,但是类型d的所有对象都是同一种对象 在此处输入图片说明

You should create a table as you say in order to store the structue 您应该按说的那样创建表以存储结构

create table graph (
ID int not null,
COMPONENT char(2) not null,
ParentComponent int)

Create a primary key for ID column. 为ID列创建一个主键。 And a foreign key for parentComponent referencing the same table with the ID column. 并且parentComponent的外键使用ID列引用同一表。

Then, you can create a set of UDF and stored procedure to retrieve values, or to print the tree structure. 然后,您可以创建一组UDF和存储过程来检索值,或打印树结构。 For example, UDFs for 例如,UDF用于

  • Retrieve the root node 检索根节点
  • Quantity of nodes in the tree 树中的节点数量
  • Quantity of sons of a given node 给定节点的子节点数量
  • The Parent ID of a given node. 给定节点的父代ID。

And Stored procedures for 和存储程序为

  • Return a table with the rows ordered by a specific tree traversal (Breadth-first, Depth-first) 返回表与特定的树遍历命令行(广度优先,深度优先)

You can even create check constraints to provide rules to create the graph. 您甚至可以创建检查约束,以提供创建图形的规则。 For example, root node is 0, no one else can have this ID, and the is the lowest value. 例如,根节点为0,没有其他人可以有这样的ID,并且是最低值。

What operations do you need to do in you graph? 您需要在图形中执行哪些操作?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM