trees and tables in DB2

泪湿孤枕 提交于 2020-01-16 18:16:12

问题


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. I can use these names for the columns of the table COMPONENT, SUBCOMPONENT, SUBCOMPCOUNT. Any ideas?

Components of type b occur in more than one place in the structure of component 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


回答1:


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. And a foreign key for parentComponent referencing the same table with the ID column.

Then, you can create a set of UDF and stored procedure to retrieve values, or to print the tree structure. For example, UDFs for

  • Retrieve the root node
  • Quantity of nodes in the tree
  • Quantity of sons of a given node
  • The Parent ID of a given node.

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.

What operations do you need to do in you graph?



来源:https://stackoverflow.com/questions/5344804/trees-and-tables-in-db2

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!