Connecting Tech Pros Worldwide Forums | Help | Site Map

Getting the FKs established between two or more tables

Newbie
 
Join Date: Sep 2007
Posts: 6
#1: Sep 21 '07
Hi all,
I would need to know the way to get the FKs between two or more tables.
Could you help me?

Thank you in advance,
Pumuky

amitpatel66's Avatar
Moderator
 
Join Date: Mar 2007
Location: Hyderabad, India
Posts: 2,192
#2: Sep 21 '07

re: Getting the FKs established between two or more tables


Quote:

Originally Posted by Pumuky

Hi all,
I would need to know the way to get the FKs between two or more tables.
Could you help me?

Thank you in advance,
Pumuky

In order to find the FOREIGN KEY CONSTRAINT between two tables, use below query:

Expand|Select|Wrap|Line Numbers
  1. SELECT acc1.table_name "PK TABLE",acc1.column_name "PK COLUMN", acc2.table_name "FK TABLE", acc2.column_name "FK COLUMN"
  2. FROM ALL_CONS_COLUMNS acc1,ALL_CONS_COLUMNS acc2,ALL_CONSTRAINTS ac
  3. WHERE  ac.constraint_name = acc2.constraint_name
  4. AND ac.r_constraint_name = acc1.constraint_name
  5. AND    acc1.table_name = 'DEPT'
  6. AND acc2.table_name = 'EMP'
  7.  
The above query gives you the PK-FK relation ship between EMP and DEPT table
Newbie
 
Join Date: Sep 2007
Posts: 6
#3: Sep 21 '07

re: Getting the FKs established between two or more tables


Thank you amitpatel66 but I think that I explained bad...

Your query return me all FK from one table and I need the FK name and the column name relationing two tables, I mean I have two tables and I need to know how they are relationated, obtaining their constraints and their column names.
Imagine that the query result is something like this
TABLE1, CONSTRAINT_NAME, COLUMN_NAME1, TABLE2, COLUMN_NAME2

indication that table1 has a FK named 'constraint_name1' for your colum 'column_name1' referencing the column 'column_name2' belonging to the 'table2'


I need this information to complete anothe query in your where condigition....
select * from T1, T2
where T1.Colum1=T2.colum2
and ......

Any idea?
Newbie
 
Join Date: Sep 2007
Posts: 6
#4: Sep 21 '07

re: Getting the FKs established between two or more tables


Excuse me by the previous email, I have read another query.
Your query work but only in one direction. I complete it with an union and now it look likes that it is working

SELECT acc1.table_name "PK TABLE",acc1.column_name "PK COLUMN", acc2.table_name "FK TABLE", acc2.column_name "FK COLUMN"
FROM ALL_CONS_COLUMNS acc1,ALL_CONS_COLUMNS acc2,ALL_CONSTRAINTS ac
WHERE ac.constraint_name = acc2.constraint_name
AND ac.r_constraint_name = acc1.constraint_name
AND acc1.table_name = 'DEPT'
AND acc2.table_name = 'EMP'
UNION
SELECT acc1.table_name "PK TABLE",acc1.column_name "PK COLUMN", acc2.table_name "FK TABLE", acc2.column_name "FK COLUMN"
FROM ALL_CONS_COLUMNS acc1,ALL_CONS_COLUMNS acc2,ALL_CONSTRAINTS ac
WHERE ac.constraint_name = acc2.constraint_name
AND ac.r_constraint_name = acc1.constraint_name
AND acc1.table_name = 'EMP'
AND acc2.table_name = 'DEPT'


Thanks so much!
amitpatel66's Avatar
Moderator
 
Join Date: Mar 2007
Location: Hyderabad, India
Posts: 2,192
#5: Sep 21 '07

re: Getting the FKs established between two or more tables


