Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Oracle all foreign key references

Tags:

oracle

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?

like image 582
AJM Avatar asked Jul 23 '09 12:07

AJM


People also ask

How can I list all foreign keys referencing a given table in Oracle?

First method is with table Constraints tab (select table and select Constraints tab). Tab lists table constraints - primary, unique and foreign keys and check constraints - all in one grid. Foreign keys are the ones with 'Foreign_Key' value in CONSTRAINT_TYPE column.

How can I find out what foreign key constraint references a table in Oracle?

You can check for foreign key constraint errors by looking at the dba_constraints and the dba_cons_columns views. Using the cascade constraints clause in a drop table will force a cascade delete to occur in all child tables. Oracle also has the drop table if exists feature.

Can foreign key have multiple references?

Foreign key columns must use their referenced column's type. A foreign key column cannot be a virtual computed column, but it can be a stored computed column. A single column can have multiple foreign key constraints. For an example, see Add multiple foreign key constraints to a single column.


3 Answers

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.

like image 132
Rob van Wijk Avatar answered Oct 22 '22 00:10

Rob van Wijk


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.

like image 39
Stan Avatar answered Oct 22 '22 01:10

Stan


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'

like image 20
Erich Kitzmueller Avatar answered Oct 22 '22 01:10

Erich Kitzmueller