17
votes

I've inherited a schema so don't have complete knowledge/confidence in what is there.

I have a project table with a projectId. There are a whole bunch of other tables that reference this table by project id. What I want to do is run a query to establish:

  1. Which tables have foreign key references to the project table on the project id
  2. Which tables have a column called project id (in case foreign keys are not set up).

If it was SQL Server I know how to query the metadata but how do I do this in Oracle?

6

6 Answers

22
votes

1)

select table_name
  from all_constraints
 where r_constraint_name = [your pk/uk constraint on PROJECTS(id)]

2)

select table_name
  from all_tab_columns
 where column_name = 'PROJECT_ID'

You may want to add an extra predicate containing the OWNER column.

Regards, Rob.

18
votes

Ok. Here a request that give you the referenced table and column :

SELECT
 c_list.CONSTRAINT_NAME as NAME,
 substr(c_src.COLUMN_NAME, 1, 20) as SRC_COLUMN,
 c_dest.TABLE_NAME as DEST_TABLE,
 substr(c_dest.COLUMN_NAME, 1, 20) as DEST_COLUMN
FROM ALL_CONSTRAINTS c_list, ALL_CONS_COLUMNS c_src, ALL_CONS_COLUMNS c_dest
WHERE c_list.CONSTRAINT_NAME   = c_src.CONSTRAINT_NAME
 AND  c_list.OWNER             = c_src.OWNER
 AND  c_list.R_CONSTRAINT_NAME = c_dest.CONSTRAINT_NAME
 AND  c_list.OWNER             = c_dest.OWNER
 AND  c_list.CONSTRAINT_TYPE = 'R'
 AND  c_src.OWNER      = '<your-schema-here>'
 AND  c_src.TABLE_NAME = '<your-table-here>'
GROUP BY c_list.CONSTRAINT_NAME, c_src.TABLE_NAME,
    c_src.COLUMN_NAME, c_dest.TABLE_NAME, c_dest.COLUMN_NAME;

Which give you something like this:

NAME                  |SRC_COLUMN      |DEST_TABLE            | DEST_COLUMN
----------------------|----------------|----------------------|-----------
CFK_RUB_FOR           |FOR_URN         |T03_FORMAT            |FOR_URN
CFK_RUB_RUB           |RUB_RUB_URN     |T01_RUBRIQUE          |RUB_URN
CFK_RUB_SUP           |SUP_URN         |T01_SUPPORT           |SUP_URN
CFK_RUB_PRD           |PRD_URN         |T05_PRODUIT           |PRD_URN

You can forget the substr() function if the result is usable without. This is not my case.

1
votes

1): SELECT * FROM USER_CONSTRAINTS WHERE CONSTRAINT_NAME='R' and R_CONSTRAINT_NAME='xxx'

where xxx is the name of the primary key constraint on the project table

2): SELECT * FROM USER_TAB_COLUMNS WHERE COLUMN_NAME='PROJECT_ID'

0
votes

The r_constraint_name answers here didn't seem to work for me, not sure why as I'm new to Oracle myself, but this worked:

SELECT * FROM ALL_CONSTRAINTS WHERE CONSTRAINT_NAME = '<constraint>';
0
votes

My problem was slightly different. I have a table and I wanted to programatically know which other tables/columns it references.

I started with Stan's response above but this didn't give me exactly what I needed, so I came up with this, which I post here in case anyone else has my problem:

WITH src as
(SELECT ac.table_name, ac.constraint_name, accs.column_name, accs.position, ac.r_constraint_name
  FROM ALL_CONSTRAINTS ac, all_cons_columns accs
 WHERE ac.owner = '<owner>'
   AND ac.constraint_type = 'R'
   AND ac.table_name = '<src_table>'
   AND accs.owner = ac.owner
   AND accs.table_name = ac.table_name
   AND accs.constraint_name = ac.constraint_name
ORDER BY ac.table_name, ac.constraint_name, accs.position),
dst as
(SELECT ac.table_name, ac.constraint_name, accs.column_name, accs.position
  FROM ALL_CONSTRAINTS ac, all_cons_columns accs
 WHERE ac.owner = '<owner>'
   AND accs.owner = ac.owner
   AND accs.table_name = ac.table_name
   AND accs.constraint_name = ac.constraint_name
ORDER BY ac.table_name, ac.constraint_name, accs.position)
SELECT src.table_name as src_table,
       dst.table_name as dst_table,
       src.constraint_name as src_constraint,
       src.column_name as src_column,
       dst.column_name as dst_column,
       src.position as position
  FROM src,dst
 WHERE src.r_constraint_name = dst.constraint_name
   AND src.position = dst.position
0
votes

Use this query.

   select b.TABLE_NAME,b.CONSTRAINT_NAME ,a.COLUMN_NAME
   from all_constraints b, all_cons_columns a
   where r_constraint_name = 'Constraint_Name' and  a.CONSTRAINT_NAME=b.CONSTRAINT_NAME;