Q
Problem solve Get help with specific problems with your technologies, process and projects.

SQL to view all foreign keys that reference a column

What is the SQL to view all the foreign keys that reference a column?
Say you have a table called MY_TABLE, and the primary key in that table is MY_COLUMN, the primary key name is PK_MY_TABLE.

Then you can say,

SELECT CONSTRAINT_NAME 
  FROM USER_CONSTRAINS 
WHERE R_CONSTRAINT_NAME ='PK_MY_TABLE';

Now, if you do not know the primary key then you can query the foreign keys as follows

SELECT CONSTRAINT_NAME 
FROM USER_CONSTRAINTS
WHERE R_CONSTRAINT_NAME IN (SELECT T1.INDEX_NAME FROM USER_IND_COLUMNS T1, USER_INDEXES T2
WHERE T1.INDEX_NAME =T2.INDEX_NAME
AND T2.TABLE_NAME = 'MY_TABLE'
AND T1.COLUMN_NAME = 'MY_COLUMN');

Dig Deeper on Using Oracle PL-SQL

Have a question for an expert?

Please add a title for your question

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

Start the conversation

Send me notifications when other members comment.

Please create a username to comment.

-ADS BY GOOGLE

SearchDataManagement

SearchBusinessAnalytics

SearchSAP

SearchSQLServer

TheServerSide.com

SearchDataCenter

SearchContentManagement

SearchHRSoftware

Close