简体   繁体   中英

How to check a particular column references parent table

I have a child table say B which references to some parent table say A . Now, suppose i have 3 coulmns in table B (example: B1, B2,B3 ). I wrote a query to get all the constraints in table B which is as follows:

SELECT a.table_name, 
       a.column_name
FROM ALL_CONS_COLUMNS A, ALL_CONSTRAINTS C  
where A.CONSTRAINT_NAME = C.CONSTRAINT_NAME 
  and a.table_name='B'
  and C.CONSTRAINT_TYPE = 'C'

Result:

Table Name Column Name
B          B1

Now i want to know that column B1 references to which parent table along with the column name too.

I just happened to have this query lying around:

SELECT
FK_Table = FK.TABLE_NAME,
FK_Column = CU.COLUMN_NAME,
PK_Table = PK.TABLE_NAME,
Constraint_Name = C.CONSTRAINT_NAME
FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS C
INNER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS FK ON C.CONSTRAINT_NAME = FK.CONSTRAINT_NAME
INNER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS PK ON C.UNIQUE_CONSTRAINT_NAME = PK.CONSTRAINT_NAME
INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE CU ON C.CONSTRAINT_NAME = CU.CONSTRAINT_NAME
INNER JOIN (
SELECT i1.TABLE_NAME, i2.COLUMN_NAME
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS i1
INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE i2 ON i1.CONSTRAINT_NAME = i2.CONSTRAINT_NAME
WHERE i1.CONSTRAINT_TYPE = 'FOREIGN KEY'
) PT ON PT.TABLE_NAME = PK.TABLE_NAME
---- optional:
ORDER BY
1,2,3,4

It should do what you want.

Try following sqls:

Get all child tables and columns by parent,

SELECT distinct confk.constraint_name,
  confk.constraint_type,
  confk.table_name child_table,
  fkcon.column_name child_column,
  conpk.table_name parent_table,
  pkcon.column_name parent_column
FROM dba_constraints confk,
  dba_constraints conpk,
  dba_cons_columns pkcon,
  dba_cons_columns fkcon
WHERE confk.r_constraint_name = conpk.constraint_name
AND conpk.constraint_name     = pkcon.constraint_name
AND confk.constraint_name     = fkcon.constraint_name
AND confk.constraint_type     = 'R'
AND pkcon.owner               = 'Your schema name'
AND pkcon.table_name          ='your parent table name'
AND pkcon.column_name         ='your parent column name';

Get the parent table name and column by child name and column,

SELECT distinct confk.constraint_name,
  confk.constraint_type,
  confk.table_name child_table,
  fkcon.column_name child_column,
  conpk.table_name parent_table,
  pkcon.column_name parent_column
FROM dba_constraints confk,
  dba_constraints conpk,
  dba_cons_columns pkcon,
  dba_cons_columns fkcon
WHERE confk.r_constraint_name = conpk.constraint_name
AND conpk.constraint_name     = pkcon.constraint_name
AND confk.constraint_name     = fkcon.constraint_name
AND confk.constraint_type     = 'R'
AND fkcon.owner               = 'Your schema name'
AND fkcon.table_name          ='your child table name'
AND fkcon.column_name         ='your child column name';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM