外键查询

1.查询该表的主键被那些表作为外键使用:

SELECT B.TABLE_NAME 
FROM ALL_CONSTRAINTS A, ALL_CONSTRAINTS B
WHERE A.TABLE_NAME = '表名'
 AND A.OWNER = SYS_CONTEXT('USERENV', 'CURRENT_SCHEMA')
 AND B.OWNER = SYS_CONTEXT('USERENV', 'CURRENT_SCHEMA')
 AND B.CONSTRAINT_TYPE = 'R'
 AND A.CONSTRAINT_NAME = B.R_CONSTRAINT_NAME;

2.查询该表引用了那些表的主键作为外键:

SELECT A.TABLE_NAME
FROM USER_CONS_COLUMNS A
LEFT JOIN ALL_CONSTRAINTS B
 ON A.CONSTRAINT_NAME = B.R_CONSTRAINT_NAME
WHERE B.CONSTRAINT_TYPE = 'R'
 AND B.TABLE_NAME = '表名';

 

你可能感兴趣的:(数据库)