473,396 Members | 1,918 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Getting the FKs established between two or more tables

6
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
Sep 21 '07 #1
8 2034
amitpatel66
2,367 Expert 2GB
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
Sep 21 '07 #2
Pumuky
6
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?
Sep 21 '07 #3
Pumuky
6
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!
Sep 21 '07 #4
amitpatel66
2,367 Expert 2GB
[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.  
Sep 21 '07 #5
Pumuky
6
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
Sep 21 '07 #6
amitpatel66
2,367 Expert 2GB
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.  
Sep 22 '07 #7
Pumuky
6
Good man!! now it is working perfectly.

Thank you very much for your quick and precise support.
Pumuky
Sep 24 '07 #8
amitpatel66
2,367 Expert 2GB
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!!
Sep 24 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

303
by: mike420 | last post by:
In the context of LATEX, some Pythonista asked what the big successes of Lisp were. I think there were at least three *big* successes. a. orbitz.com web site uses Lisp for algorithms, etc. b....
5
by: Brandons of mass destruction | last post by:
Ok, I work for a small newspaper thats looking to translate our content to the web. XML sounds like a good way to do this, but i'm a little confused. Is it possible to have <b> and <i> tags in...
2
by: sjoshi | last post by:
I'm trying this simple query but getting multiple listings for change_number field SELECT c.Change_Number, c.Submission_Date, c.Short_Description, c.CurrentStatus, dlv.Name, s.Description,...
2
by: Larry R Harrison Jr | last post by:
I have an Access 97 database with 2 tables that have a one-many relationship. I have a SQL statement in the "one" table which I want to execute and insert 7 records into the "many" table, and I...
2
by: Joe | last post by:
Hi All, I am new to using the Access DB and I need some help if someone is able to give it to me. What I want to do is get the names of the columns of certain tables. Not the data in the table...
4
by: What-a-Tool | last post by:
I am trying to write a program that will take all the members of a data base, add them to a tree, with all child relations as sub-nodes. I am having a problem getting the parent child relations...
41
by: pb648174 | last post by:
In a multi-user environment, I would like to get a list of Ids generated, similar to: declare @LastId int select @LastId = Max(Id) From TableMania INSERT INTO TableMania (ColumnA, ColumnB)...
10
by: Paul H | last post by:
I am trying to get the spec for a database. The trouble is the client frequently blurts out industry jargon, speaks insanely quickly and is easily sidetracked. They are currently using around 30...
9
by: Debbie | last post by:
I wonder if anyone can help me out, or point me in the right direction, in solving my current problem: I have started seeing an error on one of my ASP pages. Id displays totally blank except...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
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,...

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.