gamesmeister
asked on
Extracting Primary and Foreign Keys
Hi all,
This is more of an SQL question really...
I know I can get the foreign keys of a table by accessing the CONSTRAINTS and CONS_COLUMNS tables, and I know I can then get the matching Primary key of the referenced table by using the referenced Constraint.
Trouble is, I don't know how to put this into a single SQL statement such that I can identify which is which when the query runs. I want to identify the output as
Constraint Name
Primary Key Table
Primary Key Column
Foreign Key Table
Foreign Key Column
but I don't know how to identify each output pair as either a Primary or Foreign Key (the problem being that both are extracted from the same column). I'm guessing a Decode of some sort is required based on the constraint name, but I don't know how to write it.
Can anyone help please?
Many thanks
Gerry
This is more of an SQL question really...
I know I can get the foreign keys of a table by accessing the CONSTRAINTS and CONS_COLUMNS tables, and I know I can then get the matching Primary key of the referenced table by using the referenced Constraint.
Trouble is, I don't know how to put this into a single SQL statement such that I can identify which is which when the query runs. I want to identify the output as
Constraint Name
Primary Key Table
Primary Key Column
Foreign Key Table
Foreign Key Column
but I don't know how to identify each output pair as either a Primary or Foreign Key (the problem being that both are extracted from the same column). I'm guessing a Decode of some sort is required based on the constraint name, but I don't know how to write it.
Can anyone help please?
Many thanks
Gerry
ASKER
Thanks for that.
However, while this works for single column foreign keys, in the case of multiple column keys, it doesn't tie up the individual columns on either side i.e.
PKTable A, PKColumns 1 & 2
FKTable B, FKColumns 1 & 2
I'm getting results back that point B.1 to A.2 and vice versa
Any idea how to fix that?
Thanks again
Gerry
However, while this works for single column foreign keys, in the case of multiple column keys, it doesn't tie up the individual columns on either side i.e.
PKTable A, PKColumns 1 & 2
FKTable B, FKColumns 1 & 2
I'm getting results back that point B.1 to A.2 and vice versa
Any idea how to fix that?
Thanks again
Gerry
ASKER
Update to the previous post:
I'm getting 4 rows back:
A.1 to B.1
A.2 to B.1
A.1 to B.2
A.2 to B.2
Thanks
Gerry
I'm getting 4 rows back:
A.1 to B.1
A.2 to B.1
A.1 to B.2
A.2 to B.2
Thanks
Gerry
select a.TABLE_NAME||'.'||c.colum n_name||' References '||b.table_name val
from all_constraints a, all_constraints b, all_cons_columns c
where a.R_CONSTRAINT_NAME=b.CONS TRAINT_NAM E and
a.CONSTRAINT_NAME=c.CONSTR AINT_NAME
and a.CONSTRAINT_TYPE='R'
and a.owner=b.owner
and a.owner=c.owner
/
from all_constraints a, all_constraints b, all_cons_columns c
where a.R_CONSTRAINT_NAME=b.CONS
a.CONSTRAINT_NAME=c.CONSTR
and a.CONSTRAINT_TYPE='R'
and a.owner=b.owner
and a.owner=c.owner
/
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Yeah, that works. However, the only thing I would say is that it seems to return many more rows than I need, but that said, I can use code to filter the unwanted rows out (it seems to be returning all columns in a selected table, not just those related to a foreign key).
Mohan, thanks for that. Your statement only picks up the rows I need, but it doesn't give me the primary key column(s). Is it possible to add that to the statement?
Many thanks
Gerry
Mohan, thanks for that. Your statement only picks up the rows I need, but it doesn't give me the primary key column(s). Is it possible to add that to the statement?
Many thanks
Gerry
select a.TABLE_NAME||'('||
rtrim(max(decode(c.positio n,1,c.colu mn_name))| |','||
max(decode(c.position,2,c. column_nam e))||','||
max(decode(c.position,3,c. column_nam e))||','||
max(decode(c.position,4,c. column_nam e)),',')|| ') References '||
b.table_name||'('||
rtrim(max(decode(d.positio n,1,d.colu mn_name))| |','||
max(decode(d.position,2,d. column_nam e))||','||
max(decode(d.position,3,d. column_nam e))||','||
max(decode(d.position,4,d. column_nam e)),',')|| ')'
from all_constraints a, all_constraints b, all_cons_columns c, all_cons_columns d
where a.R_CONSTRAINT_NAME=b.CONS TRAINT_NAM E and
a.CONSTRAINT_NAME=c.CONSTR AINT_NAME
and b.CONSTRAINT_NAME=d.CONSTR AINT_NAME
and a.CONSTRAINT_TYPE='R'
and b.CONSTRAINT_TYPE='P'
and a.constraint_name='BOX_OFF ICE_DAILY_ FK1'
and a.owner=b.owner
and a.owner=c.owner
and a.owner=d.owner
and a.owner='WB2DEV'
group by a.table_name, b.table_name
/
rtrim(max(decode(c.positio
max(decode(c.position,2,c.
max(decode(c.position,3,c.
max(decode(c.position,4,c.
b.table_name||'('||
rtrim(max(decode(d.positio
max(decode(d.position,2,d.
max(decode(d.position,3,d.
max(decode(d.position,4,d.
from all_constraints a, all_constraints b, all_cons_columns c, all_cons_columns d
where a.R_CONSTRAINT_NAME=b.CONS
a.CONSTRAINT_NAME=c.CONSTR
and b.CONSTRAINT_NAME=d.CONSTR
and a.CONSTRAINT_TYPE='R'
and b.CONSTRAINT_TYPE='P'
and a.constraint_name='BOX_OFF
and a.owner=b.owner
and a.owner=c.owner
and a.owner=d.owner
and a.owner='WB2DEV'
group by a.table_name, b.table_name
/
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Outstanding, thank you both very much
c1.constraint_name,
decode(c2.constraint_type,
decode(c2.constraint_type,
decode(c2.constraint_type,
decode(c2.constraint_type,
from
user_cons_columns c1, user_constraints c2, user_cons_columns c3
where c1.constraint_name = c2.constraint_name and c2.r_constraint_name = c3.constraint_name (+)