get constraint name by column name in oracle database - oracle

How to get unique constraint_name of The corresponding column in oracle database?
in other words I want to get constraint name by column name.
pseudo query for this question:
select constraint_name from table_name tname where tname.column_name = 'columnName';
is this possible?

Use the Oracle dictionary views ALL_CONS_COLUMNS or USER_CONS_COLUMNS (if you want to restrict it by column type - i.e. unique or primary key constraints - then you can join in ALL_CONSTRAINTS or USER_CONSTRAINTS):
SELECT acc.constraint_name
FROM ALL_CONS_COLUMNS acc
INNER JOIN ALL_CONSTRAINTS ac
ON ( acc.CONSTRAINT_NAME = ac.CONSTRAINT_NAME )
WHERE ac.OWNER = 'YOUR_SCHEMA_NAME'
AND ac.TABLE_NAME = 'YOUR_TABLE_NAME'
AND acc.COLUMN_NAME = 'YOUR_COLUMN_NAME'
AND ac.CONSTRAINT_TYPE IN ( 'U', 'P' ) -- Unique or primary key constraints
or:
SELECT ucc.constraint_name
FROM USER_CONS_COLUMNS ucc
INNER JOIN ALL_CONSTRAINTS uc
ON ( ucc.CONSTRAINT_NAME = uc.CONSTRAINT_NAME )
WHERE uc.OWNER = 'YOUR_SCHEMA_NAME'
AND uc.TABLE_NAME = 'YOUR_TABLE_NAME'
AND ucc.COLUMN_NAME = 'YOUR_COLUMN_NAME'
AND uc.CONSTRAINT_TYPE IN ( 'U', 'P' ) -- Unique or primary key constraints

Try this.
select CONSTRAINT_NAME from USER_CONS_COLUMNS
where table_name='YOUR_TABLE'
and column_name='YOUR_COLUMN'

Related

List of tables along with key information - Oracle

I am trying to fetch the list of tables, column information including the data type from the Oracle metadata tables. You can see the list of columns below. Somehow primary key and foreign key constraint data that I am pulling seems to be replicating for all the columns of that table in question, whereas I expect null for the columns which are not tagged as primary key or foreign key in the constraint tables.
I have used a left outer join, but seems like it is not working as expected due to the joining conditions. Any help would be appreciated.
SELECT AO.OWNER,
AO.OBJECT_NAME,
ATC.COLUMN_NAME,
ATC.DATA_TYPE,
ATC.NULLABLE,
AC.CONSTRAINT_NAME
FROM ALL_OBJECTS AO
JOIN
ALL_TAB_COLUMNS ATC
ON AO.OBJECT_NAME=ATC.TABLE_NAME
LEFT JOIN
ALL_CONS_COLUMNS ACC
ON
--ACC.column_name=ATC.column_name and
ACC.TABLE_NAME=ATC.TABLE_NAME
inner JOIN
ALL_CONSTRAINTS AC
ON
AC.TABLE_NAME=ACC.TABLE_NAME
AND
AC.CONSTRAINT_NAME=ACC.CONSTRAINT_NAME
--and atc.table_name=ac.table_name
WHERE AO.OBJECT_TYPE = 'TABLE'
AND AO.OWNER = 'XYZ'
AND AO.OBJECT_NAME='ABC'
AND CONSTRAINT_TYPE IN ('P','R')
ORDER BY AO.OBJECT_NAME, ATC.COLUMN_NAME
I think your join conditions are not proper and owner must be used in join conditions as all_ views are uses
You should try following query:
SELECT AT.OWNER,
AT.TABLE_NAME,
LISTAGG(ATC.COLUMN_NAME,',') WITHIN GROUP (ORDER BY ATC.COLUMN_ID) AS COLUMN_NAMES,
LISTAGG(ATC.DATA_TYPE,',') WITHIN GROUP (ORDER BY ATC.COLUMN_ID) AS DATA_TYPE,
LISTAGG(ATC.NULLABLE,',') WITHIN GROUP (ORDER BY ATC.COLUMN_ID) AS NULLABLE,
AC.CONSTRAINT_NAME
FROM ALL_TABLES AT
JOIN
ALL_TAB_COLUMNS ATC
ON AT.TABLE_NAME=ATC.TABLE_NAME
AND AT.OWNER = ATC.OWNER
LEFT JOIN
ALL_CONS_COLUMNS ACC
ON ATC.COLUMN_NAME = ACC.COLUMN_NAME
AND ACC.TABLE_NAME = AT.TABLE_NAME
AND AC.OWNER = ACC.OWNER
LEFT JOIN ALL_CONSTRAINTS AC
ON AT.TABLE_NAME = AC.TABLE_NAME
AND AC.CONSTRAINT_NAME = ACC.CONSTRAINT_NAME
AND AT.OWNER = AC.OWNER
WHERE AT.OBJECT_TYPE = 'TABLE'
AND AT.OWNER = 'XYZ'
AND AT.OBJECT_NAME='ABC'
AND CONSTRAINT_TYPE IN ('P','R')
GROUP BY AT.OWNER,
AT.TABLE_NAME,
AC.CONSTRAINT_NAME
Cheers!!

