473,698 Members | 2,588 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Traversing Relationship Tree

I am attempting to write a query that will traverse the relationships
for tables in my database that will provide me the order in which I
need to perform delete operations to avoid running into foreign key
constraints. I have determined the delete order manually, but since the
database is complex and always having to structures added to it as the
scope of the application grows, I was looking for an automated
approach.

I tried modelling the query after some recursive examples from Graeme
Birchall's SQL Cookbook, but am missing something fundamental and the
query produces "infinite" output. I have been using SYSCAT.REFERENC ES
as my target table (view). The last incarnation of my query looks
something like this:

with temp1 (src_name, depends_on) as
((select root.reftabname , root.tabname
from syscat.referenc es root
where root.reftabname ='PX_CASE')
union all
( select child.reftabnam e, child.tabname
from temp1 parent, syscat.referenc es child
where parent.depends_ on = child.tabname)
)
select depends_on
from temp1
order by depends_on;

There are only 130 FK constraints on the collective tables, yet this
produces thousands of rows of output before I end up manually
terminating it. I have probably bitten off more than I can chew, but am
convinced that the solution exists to this problem. Can anyone make any
suggestions?

Thanks,
Mike

May 10 '06 #1
3 3317
mikelbell2000 wrote:
I am attempting to write a query that will traverse the relationships
for tables in my database that will provide me the order in which I
need to perform delete operations to avoid running into foreign key
constraints. I have determined the delete order manually, but since the
database is complex and always having to structures added to it as the
scope of the application grows, I was looking for an automated
approach.

I tried modelling the query after some recursive examples from Graeme
Birchall's SQL Cookbook, but am missing something fundamental and the
query produces "infinite" output. I have been using SYSCAT.REFERENC ES
as my target table (view). The last incarnation of my query looks
something like this:

with temp1 (src_name, depends_on) as
((select root.reftabname , root.tabname
from syscat.referenc es root
where root.reftabname ='PX_CASE')
union all
( select child.reftabnam e, child.tabname
from temp1 parent, syscat.referenc es child
where parent.depends_ on = child.tabname)
)


Somehow this doesn't make a lot of sense to me. src_name is the name of a
referenced table A and "depends_on " contains the name of a referencing
table B (according to the start of the recursion). The part after the
UNION ALL should now try to find all tables that reference B, right? But
the sub-select there finds table B itself again. I would have expected
something like this:

WITH temp1 (src_name, depends_on) AS
( SELECT root.reftabname , root.tabname
FROM syscat.referenc es root
WHERE root.reftabname ='PX_CASE'
UNION ALL
SELECT child.reftabnam e, child.tabname
FROM temp1 parent, syscat.referenc es child
WHERE parent.depends_ on = child.reftabnam e )
SELECT depends_on
FROM temp1
ORDER BY depends_on

There are some other issues I see:
(1) You have table 'PX_CASE' as start of the recursion. You'll only find
tables that refer directly or indirectly to that one. If there are other
"start tables", you may miss things.
(2) You should add an explicit termination criteria (see examples in the SQL
Reference) to prevent endless recursions due to circular dependencies or
self-referencing tables.
(3) I would probably start at the leafs of the dependency-tree/graph, i.e.
with the tables that are not referenced by any other.

--
Knut Stolze
DB2 Information Integration Development
IBM Germany
May 10 '06 #2
Thanks, Knut. This works much better. I usually shun recursion because
it's one of those things that makes my brain hurt when I think about it
too deeply :-)

The query I posted was probably the 10th incarnation of my attempt to
do this.

The first issue you raised is dead on. This is exactly what I want in
my particular case. I am actually looking to for all tables that are
descendants of one particular master table. My exact scenario is for
every record in PX_CASE that is older than a certain date, I want to
move it and all its dependents out of the current database into an
"archive" database. I build a list of all cases then use the table list
(which would be generated the recursive query) to export all related
data.

After loading data into archive database I need to delete from the
primary database. I need to delete from the bottom up.I will take your
version of the query and try to tweak it to include depth information
to be give me the delete order.

Cheers,
Mike

