Unix Technical Forum

Parent Child Relationship - System catalog tables

This is a discussion on Parent Child Relationship - System catalog tables within the DB2 forums, part of the Database Server Software category; --> Is it possible to determine the list of child/parent tables for a particular table from any system catalog tables? ...


Go Back   Unix Technical Forum > Database Server Software > DB2

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-27-2008, 08:11 AM
reachsamdurai@gmail.com
 
Posts: n/a
Default Parent Child Relationship - System catalog tables

Is it possible to determine the list of child/parent tables for a
particular table from any system catalog tables? Using the
syscat.tables I'm able to retrieve the no of dependent parent/child
tables but unable to determine the list of dependent table name(s)

Example :
For a simple department-employee relationship, the following query
gives the count of parent/child tables but not the table names(s).

db2 => select tabname, parents,children from syscat.tables where
tabname = 'DEPA
RTMENT' or TABNAME = 'EMPLOYEE'

TABNAME PARENTS CHILDREN
----------------------------------------------------
DEPARTMENT 0 1
EMPLOYEE 1 0

2 record(s) selected.


Thanks,
Sam.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-27-2008, 08:11 AM
Serge Rielau
 
Posts: n/a
Default Re: Parent Child Relationship - System catalog tables

syscat.references
TABSCHEMA/TABNAME shows children
REFTABSCHEMA/REFTABNAME shows parents


--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-27-2008, 08:11 AM
Mark A
 
Posts: n/a
Default Re: Parent Child Relationship - System catalog tables

<reachsamdurai@gmail.com> wrote in message
news:1148907579.501908.282360@38g2000cwa.googlegro ups.com...
> Is it possible to determine the list of child/parent tables for a
> particular table from any system catalog tables? Using the
> syscat.tables I'm able to retrieve the no of dependent parent/child
> tables but unable to determine the list of dependent table name(s)
>
> Example :
> For a simple department-employee relationship, the following query
> gives the count of parent/child tables but not the table names(s).
>
> db2 => select tabname, parents,children from syscat.tables where
> tabname = 'DEPA
> RTMENT' or TABNAME = 'EMPLOYEE'
>
> TABNAME PARENTS CHILDREN
> ----------------------------------------------------
> DEPARTMENT 0 1
> EMPLOYEE 1 0
>
> 2 record(s) selected.
>
>
> Thanks,
> Sam.
>


It is in there somewhere. Try looking at the constraints.


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-27-2008, 08:12 AM
esmith2112@gmail.com
 
Posts: n/a
Default Re: Parent Child Relationship - System catalog tables

reachsamdurai@gmail.com wrote:
> Is it possible to determine the list of child/parent tables for a
> particular table from any system catalog tables? Using the
> syscat.tables I'm able to retrieve the no of dependent parent/child
> tables but unable to determine the list of dependent table name(s)


This all depends on depends on the declared foreign key constraints. If
you have defined the relationships, then this is possible. Serge
pointed out that these can be found in the SYSCAT.REFERENCES table. In
order to traverse the tree structure, a recursive query can be used.
Assuming you have the same schema name for all of the tables, the
following query should give you a list of parent/child relationships
including how many levels deep the relationship between the top level
table and any subservient table:


WITH parentchild
(
parentname,
childname,
depth
) AS
(SELECT reftabname,
tabname,
1
FROM syscat.references
WHERE reftabname= 'DEPARTMENT'
UNION ALL
SELECT child.reftabname,
child.tabname,
depth+1
FROM parentchild parent,
syscat.references child
WHERE parent.childname = child.reftabname
)
SELECT DISTINCT parentname,
childname,
depth
FROM parentchild
ORDER BY depth


Hope this helps,
Evan

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 07:36 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com