how to get full tables and columns with relations or not in ORACLE

How can I do to (left)join user_tab_columns with user_cons_columns and user_constraints to get the result below:
Table, Column, SourceTable, SourceColumn
ORDER, ORDER_ID, null, null
ORDER, ORDER_DATE, null, null
ORDER, ORDER_CLIENT, CLIENT, CLIENT_ID
CLIENT, CLIENT_ID, null, null
ORDER_DETAIL, ORDER_ID, ORDER, ORDER_ID
ORDER_DETAIL, PRODUCT_ID, PRODUCT, PRODUCT_ID
PRODUCT PRODUCT_ID, null, null
So... I need all table and columns but once a column is a foreign key,
I need to know the table and the column (primary key) related.
Slight edits to linked answer:
List of foreign keys and the tables they reference
all_ to user sources.
Course you could just use all and then filter on owner.
SELECT a.table_name, a.column_name, a.constraint_name, c.owner,
c.r_owner, c_pk.table_name r_table_name, c_pk.constraint_name r_pk
FROM user_cons_columns a
LEFT JOIN user_constraints c
ON a.owner = c.owner
AND a.constraint_name = c.constraint_name
LEFT JOIN user_constraints c_pk
ON c.r_owner = c_pk.owner
AND c.r_constraint_name = c_pk.constraint_name
WHERE c.constraint_type = 'R'
--AND a.table_name = :TableName
check following query :
select a.table_name,c1.column_name,b.table_name,c2.column_name from USER_constraints a,USER_constraints b,user_cons_columns c1,user_cons_columns c2
where a.r_constraint_name = b.constraint_name and a.table_name = c1.table_name and b.table_name = c2.table_name if you just need FK constraint use a.constraint_type ='R' in where clouse.

List of table which don't have a primary key