May 10 '06 #3
mikelbell2000 wrote:
Thanks, Knut. This works much better. I usually shun recursion because
it's one of those things that makes my brain hurt when I think about it
too deeply :-)

The query I posted was probably the 10th incarnation of my attempt to
do this.

The first issue you raised is dead on. This is exactly what I want in
my particular case. I am actually looking to for all tables that are
descendants of one particular master table. My exact scenario is for
every record in PX_CASE that is older than a certain date, I want to
move it and all its dependents out of the current database into an
"archive" database. I build a list of all cases then use the table list
(which would be generated the recursive query) to export all related
data.

After loading data into archive database I need to delete from the
primary database. I need to delete from the bottom up.I will take your
version of the query and try to tweak it to include depth information
to be give me the delete order.


Great, archiving. I did something along those lines in my master's
thesis. ;-)

--
Knut Stolze
DB2 Information Integration Development
IBM Germany
May 12 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2742
by: Ray | last post by:
I have a real puzzle, I figured someone on here could help. I have a table that tracks all parents and children. I would like to set iterate over all entities where I set a variable to equal a root parent and return all relations disregarding why the relation exists. Essentially here is what I would like to do however, this won't work for obvious reasons: select parent, (select child from table start with parent = (select parent from...
28
2205
by: Jeff Lanfield | last post by:
Suppose I have users that can belong to organizations. Organizations are arranged in a tree. Each organization has only one parent organization but a user maybe a member of multiple organizations. The problem that I'm facing that both organizations and individual users may have relationships with other entities which are semantically the same. For instance, an individual user can purchase things and so can an organization. An individual...
2
7215
by: My Internet | last post by:
Hello, I want to know if in PostgreSQL, there is a command equivalent to START WITH... CONNECT BY from Orcale? This command is used to traverse a tree. Thank you, Jean-Marc _________________________________________________________________
1
1436
by: alfred | last post by:
Hi my question is on traversing a tree with DOM. how would I be able to traverse 2 trees at the same time. I have 2 XML documents, with similar nodes. I would like to traverse 1 xml document, if I found a node that matches another node in the other document, I would like to update attributes in the other document. I know you can recursive traverse a document, my problem is to traverse 2 document at the same time, thanks
4
4872
by: plmanikandan | last post by:
Hi, I am new to link list programming.I need to traverse from the end of link list.Is there any way to find the end of link list without traversing from start(i.e traversing from first to find the next for null).Is there any way to find the length of linked list in c.My need is to traverse from the end to 5th node Regards, Mani
4
5840
by: A_StClaire_ | last post by:
hi all, I'm trying to implement Huffman coding on letters of a string. I've written functions to define the initial leaf nodes (letters and their frequencies) and to sort them. I've also put together the actual tree with its appropriate nodes and child nodes. however I'm having some trouble developing a means of traversing the tree itself to create the binary key table. i.e., how would I know how many levels the tree has? this...
4
5534
by: Christian Rühl | last post by:
Good Day, folks! I'm having a problem traversing an XmlDocument tree in C#. I only want to access the InnerText of the leafs and the names of their ancestors and show them in a richTextBox. But if I go like this I get the InnerText of all the nodes and the outcome in my richTextBox is just a mess. The XML files i want to handle here have about 2300 nodes, so that accessing some nodes directly is just impossible.
1
2217
by: flavourofbru | last post by:
Hello, I am trying to traverse and tree and want to assign the node id for each node in the tree. It would be great if someone helps me with it. Here is the code which i am using and the problem is the nodes in the tree are repeating everytime and i want to visit each node only once and assign the node id to it. protected void walk(TreeModel model, Object o){ int cc;
1
3437
by: dhettlinger | last post by:
I'm attempting to write some SQL code in Oracle 10g to pull data from a tree structure. Basically, I have a users table, and each user is assigned to a manager. (The user's manager's ID is kept in the manager field of the user's entry in the users table.) From this tree structure, I want to pull a list of all users who report either directly or indirectly to a particular division head (top level manager). I imagine that this query would...
0
8609
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9169
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8899
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8871
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7738
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5861
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4371
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2335
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.