[quote=
Imagine that the query result is something like this
TABLE1, CONSTRAINT_NAME, COLUMN_NAME1, TABLE2, COLUMN_NAME2

indication that table1 has a FK named 'constraint_name1' for your colum 'column_name1' referencing the column 'column_name2' belonging to the 'table2'


I need this information to complete anothe query in your where condigition....
select * from T1, T2
where T1.Colum1=T2.colum2
and ......

Any idea?[/QUOTE]

Use Below Query as per your request:

Expand|Select|Wrap|Line Numbers
  1. SELECT acc2.table_name TABLE1,acc2.constraint_name CONSTRAINT_NAME,acc2.column_name COLUMN_NAME1,acc1.table_name TABLE2,acc1.column_name COLUMN_NAME2
  2. FROM ALL_CONS_COLUMNS acc1,ALL_CONS_COLUMNS acc2,ALL_CONSTRAINTS ac
  3. WHERE  ac.constraint_name = acc2.constraint_name
  4. AND ac.r_constraint_name = acc1.constraint_name
  5. AND    acc1.table_name = 'DEPT'
  6. AND acc2.table_name = 'EMP'
  7.  
Newbie
 
Join Date: Sep 2007
Posts: 6
#6: Sep 21 '07

re: Getting the FKs established between two or more tables


Apologies for the inconvienences but It does not work as expected.

This is the FK that I have defined in LOAD_ACTIONS table

Expand|Select|Wrap|Line Numbers
  1. CONSTRAINT FK_ACTIONS2LOADS FOREIGN KEY(ACTION_FILENAME,ACTION_CHK_SUM) 
  2. REFERENCES LOADS(FILENAME,CHK_SUM) ON DELETE CASCADE
And when I execute the following query...

Expand|Select|Wrap|Line Numbers
  1. SELECT acc1.table_name "PK TABLE",acc1.column_name "PK COLUMN", acc2.table_name "FK TABLE", cc2.column_name "FK COLUMN" 
  2. FROM ALL_CONS_COLUMNS acc1, ALL_CONS_COLUMNS acc2, ALL_CONSTRAINTS ac
  3. WHERE  ac.constraint_name = acc2.constraint_name
  4. AND ac.r_constraint_name = acc1.constraint_name
  5. and acc1.owner=acc2.owner
  6. and acc1.owner='my_schema'
  7. and ac.owner='my_schema'
  8. AND acc1.table_name = 'LOADS'
  9. AND acc2.table_name = 'LOAD_ACTIONS'
I got the follwoing answer...

Expand|Select|Wrap|Line Numbers
  1. LOADS    FILENAME    LOAD_ACTIONS    ACTION_FILENAME
  2. LOADS    CHK_SUM    LOAD_ACTIONS    ACTION_FILENAME
  3. LOADS    FILENAME    LOAD_ACTIONS    ACTION_CHK_SUM
  4. LOADS    CHK_SUM    LOAD_ACTIONS    ACTION_CHK_SUM
As you can see the result that I will expect to obtain will be only

Expand|Select|Wrap|Line Numbers
  1. LOADS    FILENAME    LOAD_ACTIONS    ACTION_FILENAME
  2. LOADS    CHK_SUM    LOAD_ACTIONS    ACTION_CHK_SUM
I am trying to get it without success.
Have you got any idea about how to get it.

Thanks so much in advance.
Pumuky
amitpatel66's Avatar
Moderator
 
Join Date: Mar 2007
Location: Hyderabad, India
Posts: 2,192
#7: Sep 22 '07

re: Getting the FKs established between two or more tables


Quote:

Originally Posted by Pumuky

Apologies for the inconvienences but It does not work as expected.

This is the FK that I have defined in LOAD_ACTIONS table

Expand|Select|Wrap|Line Numbers
  1. CONSTRAINT FK_ACTIONS2LOADS FOREIGN KEY(ACTION_FILENAME,ACTION_CHK_SUM) 
  2. REFERENCES LOADS(FILENAME,CHK_SUM) ON DELETE CASCADE
And when I execute the following query...

Expand|Select|Wrap|Line Numbers
  1. SELECT acc1.table_name "PK TABLE",acc1.column_name "PK COLUMN", acc2.table_name "FK TABLE", cc2.column_name "FK COLUMN" 
  2. FROM ALL_CONS_COLUMNS acc1, ALL_CONS_COLUMNS acc2, ALL_CONSTRAINTS ac
  3. WHERE  ac.constraint_name = acc2.constraint_name
  4. AND ac.r_constraint_name = acc1.constraint_name
  5. and acc1.owner=acc2.owner
  6. and acc1.owner='my_schema'
  7. and ac.owner='my_schema'
  8. AND acc1.table_name = 'LOADS'
  9. AND acc2.table_name = 'LOAD_ACTIONS'
I got the follwoing answer...

Expand|Select|Wrap|Line Numbers
  1. LOADS    FILENAME    LOAD_ACTIONS    ACTION_FILENAME
  2. LOADS    CHK_SUM    LOAD_ACTIONS    ACTION_FILENAME
  3. LOADS    FILENAME    LOAD_ACTIONS    ACTION_CHK_SUM
  4. LOADS    CHK_SUM    LOAD_ACTIONS    ACTION_CHK_SUM
As you can see the result that I will expect to obtain will be only

Expand|Select|Wrap|Line Numbers
  1. LOADS    FILENAME    LOAD_ACTIONS    ACTION_FILENAME
  2. LOADS    CHK_SUM    LOAD_ACTIONS    ACTION_CHK_SUM
I am trying to get it without success.
Have you got any idea about how to get it.

Thanks so much in advance.
Pumuky

Try below Query to get the desired result:

Expand|Select|Wrap|Line Numbers
  1. SELECT acc1.table_name TABLE1,acc1.constraint_name CONSTRAINT_NAME,acc1.column_name COLUMN_NAME1,acc2.table_name TABLE2,acc2.column_name COLUMN_NAME2
  2. FROM ALL_CONS_COLUMNS acc1,ALL_CONS_COLUMNS acc2,ALL_CONSTRAINTS ac
  3. WHERE  ac.constraint_name = acc2.constraint_name
  4. AND ac.r_constraint_name = acc1.constraint_name
  5. AND acc1.position = acc2.position
  6. AND acc1.table_name = 'LOADS'
  7. AND acc2.table_name = 'LOAD_ACTION'
  8.  
Newbie
 
Join Date: Sep 2007
Posts: 6
#8: Sep 24 '07

re: Getting the FKs established between two or more tables


Good man!! now it is working perfectly.

Thank you very much for your quick and precise support.
Pumuky
amitpatel66's Avatar
Moderator
 
Join Date: Mar 2007
Location: Hyderabad, India
Posts: 2,192
#9: Sep 24 '07

re: Getting the FKs established between two or more tables


Quote:

Originally Posted by Pumuky

Good man!! now it is working perfectly.

Thank you very much for your quick and precise support.
Pumuky

You are Welcome!!
Do POST in case if you have any oracle related issues in future!!
Thanks!!
Reply