Need to get the list of tables name from a schema which doesn't have a primary key. I tried the following query to get this, but it will list all other keys except primary keys.
SELECT a.constraint_name,a.table_name
FROM ALL_CONS_COLUMNS A
JOIN ALL_CONSTRAINTS C
ON A.CONSTRAINT_NAME = C.CONSTRAINT_NAME
WHERE
C.CONSTRAINT_TYPE not in('P')
and a.owner ='my_schema';
If you only want this for the current user, it's better to user the user_xxx views instead of the all_xxx views.
The following should do what you want:
select ut.table_name
from user_tables ut
where not exists (select 1
from user_constraints ac
where ac.table_name = ut.table_name
and ac.constraint_type = 'P'
);
If you do need this for a different user, then you can use the following.
select at.*
from all_tables at
where not exists (select 1
from all_constraints ac
where ac.owner = at.owner
and ac.table_name = at.table_name
and ac.constraint_type = 'P'
)
and at.owner = 'MY_SCHEMA';
Don't forget that Oracle is case sensitive and that user names are stored in uppercase, so a.owner ='my_schema' will most probably not return anything.
Another way:
select owner,table_name
from all_tables
where owner = 'my_schema'
MINUS
select owner,table_name
from all_constraints
where owner = 'my_schema'
and constraint_type = 'P'
Try like this,
SELECT table_name
FROM all_tables A
WHERE table_name NOT IN
(
SELECT table_name
FROM all_constraints WHERE constraint_type ='P'
)
AND a.owner = 'my_schema';
Select tables.owner || ‘.’ ||
tables.table_name
as table_owner
From all_tables tables,
(Select
owner,
TABLE_NAME,
constraint_type,
CONSTRAINT_NAME
FROM ALL_CONSTRAINTS
WHERE CONSTRAINT_TYPE = ‘P’ /* list of all tables with PK */
) constr
Where tables.owner = constr.owner (+)
And tables.table_name = constr.table_name (+)
and tables.owner <> ‘SYS’
and tables.owner <> ‘SYSTEM’
And constr.owner IS NULL
And constr.table_name IS NULL
ORDER BY 1

How to find tables having foreign key to a table in Oracle?

