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
8 2034
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: -
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'
-
The above query gives you the PK-FK relation ship between EMP and DEPT table
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?
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!
[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: -
SELECT acc2.table_name TABLE1,acc2.constraint_name CONSTRAINT_NAME,acc2.column_name COLUMN_NAME1,acc1.table_name TABLE2,acc1.column_name COLUMN_NAME2
-
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'
-
Apologies for the inconvienences but It does not work as expected.
This is the FK that I have defined in LOAD_ACTIONS table -
CONSTRAINT FK_ACTIONS2LOADS FOREIGN KEY(ACTION_FILENAME,ACTION_CHK_SUM)
-
REFERENCES LOADS(FILENAME,CHK_SUM) ON DELETE CASCADE
And when I execute the following query... -
SELECT acc1.table_name "PK TABLE",acc1.column_name "PK COLUMN", acc2.table_name "FK TABLE", cc2.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.owner=acc2.owner
-
and acc1.owner='my_schema'
-
and ac.owner='my_schema'
-
AND acc1.table_name = 'LOADS'
-
AND acc2.table_name = 'LOAD_ACTIONS'
I got the follwoing answer... -
LOADS FILENAME LOAD_ACTIONS ACTION_FILENAME
-
LOADS CHK_SUM LOAD_ACTIONS ACTION_FILENAME
-
LOADS FILENAME LOAD_ACTIONS ACTION_CHK_SUM
-
LOADS CHK_SUM LOAD_ACTIONS ACTION_CHK_SUM
As you can see the result that I will expect to obtain will be only -
LOADS FILENAME LOAD_ACTIONS ACTION_FILENAME
-
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
Apologies for the inconvienences but It does not work as expected.
This is the FK that I have defined in LOAD_ACTIONS table -
CONSTRAINT FK_ACTIONS2LOADS FOREIGN KEY(ACTION_FILENAME,ACTION_CHK_SUM)
-
REFERENCES LOADS(FILENAME,CHK_SUM) ON DELETE CASCADE
And when I execute the following query... -
SELECT acc1.table_name "PK TABLE",acc1.column_name "PK COLUMN", acc2.table_name "FK TABLE", cc2.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.owner=acc2.owner
-
and acc1.owner='my_schema'
-
and ac.owner='my_schema'
-
AND acc1.table_name = 'LOADS'
-
AND acc2.table_name = 'LOAD_ACTIONS'
I got the follwoing answer... -
LOADS FILENAME LOAD_ACTIONS ACTION_FILENAME
-
LOADS CHK_SUM LOAD_ACTIONS ACTION_FILENAME
-
LOADS FILENAME LOAD_ACTIONS ACTION_CHK_SUM
-
LOADS CHK_SUM LOAD_ACTIONS ACTION_CHK_SUM
As you can see the result that I will expect to obtain will be only -
LOADS FILENAME LOAD_ACTIONS ACTION_FILENAME
-
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: -
SELECT acc1.table_name TABLE1,acc1.constraint_name CONSTRAINT_NAME,acc1.column_name COLUMN_NAME1,acc2.table_name TABLE2,acc2.column_name COLUMN_NAME2
-
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.position = acc2.position
-
AND acc1.table_name = 'LOADS'
-
AND acc2.table_name = 'LOAD_ACTION'
-
Good man!! now it is working perfectly.
Thank you very much for your quick and precise support.
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!!
Sign in to post your reply or Sign up for a free account.
Similar topics
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....
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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)...
|
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...
|
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...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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...
|
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,...
|
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,...
| |