tags:

views:

1513

answers:

4

Hello All. Is there a command in oracle 9i that displays the foreign keys of a table and also the table that those foreign keys reference?

I was searching, did not find anything but i found an equivalent command that works with MySql which is SHOW CREATE TABLE

Is there an equivalent command for this within oracle's SQL?

I appreciate your response, however I thought there was a really short way of doing this like MySql.

+1  A: 

This may do what you want, it uses Oracle system views. I don't have an Oracle instance handy to test it, however.

SELECT fk.owner, fk.constraint_name, fk.table_name, fc.column_name,
  pk.owner, pk.constraint_name, pk.table_name, pc.column_name
FROM all_constraints fk
 JOIN all_cons_columns fc ON (fk.owner = fc.owner AND fk.constraint_name = fc.constraint_name)
 JOIN (all_constraints pk
   JOIN all_cons_columns pc ON (pk.owner = pc.owner AND pk.constraint_name = pc.constraint_name)) 
 ON (fk.r_owner = pk.owner AND fk.r_constraint_name = pk.constraint_name
   AND fc.position = pc.position)
WHERE fk.constraint_type = 'R' AND pk.constraint_type IN ('P', 'U')
  AND fk.owner = '<schema>' AND fk.table_name = '<table>';
Bill Karwin
Yeesh... you try to help, and see what you get.
Bill Karwin
I got the following error message:ERROR at line 7:ORA-25154: column part of USING clause cannot have qualifier
I'll rewrite this with ON syntax instead of USING.
Bill Karwin
+2  A: 

You could start by listing all of the constraints for the table along with any referenced constraint on other tables:

SELECT
     acc.table_name
    ,acc.column_name
    ,acc.constraint_name
    ,ac.r_constraint_name AS referenced_constraint
FROM all_cons_columns acc
INNER JOIN all_constraints ac ON (acc.constraint_name = ac.constraint_name)
WHERE acc.table_name = UPPER('your_table_here');

If you have sensible naming conventions for your constraints it should be possible to identify which are the foreign keys, an 'FK' prefix/suffix is typical.

ninesided
I got: no rows selectedAnd did enter a correct table name.
I've updated it to use all_constraints rather than user_constraints, try that for size
ninesided
+4  A: 

Here's another answer: The dbms_metadata package has a function that can return the DDL for a table definition.

SELECT dbms_metadata.get_ddl('TABLE', '<table>', '<schema>') FROM dual;

This package has apparently been available since Oracle 9.2

http://download-west.oracle.com/docs/cd/B10501_01/appdev.920/a96612/d_metada.htm#1656

Bill Karwin
I got a really long error message that starts with:ORA-31603: object "registration" of type TABLE not found in schema "tset"I think it might be because im using oracle 9i
I saw an Oracle forum message that suggests you need to create the get_ddl() procedure in the SYS schema for it to have the right privileges to system metadata.
Bill Karwin
The names are case-sensitive here, and are stored as uppercase unless explicitly quoted. So REGISTRATION and TSET (or should that be TEST) may work.
Gary
@igor-db: Yes, good point. Oracle says it's "case-insensitive" but that's just because it maps names to uppercase as it stores them.
Bill Karwin
A: 

If you need the DDL for the foreign keys in the future, then here is the answer in advance :)

select 
  DBMS_METADATA.GET_DEPENDENT_DDL('REF_CONSTRAINT' ,atb.table_name, atb.owner) 
from 
  all_tables atb, all_constraints ac
where 
  atb.owner = ac.owner and
  ac.constraint_type = 'R' and
  ac.table_name = atb.table_name and
  atb.owner = 'YOURSCHEMA';
bpelhos