I plan to delete data from a table, I would like to know how many and which tables have a foreign key reference to this particular table in Oracle. As I will have to set the foreign keys to null. I would like to know list of all tables which have a FK to this particular table.
SELECT
FK.OWNER||'.'||FK.TABLE_NAME AS CHILD_TABLE,
SRC.OWNER||'.'||SRC.TABLE_NAME AS PARENT_TABLE,
FK.CONSTRAINT_NAME AS FK_CONSTRAINT,
SRC.CONSTRAINT_NAME AS REFERENCED_CONSTRAINT
FROM ALL_CONSTRAINTS FK
JOIN ALL_CONSTRAINTS SRC ON FK.R_CONSTRAINT_NAME = SRC.CONSTRAINT_NAME
WHERE
FK.CONSTRAINT_TYPE = 'R'
AND SRC.OWNER = 'MY_SCHEMA'
AND SRC.TABLE_NAME = 'MY_TABLE';
I have a situation where the table I'm interested in isn't owned by the schema I was connecting as. So I needed to modify the query in the currently accepted answer to use ALL_CONSTRAINTS instead of USER_CONSTRAINTS. In the process, I made a mistake, and I found the accepted answer to be very difficult to read so that I could fix it. (The lack of explanation didn't help.) As a result, I ended up coming up with my own query. It's basically the same, but I think it's a bit easier to grok.
FK.CONSTRAINT_TYPE = 'R' filters down FK to a set of foreign key constraints, and the join pairs these foreign keys up with their "Referenced constraint". (The referenced constraint is usually the primary key of the "parent" table.) Finally, we filter down to the parent table we're interested in using SRC.OWNER = 'MY_SCHEMA' AND SRC.TABLE_NAME = 'MY_TABLE'.
Naturally, you can switch this to use USER_CONSTRAINTS if you wish; just remove the SRC.OWNER check and the OWNER prefixes in the SELECT.
SELECT d.table_name,
d.constraint_name "Primary Constraint Name",
b.constraint_name "Referenced Constraint Name"
FROM user_constraints d,
(SELECT c.constraint_name,
c.r_constraint_name,
c.table_name
FROM user_constraints c
WHERE table_name='EMPLOYEES' --your table name instead of EMPLOYEES
AND constraint_type='R') b
WHERE d.constraint_name=b.r_constraint_name
Below query will give all the foreign key constraints defined on TABLE_NAME:
select baseTable.* from all_constraints baseTable , all_constraints referentedTable
where baseTable.R_CONSTRAINT_NAME = referentedTable.CONSTRAINT_NAME
and baseTable.constraint_type = 'R'
and referentedTable.table_name = 'TABLE_NAME';
Maybe I misunderstood what Walker asked, but what I understood is : How to find tables that have a foreign key reference to a particular table (ex : EMPLOYEES).
If I try Kupa's answer :
select d.table_name,
d.constraint_name "Primary Constraint Name",
b.constraint_name "Referenced Constraint Name"
from user_constraints d,
(select c.constraint_name,
c.r_constraint_name,
c.table_name
from user_constraints c
where table_name='EMPLOYEES' --your table name instead of EMPLOYEES
and constraint_type='R') b
where d.constraint_name=b.r_constraint_name
I get the tables on which EMPLOYEES have a foreign key reference to.
EMPLOYEES.foreign_key => TABLES.primary_key
See below the updated sql to retrieve the tables that have a foreign key reference to EMPLOYEES.
TABLES.foreign_key => EMPLOYEES.primary_key
select b.table_name "Table Name",
b.constraint_name "Constraint Name",
d.table_name "Referenced Table Name",
d.constraint_name "Referenced Constraint Name"
from user_constraints d,
(select c.constraint_name,
c.r_constraint_name,
c.table_name
from user_constraints c
where constraint_type='R') b
where d.table_name = 'EMPLOYEES' --your table name instead of EMPLOYEES
and b.r_constraint_name = d.constraint_name;
If you also need the fields to be included:
select b.table_name "Referencing Table",
b.CONSTRAINT_NAME "Referencing Constraint",
(select wm_concat(column_name)
from all_cons_columns
where owner = b.owner
and constraint_name = b.CONSTRAINT_NAME
) "Referencing Columns",
a.CONSTRAINT_NAME "Referenced Constraint",
(select wm_concat(column_name)
from all_cons_columns
where owner = a.owner
and constraint_name = a.CONSTRAINT_NAME
) "Referenced columns"
from all_constraints a,
all_constraints b
where a.owner = b.r_owner
and a.owner = '<<OWNER>>'
and a.table_name = '<<TABLE_NAME>>'
and a.constraint_type in ('P', 'U')
and b.constraint_type = 'R'
and b.R_CONSTRAINT_NAME = a.constraint_name
There is no need to do this step manually - you can just use a cascading delete.
SELECT a.table_name, a.column_name, a.constraint_name, c.owner,
-- referenced pk
c.r_owner, c_pk.table_name r_table_name, c_pk.constraint_name r_pk
FROM all_cons_columns a
JOIN all_constraints c ON a.owner = c.owner
AND a.constraint_name = c.constraint_name
JOIN all_constraints c_pk ON c.r_owner = c_pk.owner
AND c.r_constraint_name = c_pk.constraint_name
WHERE c.constraint_type = 'R'
AND a.table_name = :TableName
SELECT CONSTRAINT_NAME from ALL_CONSTRAINTS WHERE OWNER= sys_context('userenv','current_schema') AND CONSTRAINT_TYPE='R';

List of foreign keys and the tables they reference in Oracle DB

I'm trying to find a query which will return me a list of the foreign keys for a table and the tables and columns they reference. I am half way there with
SELECT a.table_name,
a.column_name,
a.constraint_name,
c.owner
FROM ALL_CONS_COLUMNS A, ALL_CONSTRAINTS C
where A.CONSTRAINT_NAME = C.CONSTRAINT_NAME
and a.table_name=:TableName
and C.CONSTRAINT_TYPE = 'R'
But I still need to know which table and primary key are referenced by this key. How would I get that?
The referenced primary key is described in the columns r_owner and r_constraint_name of the table ALL_CONSTRAINTS. This will give you the info you want:
SELECT a.table_name, a.column_name, a.constraint_name, c.owner,
-- referenced pk
c.r_owner, c_pk.table_name r_table_name, c_pk.constraint_name r_pk
FROM all_cons_columns a
JOIN all_constraints c ON a.owner = c.owner
AND a.constraint_name = c.constraint_name
JOIN all_constraints c_pk ON c.r_owner = c_pk.owner
AND c.r_constraint_name = c_pk.constraint_name
WHERE c.constraint_type = 'R'
AND a.table_name = :TableName
Try this:
select * from all_constraints where r_constraint_name in (select constraint_name
from all_constraints where table_name='YOUR_TABLE_NAME');
Here is an all-purpose script we use that has been incredibly handy.
Save it off so you can execute it directly (#fkeys.sql). It will let you search by Owner and either the Parent or Child table and show foreign key relationships. The current script does explicitly spool to C:\SQLRPTS so you will need to create that folder of change that line to something you want to use.
REM ########################################################################
REM ##
REM ## fkeys.sql
REM ##
REM ## Displays the foreign key relationships
REM ##
REM #######################################################################
CLEAR BREAK
CLEAR COL
SET LINES 200
SET PAGES 54
SET NEWPAGE 0
SET WRAP OFF
SET VERIFY OFF
SET FEEDBACK OFF
break on table_name skip 2 on constraint_name on r_table_name skip 1
column CHILDCOL format a60 head 'CHILD COLUMN'
column PARENTCOL format a60 head 'PARENT COLUMN'
column constraint_name format a30 head 'FK CONSTRAINT NAME'
column delete_rule format a15
column bt noprint
column bo noprint
TTITLE LEFT _DATE CENTER 'FOREIGN KEY RELATIONSHIPS ON &new_prompt' RIGHT 'PAGE:'FORMAT 999 SQL.PNO SKIP 2
SPOOL C:\SQLRPTS\FKeys_&new_prompt
ACCEPT OWNER_NAME PROMPT 'Enter Table Owner (or blank for all): '
ACCEPT PARENT_TABLE_NAME PROMPT 'Enter Parent Table or leave blank for all: '
ACCEPT CHILD_TABLE_NAME PROMPT 'Enter Child Table or leave blank for all: '
select b.owner || '.' || b.table_name || '.' || b.column_name CHILDCOL,
b.position,
c.owner || '.' || c.table_name || '.' || c.column_name PARENTCOL,
a.constraint_name,
a.delete_rule,
b.table_name bt,
b.owner bo
from all_cons_columns b,
all_cons_columns c,
all_constraints a
where b.constraint_name = a.constraint_name
and a.owner = b.owner
and b.position = c.position
and c.constraint_name = a.r_constraint_name
and c.owner = a.r_owner
and a.constraint_type = 'R'
and c.owner like case when upper('&OWNER_NAME') is null then '%'
else upper('&OWNER_NAME') end
and c.table_name like case when upper('&PARENT_TABLE_NAME') is null then '%'
else upper('&PARENT_TABLE_NAME') end
and b.table_name like case when upper('&CHILD_TABLE_NAME') is null then '%'
else upper('&CHILD_TABLE_NAME') end
order by 7,6,4,2
/
SPOOL OFF
TTITLE OFF
SET FEEDBACK ON
SET VERIFY ON
CLEAR BREAK
CLEAR COL
SET PAGES 24
SET LINES 100
SET NEWPAGE 1
UNDEF OWNER
This will travel the hierarchy of foreign keys for a given table and column and return columns from child and grandchild, and all descendant tables. It uses sub-queries to add r_table_name and r_column_name to user_constraints, and then uses them to connect rows.
select distinct table_name, constraint_name, column_name, r_table_name, position, constraint_type
from (
SELECT uc.table_name,
uc.constraint_name,
cols.column_name,
(select table_name from user_constraints where constraint_name = uc.r_constraint_name)
r_table_name,
(select column_name from user_cons_columns where constraint_name = uc.r_constraint_name and position = cols.position)
r_column_name,
cols.position,
uc.constraint_type
FROM user_constraints uc
inner join user_cons_columns cols on uc.constraint_name = cols.constraint_name
where constraint_type != 'C'
)
start with table_name = 'MY_TABLE_NAME' and column_name = 'MY_COLUMN_NAME'
connect by nocycle
prior table_name = r_table_name
and prior column_name = r_column_name;
Here is an another solution. Using sys's default views are so slow (approx 10s in my situation). This is much faster than that (approx. 0.5s).
SELECT
CONST.NAME AS CONSTRAINT_NAME,
RCONST.NAME AS REF_CONSTRAINT_NAME,
OBJ.NAME AS TABLE_NAME,
COALESCE(ACOL.NAME, COL.NAME) AS COLUMN_NAME,
CCOL.POS# AS POSITION,
ROBJ.NAME AS REF_TABLE_NAME,
COALESCE(RACOL.NAME, RCOL.NAME) AS REF_COLUMN_NAME,
RCCOL.POS# AS REF_POSITION
FROM SYS.CON$ CONST
INNER JOIN SYS.CDEF$ CDEF ON CDEF.CON# = CONST.CON#
INNER JOIN SYS.CCOL$ CCOL ON CCOL.CON# = CONST.CON#
INNER JOIN SYS.COL$ COL ON (CCOL.OBJ# = COL.OBJ#) AND (CCOL.INTCOL# = COL.INTCOL#)
INNER JOIN SYS.OBJ$ OBJ ON CCOL.OBJ# = OBJ.OBJ#
LEFT JOIN SYS.ATTRCOL$ ACOL ON (CCOL.OBJ# = ACOL.OBJ#) AND (CCOL.INTCOL# = ACOL.INTCOL#)
INNER JOIN SYS.CON$ RCONST ON RCONST.CON# = CDEF.RCON#
INNER JOIN SYS.CCOL$ RCCOL ON RCCOL.CON# = RCONST.CON#
INNER JOIN SYS.COL$ RCOL ON (RCCOL.OBJ# = RCOL.OBJ#) AND (RCCOL.INTCOL# = RCOL.INTCOL#)
INNER JOIN SYS.OBJ$ ROBJ ON RCCOL.OBJ# = ROBJ.OBJ#
LEFT JOIN SYS.ATTRCOL$ RACOL ON (RCCOL.OBJ# = RACOL.OBJ#) AND (RCCOL.INTCOL# = RACOL.INTCOL#)
WHERE CONST.OWNER# = userenv('SCHEMAID')
AND RCONST.OWNER# = userenv('SCHEMAID')
AND CDEF.TYPE# = 4 /* 'R' Referential/Foreign Key */;
If you need all the foreign keys of the user then use the following script
SELECT a.constraint_name, a.table_name, a.column_name, c.owner,
c_pk.table_name r_table_name, b.column_name r_column_name
FROM user_cons_columns a
JOIN user_constraints c ON a.owner = c.owner
AND a.constraint_name = c.constraint_name
JOIN user_constraints c_pk ON c.r_owner = c_pk.owner
AND c.r_constraint_name = c_pk.constraint_name
JOIN user_cons_columns b ON C_PK.owner = b.owner
AND C_PK.CONSTRAINT_NAME = b.constraint_name AND b.POSITION = a.POSITION
WHERE c.constraint_type = 'R'
based on Vincent Malgrat code
I know it's kinda late to answer but let me answer anyway, some of the answers
above are quite complicated hence here is a much simpler take.
SELECT a.table_name child_table, a.column_name child_column, a.constraint_name,
b.table_name parent_table, b.column_name parent_column
FROM all_cons_columns a
JOIN all_constraints c ON a.owner = c.owner AND a.constraint_name = c.constraint_name
join all_cons_columns b on c.owner = b.owner and c.r_constraint_name = b.constraint_name
WHERE c.constraint_type = 'R'
AND a.table_name = 'your table name'
My version, in my humble opinion, more readable:
SELECT PARENT.TABLE_NAME "PARENT TABLE_NAME"
, PARENT.CONSTRAINT_NAME "PARENT PK CONSTRAINT"
, '->' " "
, CHILD.TABLE_NAME "CHILD TABLE_NAME"
, CHILD.COLUMN_NAME "CHILD COLUMN_NAME"
, CHILD.CONSTRAINT_NAME "CHILD CONSTRAINT_NAME"
FROM ALL_CONS_COLUMNS CHILD
, ALL_CONSTRAINTS CT
, ALL_CONSTRAINTS PARENT
WHERE CHILD.OWNER = CT.OWNER
AND CT.CONSTRAINT_TYPE = 'R'
AND CHILD.CONSTRAINT_NAME = CT.CONSTRAINT_NAME
AND CT.R_OWNER = PARENT.OWNER
AND CT.R_CONSTRAINT_NAME = PARENT.CONSTRAINT_NAME
AND CHILD.TABLE_NAME = ::table -- table name variable
AND CT.OWNER = ::owner; -- schema variable, could not be needed
Its a bit late to anwser, but I hope my answer been useful for someone, who needs to select Composite foreign keys.
SELECT
"C"."CONSTRAINT_NAME",
"C"."OWNER" AS "SCHEMA_NAME",
"C"."TABLE_NAME",
"COL"."COLUMN_NAME",
"REF_COL"."OWNER" AS "REF_SCHEMA_NAME",
"REF_COL"."TABLE_NAME" AS "REF_TABLE_NAME",
"REF_COL"."COLUMN_NAME" AS "REF_COLUMN_NAME"
FROM
"USER_CONSTRAINTS" "C"
INNER JOIN "USER_CONS_COLUMNS" "COL" ON "COL"."OWNER" = "C"."OWNER"
AND "COL"."CONSTRAINT_NAME" = "C"."CONSTRAINT_NAME"
INNER JOIN "USER_CONS_COLUMNS" "REF_COL" ON "REF_COL"."OWNER" = "C"."R_OWNER"
AND "REF_COL"."CONSTRAINT_NAME" = "C"."R_CONSTRAINT_NAME"
AND "REF_COL"."POSITION" = "COL"."POSITION"
WHERE "C"."TABLE_NAME" = 'TableName' AND "C"."CONSTRAINT_TYPE" = 'R'
SELECT a.table_name, a.column_name, a.constraint_name, c.owner,
-- referenced pk
c.r_owner, c_pk.table_name r_table_name, c_pk.constraint_name r_pk
FROM all_cons_columns a
JOIN all_constraints c ON a.owner = c.owner
AND a.constraint_name = c.constraint_name
JOIN all_constraints c_pk ON c.r_owner = c_pk.owner
AND c.r_constraint_name = c_pk.constraint_name
WHERE c.constraint_type = 'R'
AND a.table_name :=TABLE_NAME
AND c.owner :=OWNER_NAME;
In case one wants to create FK constraints from UAT environment table to Live, fire below dynamic query.....
SELECT 'ALTER TABLE '||OBJ.NAME||' ADD CONSTRAINT '||CONST.NAME||' FOREIGN KEY ('||COALESCE(ACOL.NAME, COL.NAME)||') REFERENCES '
||ROBJ.NAME ||' ('||COALESCE(RACOL.NAME, RCOL.NAME) ||');'
FROM SYS.CON$ CONST
INNER JOIN SYS.CDEF$ CDEF ON CDEF.CON# = CONST.CON#
INNER JOIN SYS.CCOL$ CCOL ON CCOL.CON# = CONST.CON#
INNER JOIN SYS.COL$ COL ON (CCOL.OBJ# = COL.OBJ#) AND (CCOL.INTCOL# = COL.INTCOL#)
INNER JOIN SYS.OBJ$ OBJ ON CCOL.OBJ# = OBJ.OBJ#
LEFT JOIN SYS.ATTRCOL$ ACOL ON (CCOL.OBJ# = ACOL.OBJ#) AND (CCOL.INTCOL# = ACOL.INTCOL#)
INNER JOIN SYS.CON$ RCONST ON RCONST.CON# = CDEF.RCON#
INNER JOIN SYS.CCOL$ RCCOL ON RCCOL.CON# = RCONST.CON#
INNER JOIN SYS.COL$ RCOL ON (RCCOL.OBJ# = RCOL.OBJ#) AND (RCCOL.INTCOL# = RCOL.INTCOL#)
INNER JOIN SYS.OBJ$ ROBJ ON RCCOL.OBJ# = ROBJ.OBJ#
LEFT JOIN SYS.ATTRCOL$ RACOL ON (RCCOL.OBJ# = RACOL.OBJ#) AND (RCCOL.INTCOL# = RACOL.INTCOL#)
WHERE CONST.OWNER# = userenv('SCHEMAID')
AND RCONST.OWNER# = userenv('SCHEMAID')
AND CDEF.TYPE# = 4
AND OBJ.NAME = <table_name>;
I used below code and it served my purpose-
SELECT fk.owner, fk.table_name, col.column_name
FROM dba_constraints pk, dba_constraints fk, dba_cons_columns col
WHERE pk.constraint_name = fk.r_constraint_name
AND fk.constraint_name = col.constraint_name
AND pk.owner = col.owner
AND pk.owner = fk.owner
AND fk.constraint_type = 'R'
AND pk.owner = sys_context('USERENV', 'CURRENT_SCHEMA')
AND pk.table_name = :my_table
AND pk.constraint_type = 'P';
select d.table_name,
d.constraint_name "Primary Constraint Name",
b.constraint_name "Referenced Constraint Name"
from user_constraints d,
(select c.constraint_name,
c.r_constraint_name,
c.table_name
from user_constraints c
where table_name='EMPLOYEES' --your table name instead of EMPLOYEES
and constraint_type='R') b
where d.constraint_name=b.r_constraint_name
WITH reference_view AS
(SELECT a.owner, a.table_name, a.constraint_name, a.constraint_type,
a.r_owner, a.r_constraint_name, b.column_name
FROM dba_constraints a, dba_cons_columns b
WHERE a.owner LIKE UPPER ('SYS') AND
a.owner = b.owner
AND a.constraint_name = b.constraint_name
AND constraint_type = 'R'),
constraint_view AS
(SELECT a.owner a_owner, a.table_name, a.column_name, b.owner b_owner,
b.constraint_name
FROM dba_cons_columns a, dba_constraints b
WHERE a.owner = b.owner
AND a.constraint_name = b.constraint_name
AND b.constraint_type = 'P'
AND a.owner LIKE UPPER ('SYS')
)
SELECT
rv.table_name FK_Table , rv.column_name FK_Column ,
CV.table_name PK_Table , rv.column_name PK_Column , rv.r_constraint_name Constraint_Name
FROM reference_view rv, constraint_view CV
WHERE rv.r_constraint_name = CV.constraint_name AND rv.r_owner = CV.b_owner;
For Load UserTable (List of foreign keys and the tables they reference)
WITH
reference_view AS
(SELECT a.owner, a.table_name, a.constraint_name, a.constraint_type,
a.r_owner, a.r_constraint_name, b.column_name
FROM dba_constraints a, dba_cons_columns b
WHERE
a.owner = b.owner
AND a.constraint_name = b.constraint_name
AND constraint_type = 'R'),
constraint_view AS
(SELECT a.owner a_owner, a.table_name, a.column_name, b.owner b_owner,
b.constraint_name
FROM dba_cons_columns a, dba_constraints b
WHERE a.owner = b.owner
AND a.constraint_name = b.constraint_name
AND b.constraint_type = 'P'
) ,
usertableviewlist AS
(
select TABLE_NAME from user_tables
)
SELECT
rv.table_name FK_Table , rv.column_name FK_Column ,
CV.table_name PK_Table , rv.column_name PK_Column , rv.r_constraint_name Constraint_Name
FROM reference_view rv, constraint_view CV , usertableviewlist UTable
WHERE rv.r_constraint_name = CV.constraint_name AND rv.r_owner = CV.b_owner And UTable.TABLE_NAME = rv.table_name;
The more simple solution:
SELECT *
FROM all_indexes
WHERE index_name like '%FK_PROCESS%'

Resources