列出数据库中子表上没有对应索引的外键

from http://www.askmaclean.com/archives/list-foreign-keys-with-no-matching-index-on-child-table-causes-locks.html

REM  List foreign keys with no matching index on child table - causes locks

set linesize 150;

col owner for a20;
col COLUMN_NAME for a20;

SELECT C.OWNER, C.CONSTRAINT_NAME, C.TABLE_NAME, CC.COLUMN_NAME, C.STATUS
  FROM DBA_CONSTRAINTS C, DBA_CONS_COLUMNS CC
 WHERE C.CONSTRAINT_TYPE = 'R'
   AND C.OWNER NOT IN ('SYS',
                       'SYSTEM',
                       'SYSMAN',
                       'EXFSYS',
                       'WMSYS',
                       'OLAPSYS',
                       'OUTLN',
                       'DBSNMP',
                       'ORDSYS',
                       'ORDPLUGINS',
                       'MDSYS',
                       'CTXSYS',
                       'AURORA$ORB$UNAUTHENTICATED',
                       'XDB',
                       'FLOWS_030000',
                       'FLOWS_FILES')
   AND C.OWNER = CC.OWNER
   AND C.CONSTRAINT_NAME = CC.CONSTRAINT_NAME
   AND NOT EXISTS
 (SELECT 'x'
          FROM DBA_IND_COLUMNS IC
         WHERE CC.OWNER = IC.TABLE_OWNER
           AND CC.TABLE_NAME = IC.TABLE_NAME
           AND CC.COLUMN_NAME = IC.COLUMN_NAME
           AND CC.POSITION = IC.COLUMN_POSITION
           AND NOT EXISTS
         (SELECT OWNER, INDEX_NAME
                  FROM DBA_INDEXES I
                 WHERE I.TABLE_OWNER = C.OWNER
                   AND I.INDEX_NAME = IC.INDEX_NAME
                   AND I.OWNER = IC.INDEX_OWNER
                   AND (I.STATUS = 'UNUSABLE' OR
                       I.PARTITIONED = 'YES' AND EXISTS
                        (SELECT 'x'
                           FROM DBA_IND_PARTITIONS IP
                          WHERE STATUS = 'UNUSABLE'
                            AND IP. INDEX_OWNER = I. OWNER
                            AND IP. INDEX_NAME = I. INDEX_NAME
                         UNION ALL
                         SELECT 'x'
                           FROM DBA_IND_SUBPARTITIONS ISP
                          WHERE STATUS = 'UNUSABLE'
                            AND ISP. INDEX_OWNER = I. OWNER
                            AND ISP. INDEX_NAME = I. INDEX_NAME))))
 ORDER BY 1, 2


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值