If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > Informix > How can I get all tables that have FK on a PK

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-29-05, 04:43
Gaston de Foix Gaston de Foix is offline
Registered User
 
Join Date: Sep 2005
Posts: 3
How can I get all tables that have FK on a PK?

Hi

I'm new in Informix. I have a big problem.

I have table "person" with a Primary key "pk_idPers" on a field "idPers":
------------
| idPers |
------------
| name |
------------
| pname |
------------

I want all tables(and coresponding fields for foreign key) that have foreign keys on idPers?

Example: Let's say that I have 30 tables that have foreign keys to table "person". I have in table "person" a record with idPers=1.
I want to get all tables from those 30 that have in field (that is in foreign key) the value 1.

My job is when I delete a record from a table to get all tables that have foreign keys to Primary key of my table.

All I achived is to get all tables that have foreign keys but I can't get the fields on FK. If I can get the fields in a stored procedure I want execute at runtime a statement that will retrive exactly the tables that have the value 1 for field that is in FK.

select SYSTABLES.tabname, con2.constrname from SYSREFERENCES,
sysconstraints con1, sysconstraints con2, SYSTABLES where
con1.constrname='pk_idPers' and SYSREFERENCES.primary=con1.constrid and
SYSREFERENCES.constrid=con2.constrid and con2.tabid=SYSTABLES.tabid;

I hope you understand, and somebody can help me. Thanks in advance...

Last edited by Gaston de Foix; 09-29-05 at 04:54.
Reply With Quote
  #2 (permalink)  
Old 09-29-05, 11:18
blackguard blackguard is offline
Registered User
 
Join Date: Sep 2002
Posts: 102
select e.tabname
from systables a,
sysconstraints b,
sysreferences c,
sysconstraints d,
systables e
where a.tabname='person' and
a.tabid=b.tabid and b.constrtype='P' and
b.constrid=c.primary and
b.tabid=c.ptabid and
c.constrid=d.constrid and
d.tabid=e.tabid
;


Let me know if this is what you're looking for.
Reply With Quote
  #3 (permalink)  
Old 09-30-05, 05:15
Gaston de Foix Gaston de Foix is offline
Registered User
 
Join Date: Sep 2005
Posts: 3
Tks for response.

Yes and No. I want columns too. Your select display only tables
Reply With Quote
  #4 (permalink)  
Old 09-30-05, 08:40
blackguard blackguard is offline
Registered User
 
Join Date: Sep 2002
Posts: 102
I can give you want you want but you got to tell me how many columns are in the primary key.
Reply With Quote
  #5 (permalink)  
Old 09-30-05, 09:14
blackguard blackguard is offline
Registered User
 
Join Date: Sep 2002
Posts: 102
Here you go, assuming only 1 column key:

select e.tabname,g1.colname
from systables a,
sysconstraints b,
sysreferences c,
sysconstraints d,
systables e,
sysindexes f,
syscolumns g1
where a.tabname='person' and
a.tabid=b.tabid and b.constrtype='P' and
b.constrid=c.primary and
b.tabid=c.ptabid and
c.constrid=d.constrid and
d.tabid=e.tabid and
e.tabid=f.tabid and
f.idxname=d.idxname and
f.tabid=g1.tabid and abs(f.part1)=g1.colno
;
Reply With Quote
  #6 (permalink)  
Old 09-30-05, 10:45
Gaston de Foix Gaston de Foix is offline
Registered User
 
Join Date: Sep 2005
Posts: 3
Thanks a lot man. It's working

You are the best.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On